Google Analytics (analytics.js) – traffic analysis¶
Google Analytics is the well-known web analytics service from Google. The product is aimed more at marketers than webmasters or technologists, supporting integration with AdWords and other e-commence features. The analytics.js library (also known as “the Google Analytics tag”) is a JavaScript library for measuring how users interact with your website.
Installation¶
To start using the Google Analytics integration, you must have installed
the django-analytical package and have added the analytical
application to INSTALLED_APPS
in your project
settings.py
file. See Installation and configuration for details.
Next you need to add the Google Analytics template tag to your
templates. This step is only needed if you are not using the generic
analytical.*
tags. If you are, skip to
Configuration.
The Google Analytics tracking code is inserted into templates using a
template tag. Load the google_analytics_js
template tag library and
insert the google_analytics_js
tag. Because every page that you
want to track must have the tag, it is useful to add it to your base
template. Insert the tag at the bottom of the HTML head:
{% load google_analytics_js %}
<html>
<head>
...
{% google_analytics_js %}
</head>
...
Configuration¶
Before you can use the Google Analytics integration, you must first set your website property ID. If you track multiple domains with the same code, you also need to set-up the domain. Finally, you can add custom segments for Google Analytics to track.
Setting the property ID¶
Every website you track with Google Analytics gets its own property ID,
and the google_analytics_js
tag will include it in the rendered
Javascript code. You can find the web property ID on the overview page
of your account. Set GOOGLE_ANALYTICS_JS_PROPERTY_ID
in the
project settings.py
file:
GOOGLE_ANALYTICS_JS_PROPERTY_ID = 'UA-XXXXXXXX-X'
If you do not set a property ID, the tracking code will not be rendered.
Tracking multiple domains¶
The default code is suitable for tracking a single domain. If you track
multiple domains, set the GOOGLE_ANALYTICS_TRACKING_STYLE
setting to one of the analytical.templatetags.google_analytics_js.TRACK_*
constants:
Constant | Value | Description |
---|---|---|
TRACK_SINGLE_DOMAIN |
1 | Track one domain. |
TRACK_MULTIPLE_SUBDOMAINS |
2 | Track multiple subdomains of the same top domain (e.g. fr.example.com and nl.example.com). |
TRACK_MULTIPLE_DOMAINS |
3 | Track multiple top domains (e.g. example.fr and example.nl). |
As noted, the default tracking style is
TRACK_SINGLE_DOMAIN
.
When you track multiple (sub)domains, django-analytical needs to know
what domain name to pass to Google Analytics. If you use the contrib
sites app, the domain is automatically picked up from the current
Site
instance. Otherwise, you may
either pass the domain to the template tag through the context variable
google_analytics_domain
(fallback: analytical_domain
)
or set it in the project settings.py
file using
GOOGLE_ANALYTICS_DOMAIN
(fallback: ANALYTICAL_DOMAIN
).
Display Advertising¶
Display Advertising allows you to view Demographics and Interests reports, add Remarketing Lists and support DoubleClick Campain Manager integration.
You can enable Display Advertising features by setting the
GOOGLE_ANALYTICS_DISPLAY_ADVERTISING
configuration setting:
GOOGLE_ANALYTICS_DISPLAY_ADVERTISING = True
By default, display advertising features are disabled.
Internal IP addresses¶
Usually you do not want to track clicks from your development or
internal IP addresses. By default, if the tags detect that the client
comes from any address in the GOOGLE_ANALYTICS_INTERNAL_IPS
setting, the tracking code is commented out. It takes the value of
ANALYTICAL_INTERNAL_IPS
by default (which in turn is
INTERNAL_IPS
by default). See Identifying authenticated users for
important information about detecting the visitor IP address.
Custom variables¶
As described in the Google Analytics custom variables documentation
page, you can define custom segments. Using template context variables
google_analytics_var1
through google_analytics_var5
, you can let
the google_analytics_js
tag pass custom variables to Google
Analytics automatically. You can set the context variables in your view
when your render a template containing the tracking code:
context = RequestContext({'google_analytics_var1': ('gender', 'female'),
'google_analytics_var2': ('visit', 1)})
return some_template.render(context)
The value of the context variable is a tuple (name, value).
You may want to set custom variables in a context processor that you add
to the TEMPLATE_CONTEXT_PROCESSORS
list in settings.py
:
def google_analytics_segment_language(request):
try:
return {'google_analytics_var3': request.LANGUAGE_CODE}
except AttributeError:
return {}
Just remember that if you set the same context variable in the
RequestContext
constructor and in a
context processor, the latter clobbers the former.
Anonymize IPs¶
You can enable the IP anonymization feature by setting the
GOOGLE_ANALYTICS_ANONYMIZE_IP
configuration setting:
GOOGLE_ANALYTICS_ANONYMIZE_IP = True
This may be mandatory for deployments in countries that have a firm policies concerning data privacy (e.g. Germany).
By default, IPs are not anonymized.
Sample Rate¶
You can configure the Sample Rate feature by setting the
GOOGLE_ANALYTICS_SAMPLE_RATE
configuration setting:
GOOGLE_ANALYTICS_SAMPLE_RATE = 10
The value is a percentage and can be between 0 and 100 and can be a string or integer value.
Site Speed Sample Rate¶
You can configure the Site Speed Sample Rate feature by setting the
GOOGLE_ANALYTICS_SITE_SPEED_SAMPLE_RATE
configuration setting:
GOOGLE_ANALYTICS_SITE_SPEED_SAMPLE_RATE = 10
The value is a percentage and can be between 0 and 100 and can be a string or integer value.
Cookie Expiration¶
You can configure the Cookie Expiration feature by setting the
GOOGLE_ANALYTICS_COOKIE_EXPIRATION
configuration setting:
GOOGLE_ANALYTICS_COOKIE_EXPIRATION = 3600000
The value is the cookie expiration in seconds or 0 to delete the cookie when the browser is closed.
Custom Javascript Source¶
You can configure a custom URL for the javascript file by setting the
GOOGLE_ANALYTICS_JS_SOURCE
configuration setting:
GOOGLE_ANALYTICS_JS_SOURCE = 'https://www.example.com/analytics.js'