From 315dcfcb65e42e1a964e2d837ecc921ae72c6599 Mon Sep 17 00:00:00 2001 From: CM Lubinski Date: Mon, 14 Sep 2015 23:02:23 +0000 Subject: [PATCH 01/10] Move analytics code into its own template --- regulations/templates/regulations/analytics.html | 12 ++++++++++++ regulations/templates/regulations/base.html | 10 +--------- 2 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 regulations/templates/regulations/analytics.html diff --git a/regulations/templates/regulations/analytics.html b/regulations/templates/regulations/analytics.html new file mode 100644 index 00000000..81b3837e --- /dev/null +++ b/regulations/templates/regulations/analytics.html @@ -0,0 +1,12 @@ +{% comment %} +Analytics tags. Override for different IDs +{% endcomment %} + + + + diff --git a/regulations/templates/regulations/base.html b/regulations/templates/regulations/base.html index fc937e65..c8da15cc 100644 --- a/regulations/templates/regulations/base.html +++ b/regulations/templates/regulations/base.html @@ -54,15 +54,7 @@ - - - - + {% include "regulations/analytics.html" %} {% block body %} {% endblock %} From 6a33d718d4252c9cd88b6a638d34bdf22a467d2b Mon Sep 17 00:00:00 2001 From: jbarnicle Date: Thu, 19 Nov 2015 14:22:19 -0600 Subject: [PATCH 02/10] Implement site specific Google Analytics --- regulations/settings/base.py | 4 ++-- regulations/templates/regulations/base.html | 13 +++++++++++++ regulations/views/utils.py | 13 +------------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/regulations/settings/base.py b/regulations/settings/base.py index b64852a0..b280b560 100644 --- a/regulations/settings/base.py +++ b/regulations/settings/base.py @@ -130,8 +130,8 @@ DATE_FORMAT = 'n/j/Y' -EREGS_GA_ID = '' -EREGS_GA_SITE = '' +# Google Analytics UA-xxxxxx-x code +EREGS_GA_SITE_ID = '' # Use the 'source' directory; useful for development JS_DEBUG = False diff --git a/regulations/templates/regulations/base.html b/regulations/templates/regulations/base.html index c8da15cc..ea050349 100644 --- a/regulations/templates/regulations/base.html +++ b/regulations/templates/regulations/base.html @@ -20,6 +20,19 @@ + {% if eregs_ga_site_id != '' %} + + {% endif %} {% if env == 'built' %} - + +{% if EREGS_GTM_SITE_ID != '' %} + + + + +{% endif %} +{% if EREGS_GA_SITE_ID != '' %} + +{% endif %} +{% if EREGS_DAP_AGENCY != '' %} + +{% endif %} diff --git a/regulations/templates/regulations/base.html b/regulations/templates/regulations/base.html index ea050349..c8da15cc 100644 --- a/regulations/templates/regulations/base.html +++ b/regulations/templates/regulations/base.html @@ -20,19 +20,6 @@ - {% if eregs_ga_site_id != '' %} - - {% endif %} {% if env == 'built' %} +{% endif %} diff --git a/regulations/templates/regulations/google_analytics.html b/regulations/templates/regulations/google_analytics.html new file mode 100644 index 00000000..2b6f2022 --- /dev/null +++ b/regulations/templates/regulations/google_analytics.html @@ -0,0 +1,28 @@ +{% comment %} +Google Analytics tags. Override for different IDs +{% endcomment %} + +{% if ANALYTICS.GOOGLE.GTM_SITE_ID %} + + + + +{% endif %} +{% if ANALYTICS.GOOGLE.GA_SITE_ID %} + +{% endif %} \ No newline at end of file diff --git a/regulations/views/utils.py b/regulations/views/utils.py index 73d37e1d..c8168ace 100644 --- a/regulations/views/utils.py +++ b/regulations/views/utils.py @@ -1,5 +1,6 @@ #vim: set encoding=utf-8 import itertools +import urllib from django.conf import settings from django.core.urlresolvers import reverse @@ -61,12 +62,20 @@ def add_extras(context): prefix = reverse('regulation_landing_view', kwargs={'label_id': '9999'}) prefix = prefix.replace('9999', '') context['APP_PREFIX'] = prefix - context['EREGS_GA_SITE_ID'] = getattr(settings, 'EREGS_GA_SITE_ID', '') - context['EREGS_GTM_SITE_ID'] = getattr(settings, 'EREGS_GTM_SITE_ID', '') - context['EREGS_DAP_AGENCY'] = getattr(settings, 'EREGS_DAP_AGENCY', '') - context['EREGS_DAP_SUBAGENCY'] = getattr(settings, 'EREGS_DAP_SUBAGENCY', '') + context['ANALYTICS'] = getattr(settings, 'ANALYTICS', {}) + if 'DAP' in context['ANALYTICS']: + context['ANALYTICS']['DAP']['DAP_URL_PARAMS'] = create_dap_url_params(context['ANALYTICS']['DAP']) return context +def create_dap_url_params(dap_settings): + """ Create the DAP url string to append to script tag """ + dap_params = {} + if 'AGENCY' in dap_settings and dap_settings['AGENCY']: + dap_params['agency'] = dap_settings['AGENCY'] + if 'SUBAGENCY' in dap_settings and dap_settings['SUBAGENCY']: + dap_params['subagency'] = dap_settings['SUBAGENCY'] + + return urllib.urlencode(dap_params) def first_section(reg_part, version): """ Use the table of contents for a regulation, to get the label of the From ca6ead81b98cf0f8cb1c22cd6659541c1ee04757 Mon Sep 17 00:00:00 2001 From: jbarnicle Date: Mon, 23 Nov 2015 10:26:08 -0600 Subject: [PATCH 08/10] remove old analytics attribes - set defaults to blank --- regulations/settings/base.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/regulations/settings/base.py b/regulations/settings/base.py index 3adae666..3c68f89a 100644 --- a/regulations/settings/base.py +++ b/regulations/settings/base.py @@ -134,25 +134,15 @@ ANALYTICS = { 'GOOGLE': { - 'GTM_SITE_ID': 'GTM-KMMLRS', - 'GA_SITE_ID': 'UA-xxxxxx-x', + 'GTM_SITE_ID': '', + 'GA_SITE_ID': '', }, 'DAP': { - 'AGENCY': 'DOJ', - 'SUBAGENCY': 'ATF', + 'AGENCY': '', + 'SUBAGENCY': '', }, } -# Google Tag Manager ipmlementations -EREGS_GTM_SITE_ID = 'GTM-KMMLRS' - -# Google Analytics UA-xxxxxx-x code -EREGS_GA_SITE_ID = '' - -#DAP Universal Analytics -EREGS_DAP_AGENCY = '' -EREGS_DAP_SUBAGENCY = '' - # Use the 'source' directory; useful for development JS_DEBUG = False From 97069b1a23059a24e533fb454310c903c50698b9 Mon Sep 17 00:00:00 2001 From: jbarnicle Date: Mon, 23 Nov 2015 10:31:30 -0600 Subject: [PATCH 09/10] remove non-namespaced analytics template --- .../templates/regulations/analytics.html | 31 ------------------- .../regulations/google_analytics.html | 4 +-- 2 files changed, 2 insertions(+), 33 deletions(-) delete mode 100644 regulations/templates/regulations/analytics.html diff --git a/regulations/templates/regulations/analytics.html b/regulations/templates/regulations/analytics.html deleted file mode 100644 index bfb55e83..00000000 --- a/regulations/templates/regulations/analytics.html +++ /dev/null @@ -1,31 +0,0 @@ -{% comment %} -Analytics tags. Override for different IDs -{% endcomment %} - -{% if EREGS_GTM_SITE_ID != '' %} - - - - -{% endif %} -{% if EREGS_GA_SITE_ID != '' %} - -{% endif %} -{% if EREGS_DAP_AGENCY != '' %} - -{% endif %} diff --git a/regulations/templates/regulations/google_analytics.html b/regulations/templates/regulations/google_analytics.html index 2b6f2022..13e856ad 100644 --- a/regulations/templates/regulations/google_analytics.html +++ b/regulations/templates/regulations/google_analytics.html @@ -4,7 +4,7 @@ {% if ANALYTICS.GOOGLE.GTM_SITE_ID %} - -{% endif %} \ No newline at end of file +{% endif %} From 943bc4151ba3986cf66a578c50ff850ecc57b6f0 Mon Sep 17 00:00:00 2001 From: jbarnicle Date: Mon, 23 Nov 2015 12:33:16 -0600 Subject: [PATCH 10/10] revised unit test for namespaced analytics --- regulations/tests/views_utils_test.py | 73 ++++++++------------------- 1 file changed, 20 insertions(+), 53 deletions(-) diff --git a/regulations/tests/views_utils_test.py b/regulations/tests/views_utils_test.py index 2f2a4da7..70474da1 100644 --- a/regulations/tests/views_utils_test.py +++ b/regulations/tests/views_utils_test.py @@ -9,45 +9,15 @@ class UtilsTest(TestCase): def setUp(self): - if hasattr(settings, 'GOOGLE_ANALYTICS_ID'): - self.old_gai = settings.GOOGLE_ANALYTICS_ID - if hasattr(settings, 'GOOGLE_ANALYTICS_SITE'): - self.old_gas = settings.GOOGLE_ANALYTICS_SITE - if hasattr(settings, 'EREGS_GA'): - self.eregs_gai = settings.EREGS_GA['EREGS']['ID'] - self.eregs_gas = settings.EREGS_GA['EREGS']['SITE'] - self.cfgov_gai = settings.EREGS_GA['ALT']['ID'] - self.cfgov_gas = settings.EREGS_GA['ALT']['SITE'] + + if hasattr(settings, 'ANALYTICS'): + self.old_analytics = settings.ANALYTICS if hasattr(settings, 'JS_DEBUG'): self.old_js_debug = settings.JS_DEBUG def tearDown(self): - if hasattr(self, 'old_gai'): - settings.GOOGLE_ANALYTICS_ID = self.old_gai - elif hasattr(settings, 'GOOGLE_ANALYTICS_ID'): - del(settings.GOOGLE_ANALYTICS_ID) - if hasattr(self, 'old_gas'): - settings.GOOGLE_ANALYTICS_SITE = self.old_gas - elif hasattr(settings, 'GOOGLE_ANALYTICS_SITE'): - del(settings.GOOGLE_ANALYTICS_SITE) - - if hasattr(self, 'eregs_gai'): - settings.EREGS_GA_EREGS_ID = self.eregs_gai - elif hasattr(settings, 'EREGS_GA_EREGS_ID'): - del(settings.EREGS_GA_EREGS_ID) - if hasattr(self, 'eregs_gas'): - settings.EREGS_GA_EREGS_SITE = self.eregs_gas - elif hasattr(settings, 'EREGS_GA_EREGS_SITE'): - del(settings.EREGS_GA_EREGS_SITE) - - if hasattr(self, 'cfgov_gai'): - settings.EREGS_GA_ALT_ID = self.cfgov_gai - elif hasattr(settings, 'EREGS_GA_ALT_ID'): - del(settings.EREGS_GA_ALT_ID) - if hasattr(self, 'cfgov_gas'): - settings.EREGS_GA_ALT_SITE = self.cfgov_gas - elif hasattr(settings, 'EREGS_GA_ALT_SITE'): - del(settings.EREGS_GA_ALT_SITE) + if hasattr(self, 'old_analytics'): + settings.ANALYTICS = self.old_analytics if hasattr(self, 'old_js_debug'): settings.JS_DEBUG = self.old_js_debug @@ -82,30 +52,27 @@ def test_add_extras_env(self): def test_add_extras(self): context = {} - settings.EREGS_GA = { - 'EREGS': {'ID': 'eregs-ga-id', 'SITE': 'eregs'}, - 'ALT': {'ID': 'alt-ga-id', 'SITE': 'alt'} + settings.ANALYTICS = { + 'GOOGLE': { + 'GTM_SITE_ID': 'gtm-site-id', + 'GA_SITE_ID': 'ga-site-id', + }, + 'DAP': { + 'AGENCY': 'agency', + 'SUBAGENCY': 'sub-agency', + } } + add_extras(context) self.assertTrue('APP_PREFIX' in context) self.assertTrue('env' in context) - self.assertEquals('eregs-ga-id', context['EREGS_GA_EREGS_ID']) - self.assertEquals('eregs', context['EREGS_GA_EREGS_SITE']) - self.assertEquals('alt-ga-id', context['EREGS_GA_ALT_ID']) - self.assertEquals('alt', context['EREGS_GA_ALT_SITE']) - - def test_add_extras_gai(self): - """Make sure we are backwards compatible with GOOGLE_* params""" - settings.GOOGLE_ANALYTICS_ID = 'googid' - settings.GOOGLE_ANALYTICS_SITE = 'googsite' - del(settings.EREGS_GA) - - context = {} - add_extras(context) - self.assertEqual('googid', context['EREGS_GA_EREGS_ID']) - self.assertEqual('googsite', context['EREGS_GA_EREGS_SITE']) + self.assertEquals('gtm-site-id', context['ANALYTICS']['GOOGLE']['GTM_SITE_ID']) + self.assertEquals('ga-site-id', context['ANALYTICS']['GOOGLE']['GA_SITE_ID']) + self.assertEquals('agency', context['ANALYTICS']['DAP']['AGENCY']) + self.assertEquals('sub-agency', context['ANALYTICS']['DAP']['SUBAGENCY']) + self.assertEquals('agency=agency&subagency=sub-agency', context['ANALYTICS']['DAP']['DAP_URL_PARAMS'] ) @patch('regulations.views.utils.fetch_toc') def test_first_section(self, fetch_toc):