-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject_ga4.py
43 lines (36 loc) · 1.1 KB
/
inject_ga4.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
import os
import sys
tracking_id = os.getenv('INPUT_TRACKING_ID')
file = os.getenv('INPUT_FILE')
print(f"Injecting GA4 code for {tracking_id} into {file}")
if not tracking_id:
print('GA4 Tracking ID not provided. Exiting script.')
sys.exit(1)
tracking_code = f"""
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={tracking_id}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{dataLayer.push(arguments);}}
gtag('js', new Date());
gtag('config', '{tracking_id}');
</script>
<!-- End Google Analytics -->
"""
# check if INPUT_FILE exists
if not file:
print('File not provided. Exiting script.')
if not os.path.exists(file):
print(f'File {file} not found. Exiting script.')
sys.exit(1)
with open(file, 'r+') as f:
content = f.read()
if '</head>' in content:
content = content.replace('</head>', tracking_code + '\n</head>')
f.seek(0)
f.write(content)
f.truncate()
print(f'Tracking code injected into {file}')
else:
print(f'</head> tag not found in {file}')
sys.exit(0)