Django Google Analytics Measurement Protocol integration
Usage
Installation
pip install django-ga-measurement-protocol
Middleware
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
Additional functions
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.
Debug
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.
Development
Installation
It is recommended to initialise a virtualenv of your choice.
pip install -r requirements.txt
Formatting
Formatting is done via Black and a pre-commit hook should be installed.
pre-commit install
Tests
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
Metadata
Release files for django-ga-measurement-protocol 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-ga-measurement-protocol-0.2.0.tar.gz | 8.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_ga_measurement_protocol-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.1 kB
Release files / django-ga-measurement-protocol-0.2.0.tar.gz
| Download URL | django-ga-measurement-protocol-0.2.0.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d49582f1901a6eb50f62a01e439d464f7d45a6364d424775a575048cb9a9269e
|
|
BLAKE2b-256 checksum How to use checksums |
5bf7b3a788c22f78c53c43daa94ce7d4fb29ecea663eeb3ffa58723660fa2a13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.10
|
Release files / django_ga_measurement_protocol-0.2.0-py3-none-any.whl
| Download URL | django_ga_measurement_protocol-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a18e1ba8542604af073b7eb453a28eff1929edffbfb52ac458a7a6b9f423ab0d
|
|
BLAKE2b-256 checksum How to use checksums |
52911a5cffc6c77755bc9730da7c89e31ccbcbf7157a346bda7f7da596435fe9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.10
|
Security for django-ga-measurement-protocol 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.