Method which gets a chunk of HTML to perform standard Google Analytics tracking as well as a noscript version. Very useful for finding out what percentage of your users do not have JavaScript support.
This is Python port of the PHP example here: http://andrescholten.net/google-analytics-zonder-javascript/
To use, you need to put the following into your settings file:
GOOGLE_ANALYTICS_ID = 'UA-1234567-1' GOOGLE_ANALYTICS_DOMAIN = 'example.com'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import random
import time
import settings
def get_google_analytics_tracking(request):
"""Gets a chunk of HTML which will track Google Analytics using the current
version of the JavaScript, alongside a noscript version."""
web_property_id = getattr(settings, 'GOOGLE_ANALYTICS_ID', False)
domain = getattr(settings, 'GOOGLE_ANALYTICS_DOMAIN', False)
if not web_property_id or not domain:
return ('<!-- Set GOOGLE_ANALYTICS_ID and GOOGLE_ANALYTICS_DOMAIN to '
'include Google Analytics tracking.')
if getattr(settings, 'DEBUG', True):
return ('<!-- Set DEBUG to False to include Google Analytics tracking.')
if (request.META.has_key('HTTP_REFERER') and
request.META['HTTP_REFERER'] != ''):
http_referer = request.META['HTTP_REFERER']
else:
http_referer = '-'
vars = {
'id': web_property_id,
'domain': domain,
'utmn': random.randint(1000000000, 9999999999),
'cookie': random.randint(10000000, 99999999),
'random': random.randint(1000000000, 2147483647),
'today': str(int(time.time())),
'referer': http_referer,
'uservar': '-',
'utmp': '/nojs' + request.path,
}
script_section = ('<script type="text/javascript">var _gaq = _gaq || []; '
'_gaq.push([\'_setAccount\', \'%(id)s\']); '
'_gaq.push([\'_setDomainName\', \'.%(domain)s\']); '
'_gaq.push([\'_trackPageview\']); '
'(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\'; '
'var s = document.getElementsByTagName(\'script\')[0]; '
's.parentNode.insertBefore(ga, s); })(); </script>') % vars
noscript_section = ('<noscript>'
'<img src="http://www.google-analytics.com/__utm.gif?utmwv=3&utmn='
'%(utmn)s&utme=&utmcs=-&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-'
'&utmhn=%(domain)s&utmhid=%(utmn)s&utmr=%(referer)s&utmp=%(utmp)s'
'&utmac=%(id)s&utmcc=__utma%%3D%(cookie)s.%(random)s.%(today)s.'
'%(today)s.%(today)s.2%%3B%%2B__utmz%%3D%(cookie)s.%(today)s.2.2.'
'utmcsr%%3D_SOURCE_%%7Cutmccn%%3D_CAMPAIGN_%%7Cutmcmd%%3D_MEDIUM_%%7'
'Cutmctr%%3D_KEYWORD_%%7Cutmcct%%3D_CONTENT_%%3B%%2B__utmv%%3D'
'%(cookie)s.%(uservar)s%%3B;" border="0" /></noscript>') % vars
return script_section + noscript_section
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.