diff --git a/analytical/templatetags/google_analytics.py b/analytical/templatetags/google_analytics.py index 5e57a0b..755f7b8 100644 --- a/analytical/templatetags/google_analytics.py +++ b/analytical/templatetags/google_analytics.py @@ -38,7 +38,7 @@ SETUP_CODE = """ %(commands)s (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; - ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; + ga.src = ('https:' == document.location.protocol ? %(display_advertising)s; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); @@ -51,7 +51,8 @@ CUSTOM_VAR_CODE = "_gaq.push(['_setCustomVar', %(index)s, '%(name)s', " \ "'%(value)s', %(scope)s]);" SITE_SPEED_CODE = "_gaq.push(['_trackPageLoadTime']);" ANONYMIZE_IP_CODE = "_gaq.push (['_gat._anonymizeIp']);" - +WITHOUT_DISPLAY_ADVERTISING = "'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'" +WITH_DISPLAY_ADVERTISING = "'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'" register = Library() @@ -79,8 +80,13 @@ class GoogleAnalyticsNode(Node): commands = self._get_domain_commands(context) commands.extend(self._get_custom_var_commands(context)) commands.extend(self._get_other_commands(context)) + if getattr(settings, 'GOOGLE_ANALYTICS_DISPLAY_ADVERTISING', False): + display_advertising = WITH_DISPLAY_ADVERTISING + else: + display_advertising = WITHOUT_DISPLAY_ADVERTISING html = SETUP_CODE % {'property_id': self.property_id, - 'commands': " ".join(commands)} + 'commands': " ".join(commands), + 'display_advertising': display_advertising} if is_internal_ip(context, 'GOOGLE_ANALYTICS'): html = disable_html(html, 'Google Analytics') return html diff --git a/analytical/tests/test_tag_google_analytics.py b/analytical/tests/test_tag_google_analytics.py index 3b858e6..17bf09b 100644 --- a/analytical/tests/test_tag_google_analytics.py +++ b/analytical/tests/test_tag_google_analytics.py @@ -75,7 +75,15 @@ class GoogleAnalyticsTagTestCase(TagTestCase): def test_track_page_load_time(self): r = GoogleAnalyticsNode().render(Context()) self.assertTrue("_gaq.push(['_trackPageLoadTime']);" in r, r) - + + def test_display_advertising(self): + with override_settings(GOOGLE_ANALYTICS_DISPLAY_ADVERTISING=False): + r = GoogleAnalyticsNode().render(Context()) + self.assertTrue("google-analytics.com/ga.js" in r, r) + with override_settings(GOOGLE_ANALYTICS_DISPLAY_ADVERTISING=True): + r = GoogleAnalyticsNode().render(Context()) + self.assertTrue("stats.g.doubleclick.net/dc.js" in r, r) + @override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1']) def test_render_internal_ip(self): req = HttpRequest() @@ -105,4 +113,4 @@ class NoDomainTestCase(TestCase): def test_exception_without_domain(self): context = Context() self.assertRaises(AnalyticalException, GoogleAnalyticsNode().render, - context) \ No newline at end of file + context) diff --git a/docs/services/google_analytics.rst b/docs/services/google_analytics.rst index 7136ee4..615aece 100644 --- a/docs/services/google_analytics.rst +++ b/docs/services/google_analytics.rst @@ -98,6 +98,21 @@ either pass the domain to the template tag through the context variable or set it in the project :file:`settings.py` file using :const:`GOOGLE_ANALYTICS_DOMAIN` (fallback: :const:`ANALYTICAL_DOMAIN`). +Display Advertising +------------------- + +Display Advertising allows you to view Demographics and Interests reports, +add Remarketing Lists and support DoubleClick Campain Manager integration. + +You can enable `Display Advertising features`_ by setting the +:const:`GOOGLE_ANALYTICS_DISPLAY_ADVERTISING` configuration setting:: + + GOOGLE_ANALYTICS_DISPLAY_ADVERTISING = True + +By default, display advertising features are disabled. + +.. _`Display Advertising features`: https://support.google.com/analytics/answer/3450482 + Tracking site speed ------------------- @@ -189,4 +204,4 @@ concerning data privacy (e.g. Germany). By default, IPs are not anonymized. -.. _`IP anonymization`: https://support.google.com/analytics/bin/answer.py?hl=en&answer=2763052 \ No newline at end of file +.. _`IP anonymization`: https://support.google.com/analytics/bin/answer.py?hl=en&answer=2763052