Make sure Google Analytics calls like _anonymizeIp settings happen before calling _trackPageview

This commit is contained in:
Diederik van der Boor 2018-05-17 15:36:39 +02:00 committed by Joost Cassee
parent 32c45f2f0c
commit d2c782c1d5
2 changed files with 3 additions and 1 deletions

View file

@ -34,7 +34,6 @@ SETUP_CODE = """
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%(property_id)s']);
_gaq.push(['_trackPageview']);
%(commands)s
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
@ -46,6 +45,7 @@ SETUP_CODE = """
"""
DOMAIN_CODE = "_gaq.push(['_setDomainName', '%s']);"
NO_ALLOW_HASH_CODE = "_gaq.push(['_setAllowHash', false]);"
TRACK_PAGE_VIEW = "_gaq.push(['_trackPageview']);"
ALLOW_LINKER_CODE = "_gaq.push(['_setAllowLinker', true]);"
CUSTOM_VAR_CODE = "_gaq.push(['_setCustomVar', %(index)s, '%(name)s', " \
"'%(value)s', %(scope)s]);"
@ -89,6 +89,7 @@ class GoogleAnalyticsNode(Node):
commands = self._get_domain_commands(context)
commands.extend(self._get_custom_var_commands(context))
commands.extend(self._get_other_commands(context))
commands.append(TRACK_PAGE_VIEW)
if getattr(settings, 'GOOGLE_ANALYTICS_DISPLAY_ADVERTISING', False):
source = DISPLAY_ADVERTISING_SOURCE
else:

View file

@ -93,6 +93,7 @@ class GoogleAnalyticsTagTestCase(TagTestCase):
def test_anonymize_ip(self):
r = GoogleAnalyticsNode().render(Context())
self.assertTrue("_gaq.push (['_gat._anonymizeIp']);" in r, r)
self.assertTrue(r.index('_gat._anonymizeIp') < r.index('_trackPageview'), r)
@override_settings(GOOGLE_ANALYTICS_ANONYMIZE_IP=False)
def test_anonymize_ip_not_present(self):