-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix_googleanalytics.py
More file actions
executable file
·83 lines (75 loc) · 3.04 KB
/
fix_googleanalytics.py
File metadata and controls
executable file
·83 lines (75 loc) · 3.04 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# old version:
# old_uid = 'UA-6253200-1'
# new_uid = 'UA-37306139-2'
#
# tracking_script = """<script type="text/javascript">
#
# var _gaq = _gaq || [];
# _gaq.push(['_setAccount', 'UA-37306139-2']);
# _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>"""
#
#import bs4
#import os
#
#for rootdir,dirname,flist in os.walk('.'):
# for fn in flist:
# if "htm" == fn[-3:] or 'html' == fn[-4:]:
# fullfn = os.path.join(rootdir,fn)
# with open(fullfn,'r') as f:
# data = f.read()
# if old_uid in data:
# data = data.replace(old_uid,new_uid)
# with open(fullfn,'w') as outf:
# outf.write(data)
# print "Fixed ",fullfn
# elif new_uid in data:
# print "Already fixed: ",fullfn
# else:
# print "No UA in ",fullfn
# if "</html>" in data:
# data = data.replace("</html>",tracking_script+"\n</html>")
# with open(fullfn,'w') as outf:
# outf.write(data)
import bs4
import os
# <script type="text/javascript" src='googleanalytics.js'></script>
for rootdir,dirname,flist in os.walk('.'):
for fn in flist:
if "htm" == fn[-3:] or 'html' == fn[-4:]:
fullfn = os.path.join(rootdir,fn)
with open(fullfn,'r') as f:
data = f.read()
changed = False
b = bs4.BeautifulSoup(data)
reldir = os.path.relpath('.',rootdir)
analytics = b.new_tag(name='script',
type='text/javascript',
src='{}/googleanalytics.js'.format(reldir))
scripts = b.findAll('script')
tracker = [s for s in scripts
if (s.string is not None and
('_getTracker' in s.string or
"_gaq.push(['_trackPageview']);" in s.string))]
if tracker:
for t in tracker:
t.insert_before(analytics)
t.extract()
print "Fixing script for ",fullfn, analytics
changed = True
elif not any('googleanalytics.js' in s.attrs['src'] for s in scripts if 'src' in s.attrs):
b.html.append(analytics)
print "Appending script to ",fullfn, analytics
changed = True
else:
print "No need to fix ",fullfn
if changed:
with open(fullfn,'w') as f:
f.write(str(b))