diff --git a/analytical/templatetags/analytical.py b/analytical/templatetags/analytical.py index 72eaeb6..b8365f0 100644 --- a/analytical/templatetags/analytical.py +++ b/analytical/templatetags/analytical.py @@ -5,14 +5,13 @@ from __future__ import absolute_import import logging +from importlib import import_module from django import template from django.template import Node, TemplateSyntaxError -from importlib import import_module from analytical.utils import AnalyticalException - TAG_LOCATIONS = ['head_top', 'head_bottom', 'body_top', 'body_bottom'] TAG_POSITIONS = ['first', None, 'last'] TAG_MODULES = [ @@ -83,7 +82,7 @@ def add_node_cls(location, node, position=None): try: module.contribute_to_analytical(add_node_cls) except AnalyticalException as e: - logger.debug("not loading tags from '%s': %s", path, e) + logger.trace("django-analytical settings not (or not properly) configured for '%s': %s", path, e) for location in TAG_LOCATIONS: template_nodes[location] = sum((template_nodes[location][p] for p in TAG_POSITIONS), []) diff --git a/analytical/templatetags/chartbeat.py b/analytical/templatetags/chartbeat.py index 6168c13..90a7af4 100644 --- a/analytical/templatetags/chartbeat.py +++ b/analytical/templatetags/chartbeat.py @@ -13,7 +13,6 @@ from analytical.utils import is_internal_ip, disable_html, get_required_setting - USER_ID_RE = re.compile(r'^\d+$') INIT_CODE = """""" SETUP_CODE = """ @@ -57,6 +56,8 @@ def chartbeat_top(parser, token): class ChartbeatTopNode(Node): def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" if is_internal_ip(context): return disable_html(INIT_CODE, "Chartbeat") return INIT_CODE @@ -83,6 +84,8 @@ def __init__(self): "must be (a string containing) a number") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" config = {'uid': self.user_id} domain = _get_domain(context) if domain is not None: diff --git a/analytical/templatetags/clickmap.py b/analytical/templatetags/clickmap.py index 8d7f00f..15a404c 100644 --- a/analytical/templatetags/clickmap.py +++ b/analytical/templatetags/clickmap.py @@ -6,11 +6,11 @@ import re +from django.conf import settings from django.template import Library, Node, TemplateSyntaxError from analytical.utils import is_internal_ip, disable_html, get_required_setting - CLICKMAP_TRACKER_ID_RE = re.compile(r'^\w+$') TRACKING_CODE = """ '.\ @@ -45,6 +45,8 @@ def __init__(self): ) def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" html = SETUP_CODE % { 'account_nr_1': self.account_nr[:4], 'account_nr_2': self.account_nr[4:], diff --git a/analytical/templatetags/facebook_pixel.py b/analytical/templatetags/facebook_pixel.py index 33dac0a..2624d2d 100644 --- a/analytical/templatetags/facebook_pixel.py +++ b/analytical/templatetags/facebook_pixel.py @@ -5,11 +5,11 @@ import re +from django.conf import settings from django.template import Library, Node, TemplateSyntaxError from analytical.utils import get_required_setting, is_internal_ip, disable_html - FACEBOOK_PIXEL_HEAD_CODE = """\ """ @@ -40,6 +40,8 @@ def __init__(self): "must be a string looking like 'XXXXXXX'") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" html = SETUP_CODE % {'account_number': self.account_number} if is_internal_ip(context, 'OPTIMIZELY'): html = disable_html(html, 'Optimizely') diff --git a/analytical/templatetags/performable.py b/analytical/templatetags/performable.py index 3364e18..f8fb8b2 100644 --- a/analytical/templatetags/performable.py +++ b/analytical/templatetags/performable.py @@ -6,12 +6,12 @@ import re +from django.conf import settings from django.template import Library, Node, TemplateSyntaxError from django.utils.safestring import mark_safe from analytical.utils import is_internal_ip, disable_html, get_identity, \ - get_required_setting - + get_required_setting API_KEY_RE = re.compile(r'^\w+$') SETUP_CODE = """ @@ -59,6 +59,8 @@ def __init__(self): "must be a string looking like 'XXXXX'") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" html = SETUP_CODE % {'api_key': self.api_key} identity = get_identity(context, 'performable') if identity is not None: diff --git a/analytical/templatetags/piwik.py b/analytical/templatetags/piwik.py index 37d9777..6b673f0 100644 --- a/analytical/templatetags/piwik.py +++ b/analytical/templatetags/piwik.py @@ -4,9 +4,10 @@ from __future__ import absolute_import +import re +import warnings from collections import namedtuple from itertools import chain -import re from django.conf import settings from django.template import Library, Node, TemplateSyntaxError @@ -14,7 +15,6 @@ from analytical.utils import (is_internal_ip, disable_html, get_required_setting, get_identity) -import warnings warnings.warn('The Piwik module is deprecated; use the Matomo module.', DeprecationWarning) # domain name (characters separated by a dot), optional port, optional URI path, no slash @@ -33,7 +33,7 @@ (function() { var u="//%(url)s/"; _paq.push(['setTrackerUrl', u+'piwik.php']); - _paq.push(['setSiteId', %(siteid)s]); + _paq.push(['setSiteId', '%(siteid)s']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); })(); @@ -86,6 +86,8 @@ def __init__(self): "must be a (string containing a) number") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" custom_variables = context.get('piwik_vars', ()) complete_variables = (var if len(var) >= 4 else var + (DEFAULT_SCOPE,) diff --git a/analytical/templatetags/rating_mailru.py b/analytical/templatetags/rating_mailru.py index ba1da7e..6209170 100644 --- a/analytical/templatetags/rating_mailru.py +++ b/analytical/templatetags/rating_mailru.py @@ -6,11 +6,11 @@ import re +from django.conf import settings from django.template import Library, Node, TemplateSyntaxError from analytical.utils import is_internal_ip, disable_html, \ - get_required_setting - + get_required_setting COUNTER_ID_RE = re.compile(r'^\d{7}$') COUNTER_CODE = """ @@ -56,6 +56,8 @@ def __init__(self): "must be (a string containing) a number'") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" html = COUNTER_CODE % { 'counter_id': self.counter_id, } diff --git a/analytical/templatetags/snapengage.py b/analytical/templatetags/snapengage.py index 5881a50..40d38e0 100644 --- a/analytical/templatetags/snapengage.py +++ b/analytical/templatetags/snapengage.py @@ -73,6 +73,8 @@ def __init__(self): "must be a string looking like this: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" settings_code = [] domain = self._get_setting(context, 'snapengage_domain', diff --git a/analytical/templatetags/spring_metrics.py b/analytical/templatetags/spring_metrics.py index a3093ea..08d8213 100644 --- a/analytical/templatetags/spring_metrics.py +++ b/analytical/templatetags/spring_metrics.py @@ -6,11 +6,11 @@ import re +from django.conf import settings from django.template import Library, Node, TemplateSyntaxError from analytical.utils import get_identity, is_internal_ip, disable_html, \ - get_required_setting - + get_required_setting TRACKING_ID_RE = re.compile(r'^[0-9a-f]+$') TRACKING_CODE = """ @@ -56,6 +56,8 @@ def __init__(self): TRACKING_ID_RE, "must be a hexadecimal string") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" custom = {} for dict_ in context: for var, val in dict_.items(): diff --git a/analytical/templatetags/uservoice.py b/analytical/templatetags/uservoice.py index 68f59db..b97e7bc 100644 --- a/analytical/templatetags/uservoice.py +++ b/analytical/templatetags/uservoice.py @@ -9,8 +9,8 @@ from django.conf import settings from django.template import Library, Node, TemplateSyntaxError -from analytical.utils import get_required_setting, get_identity +from analytical.utils import get_required_setting, get_identity WIDGET_KEY_RE = re.compile(r'^[a-zA-Z0-9]*$') TRACKING_CODE = """ @@ -53,6 +53,8 @@ def __init__(self): 'USERVOICE_WIDGET_KEY', WIDGET_KEY_RE, "must be an alphanumeric string") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" widget_key = context.get('uservoice_widget_key') if not widget_key: widget_key = self.default_widget_key diff --git a/analytical/templatetags/woopra.py b/analytical/templatetags/woopra.py index e6c02cc..f70d95c 100644 --- a/analytical/templatetags/woopra.py +++ b/analytical/templatetags/woopra.py @@ -55,11 +55,13 @@ def __init__(self): "must be a domain name") def render(self, context): - settings = self._get_settings(context) + if settings.get("DISABLE_TRACKING_CODE", False): + return "" + cfg = self._get_settings(context) visitor = self._get_visitor(context) html = TRACKING_CODE % { - 'settings': json.dumps(settings, sort_keys=True), + 'settings': json.dumps(cfg, sort_keys=True), 'visitor': json.dumps(visitor, sort_keys=True), } if is_internal_ip(context, 'WOOPRA'): diff --git a/analytical/templatetags/yandex_metrica.py b/analytical/templatetags/yandex_metrica.py index 4e3270f..b895c49 100644 --- a/analytical/templatetags/yandex_metrica.py +++ b/analytical/templatetags/yandex_metrica.py @@ -11,8 +11,7 @@ from django.template import Library, Node, TemplateSyntaxError from analytical.utils import is_internal_ip, disable_html, \ - get_required_setting - + get_required_setting COUNTER_ID_RE = re.compile(r'^\d{8}$') COUNTER_CODE = """ @@ -65,6 +64,8 @@ def __init__(self): "must be (a string containing) a number'") def render(self, context): + if settings.get("DISABLE_TRACKING_CODE", False): + return "" options = { 'id': int(self.counter_id), 'clickmap': True, diff --git a/docs/install.rst b/docs/install.rst index 1f97f07..417b765 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -195,6 +195,8 @@ settings required to enable each service are listed here: ---- +To disable the tracking code set DISABLE_TRACKING_CODE=True in settings (default=False). + The django-analytical application is now set-up to track visitors. For information about identifying users, further configuration and customization, see :doc:`features`. diff --git a/docs/services/matomo.rst b/docs/services/matomo.rst index d6a5b83..bbca836 100644 --- a/docs/services/matomo.rst +++ b/docs/services/matomo.rst @@ -25,12 +25,15 @@ The Matomo tracking code is inserted into templates using a template tag. Load the :mod:`matomo` template tag library and insert the :ttag:`matomo` 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 body as recommended by the +the tag at the bottom of the HEAD tag as recommended by the `Matomo best practice for Integration Plugins`_:: {% load matomo %} + + ... {% matomo %} +