Merge pull request #22 from tinnet/feature/ga-anonymize-ip

Add 'IP Anonymization' setting to GA tracking pixel
This commit is contained in:
Joost Cassee 2013-03-24 16:34:18 -07:00
commit cb71b7ca0a
3 changed files with 30 additions and 0 deletions

View file

@ -50,6 +50,7 @@ ALLOW_LINKER_CODE = "_gaq.push(['_setAllowLinker', true]);"
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']);"
register = Library()
@ -120,6 +121,8 @@ class GoogleAnalyticsNode(Node):
commands = []
if getattr(settings, 'GOOGLE_ANALYTICS_SITE_SPEED', False):
commands.append(SITE_SPEED_CODE)
if getattr(settings, 'GOOGLE_ANALYTICS_ANONYMIZE_IP', False):
commands.append(ANONYMIZE_IP_CODE)
return commands
def contribute_to_analytical(add_node):

View file

@ -86,6 +86,15 @@ class GoogleAnalyticsTagTestCase(TagTestCase):
'<!-- Google Analytics disabled on internal IP address'), r)
self.assertTrue(r.endswith('-->'), r)
@override_settings(GOOGLE_ANALYTICS_ANONYMIZE_IP=True)
def test_anonymize_ip(self):
r = GoogleAnalyticsNode().render(Context())
self.assertTrue("_gaq.push (['_gat._anonymizeIp']);" in r, r)
@override_settings(GOOGLE_ANALYTICS_ANONYMIZE_IP=False)
def test_anonymize_ip_not_present(self):
r = GoogleAnalyticsNode().render(Context())
self.assertFalse("_gaq.push (['_gat._anonymizeIp']);" in r, r)
@without_apps('django.contrib.sites')
@override_settings(GOOGLE_ANALYTICS_PROPERTY_ID='UA-123456-7',

View file

@ -172,3 +172,21 @@ Just remember that if you set the same context variable in the
context processor, the latter clobbers the former.
.. _`custom variables`: http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html
.. _google-analytics-anonimyze-ips:
Anonymize IPs
----------------
You can enable the `IP anonymization`_ feature by setting the
:const:`GOOGLE_ANALYTICS_ANONYMIZE_IP` configuration setting::
GOOGLE_ANALYTICS_ANONYMIZE_IP = True
This may be mandatory for deployments in countries that have a firm policies
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