From 756ac787e822eee87f1753d7165be70663bc6924 Mon Sep 17 00:00:00 2001 From: Marc Bourqui Date: Wed, 24 Oct 2018 16:05:32 +0200 Subject: [PATCH] Add support for gtag.js --- .../templatetags/google_analytics_gtag.py | 65 +++++++++++++++ docs/services/google_analytics_gtag.rst | 80 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 analytical/templatetags/google_analytics_gtag.py create mode 100644 docs/services/google_analytics_gtag.rst diff --git a/analytical/templatetags/google_analytics_gtag.py b/analytical/templatetags/google_analytics_gtag.py new file mode 100644 index 0000000..a2f47d7 --- /dev/null +++ b/analytical/templatetags/google_analytics_gtag.py @@ -0,0 +1,65 @@ +""" +Google Analytics template tags and filters, using the new analytics.js library. +""" + +from __future__ import absolute_import + +import re + +from django.template import Library, Node, TemplateSyntaxError + +from analytical.utils import ( + disable_html, + get_required_setting, + is_internal_ip, +) + +PROPERTY_ID_RE = re.compile(r'^UA-\d+-\d+$') +SETUP_CODE = """ + + + +""" + +register = Library() + + +@register.tag +def google_analytics_gtag(parser, token): + """ + Google Analytics tracking template tag. + + Renders Javascript code to track page visits. You must supply + your website property ID (as a string) in the + ``GOOGLE_ANALYTICS_JS_PROPERTY_ID`` setting. + """ + bits = token.split_contents() + if len(bits) > 1: + raise TemplateSyntaxError("'%s' takes no arguments" % bits[0]) + return GoogleAnalyticsGTagNode() + + +class GoogleAnalyticsGTagNode(Node): + def __init__(self): + self.property_id = get_required_setting( + 'GOOGLE_ANALYTICS_GTAG_PROPERTY_ID', PROPERTY_ID_RE, + "must be a string looking like 'UA-XXXXXX-Y'") + + def render(self, context): + html = SETUP_CODE.format( + property_id=self.property_id, + ) + if is_internal_ip(context, 'GOOGLE_ANALYTICS'): + html = disable_html(html, 'Google Analytics') + return html + + +def contribute_to_analytical(add_node): + GoogleAnalyticsGTagNode() # ensure properly configured + add_node('head_top', GoogleAnalyticsGTagNode) diff --git a/docs/services/google_analytics_gtag.rst b/docs/services/google_analytics_gtag.rst new file mode 100644 index 0000000..5a8358c --- /dev/null +++ b/docs/services/google_analytics_gtag.rst @@ -0,0 +1,80 @@ +====================================== + Google Analytics -- 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. + +.. _`Google Analytics`: http://www.google.com/analytics/ + + +.. google-analytics-installation: + +Installation +============ + +To start using the Google Analytics integration, you must have installed +the django-analytical package and have added the ``analytical`` +application to :const:`INSTALLED_APPS` in your project +:file:`settings.py` file. See :doc:`../install` 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 +:ttag:`analytical.*` tags. If you are, skip to +:ref:`google-analytics-configuration`. + +The Google Analytics tracking code is inserted into templates using a +template tag. Load the :mod:`google_analytics_gtag` template tag library and +insert the :ttag:`google_analytics_gtag` 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_gtag %} + + + {% google_analytics_gtag %} + ... + + ... + + +.. _google-analytics-configuration: + +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. + + +.. _google-analytics-property-id: + +Setting the property ID +----------------------- + +Every website you track with Google Analytics gets its own property ID, +and the :ttag:`google_analytics_gtag` tag will include it in the rendered +Javascript code. You can find the web property ID on the overview page +of your account. Set :const:`GOOGLE_ANALYTICS_GTAG_PROPERTY_ID` in the +project :file:`settings.py` file:: + + GOOGLE_ANALYTICS_GTAG_PROPERTY_ID = 'UA-XXXXXX-X' + +If you do not set a property ID, the tracking code will not be rendered. + + + +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 :const:`GOOGLE_ANALYTICS_INTERNAL_IPS` +setting, the tracking code is commented out. It takes the value of +:const:`ANALYTICAL_INTERNAL_IPS` by default (which in turn is +:const:`INTERNAL_IPS` by default). See :ref:`identifying-visitors` for +important information about detecting the visitor IP address.