Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
django-ga-measurement-protocol
Advanced tools
A Django app to provide Google Analytics measurement protocol integration.
pip install django-ga-measurement-protocol
The most basic usage is to install the page view middleware:
MIDDLEWARE = [
...
"django_ga_measurement_protocol.middleware.page_view_tracking_middleware",
...
]
and configure the required settings:
GA_MEASUREMENT_PROTOCOL_UA = "<tracking id>" # The tracking id for your property
GA_MEASUREMENT_PROTOCOL_TRACK_EVENTS = True # By default the package won't do any tracking unless explicitly set
There are additional functions that allow for custom tracking beyond what the the middleware provides.
from django_ga_measurement_protocol.track import (
track_event,
track_page_view,
)
def a_view(request):
# Tracks an event in Google Analytics
track_event(
request,
category="Category", # required
action="Action", # required
label="Label", # optional
value="5", # optional
)
# Tracks a page view in Google Analytics
track_page_view(request)
The above functions will extract the basic required information from the Django request that is required to register an event/page view in Google Analytics.
If more customisation is required there are additional functions that allow fully custom data to be sent to Google Analytics.
from django_ga_measurement_protocol.track import (
build_tracking_data,
send_tracking_data,
)
def a_view(request):
# Builds tracking data with default information from the Django request
tracking_data = build_tracking_data(
request,
{
"additional-value": "value",
},
)
# Sends tracking data to Google analytics
send_tracking_data(tracking_data)
The values that are accepted by the measurement protocol can be found here.
When sending data to Google Analytics, by default, nearly all data is accepted even if it is missing required data or is corrupted in some other way. However, there is a facility for Google Analytics to respond with some validation data for your events.
To turn this on set the following settings:
GA_MEASUREMENT_PROTOCOL_DEBUG = True
LOGGING = {
...
'loggers': {
...
'django_ga_measurement_protocol': {
'level': logging.DEBUG,
'handlers': ['console'],
},
},
}
This will log the validation in the response returned from Google Analytics.
This should not be used in production.
It is recommended to initialise a virtualenv of your choice.
pip install -r requirements.txt
Formatting is done via Black and a pre-commit hook should be installed.
pre-commit install
To run the tests with the currently installed Python and Django version run:
pytest
To run against all of the possible compatible Python and Django versions run:
tox
FAQs
A Django app to provide Google Analytics measurement protocol integration.
We found that django-ga-measurement-protocol demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.