From ba59f396df0d740bae86c8bea7fad406b3d2af23 Mon Sep 17 00:00:00 2001 From: Kevin Olbrich Date: Tue, 5 Jul 2022 14:09:37 +0200 Subject: [PATCH] add GOOGLE_ANALYTICS_JS_SOURCE setting to allow custom source URL fixes https://github.com/jazzband/django-analytical/issues/212 --- analytical/templatetags/google_analytics_js.py | 9 ++++++++- docs/services/google_analytics_js.rst | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/analytical/templatetags/google_analytics_js.py b/analytical/templatetags/google_analytics_js.py index 59bdcfa..6f133bb 100644 --- a/analytical/templatetags/google_analytics_js.py +++ b/analytical/templatetags/google_analytics_js.py @@ -26,7 +26,7 @@ SETUP_CODE = """ (function(i,s,o,g,r,a,m){{i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){{ (i[r].q=i[r].q||[]).push(arguments)}},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) -}})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); +}})(window,document,'script','{js_source}','ga'); ga('create', '{property_id}', 'auto', {create_fields}); {commands}ga('send', 'pageview'); @@ -70,10 +70,17 @@ class GoogleAnalyticsJsNode(Node): if display_features: commands.insert(0, REQUIRE_DISPLAY_FEATURES) + js_source = getattr( + settings, + 'GOOGLE_ANALYTICS_JS_SOURCE', + 'https://www.google-analytics.com/analytics.js' + ) + html = SETUP_CODE.format( property_id=self.property_id, create_fields=json.dumps(create_fields), commands="".join(commands), + js_source=js_source, ) if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') diff --git a/docs/services/google_analytics_js.rst b/docs/services/google_analytics_js.rst index a40e521..b064d2e 100644 --- a/docs/services/google_analytics_js.rst +++ b/docs/services/google_analytics_js.rst @@ -228,3 +228,11 @@ You can configure the `Cookie Expiration`_ feature by setting the The value is the cookie expiration in seconds or 0 to delete the cookie when the browser is closed. .. _`Cookie Expiration`: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiBasicConfiguration#_setsessioncookietimeout + +Custom Javascript Source +---------------------- + +You can configure a custom URL for the javascript file by setting the +:const:`GOOGLE_ANALYTICS_JS_SOURCE` configuration setting:: + + GOOGLE_ANALYTICS_JS_SOURCE = 'https://www.example.com/analytics.js'