Skip to main content

A Django app to provide Google Analytics measurement protocol integration.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ga-measurement-protocol-0.2.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ga_measurement_protocol-0.2.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file django-ga-measurement-protocol-0.2.0.tar.gz.

File metadata

  • Download URL: django-ga-measurement-protocol-0.2.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-ga-measurement-protocol-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d49582f1901a6eb50f62a01e439d464f7d45a6364d424775a575048cb9a9269e
MD5 e6a569fda9fb6306a80e08a10884ee7b
BLAKE2b-256 5bf7b3a788c22f78c53c43daa94ce7d4fb29ecea663eeb3ffa58723660fa2a13

See more details on using hashes here.

File details

Details for the file django_ga_measurement_protocol-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_ga_measurement_protocol-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_ga_measurement_protocol-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a18e1ba8542604af073b7eb453a28eff1929edffbfb52ac458a7a6b9f423ab0d
MD5 5ee38421a185523c6257c1cdd4d1cbf6
BLAKE2b-256 52911a5cffc6c77755bc9730da7c89e31ccbcbf7157a346bda7f7da596435fe9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page