-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtag_fix.py
executable file
·55 lines (38 loc) · 1.19 KB
/
gtag_fix.py
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
#!/usr/bin/env python2.7
import os
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
TAG = """
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-40344111-29"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-40344111-29');
</script>
"""
# def find_all(name, path):
# result = []
# for root, dirs, files in os.walk(path):
# if name in files:
# result.append(os.path.join(root, name))
# return result
indices = os.popen('find %s -name index.html' % (SCRIPT_DIR)).readlines()
indices = [f.strip() for f in indices]
for fname in indices:
f = open(fname)
lines = f.readlines()
f.close()
needs_analytics = True
style_end_tag_line = -1
for i, l in enumerate(lines):
if "www.googletagmanager.com" in l:
needs_analytics = False
elif "</style>" in l:
style_end_tag_line = i
if needs_analytics:
print("tagging %s" % (fname))
lines.insert(style_end_tag_line + 1, TAG)
f = open(fname, 'w')
f.write("".join(lines))
f.close()