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
|
Comments
I'm not sure if it's the same. But there's an official php version. It's a mobile tag, used for light mobile sites that are commonly accessed by non javascript browsers.
It would be nice to have a python version of that code.
http://code.google.com/mobile/analytics/docs/web/
#