add support for GA Display Advertising features

https://support.google.com/analytics/answer/3450482

The Google Analytics Display Advertising features include the following:
- Demographics and Interests reporting
- Remarketing with Google Analytics
- DoubleClick Campaign Manager integration (for Google Analytics Premium)
This commit is contained in:
Max Arnold 2013-10-26 10:17:38 +07:00
parent 11719a99f6
commit 440d078d86
3 changed files with 35 additions and 6 deletions

View file

@ -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

View file

@ -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)
context)

View file

@ -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
.. _`IP anonymization`: https://support.google.com/analytics/bin/answer.py?hl=en&answer=2763052