forked from statmike/vertex-ai-mlops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga4_remove.py
54 lines (43 loc) · 2.24 KB
/
ga4_remove.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
50
51
52
53
54
# this file, ga4_remove.py, will remove the tracking from files in the repository
# to see files this will impact first run ga4_list.py
import os
import json
import urllib.parse
# this file is in: /vertex-ai-mlops/architectures/tracking/setup/ga4/ga4_remove.py
for root, dirs, files in os.walk('../../../../.'):
for file in files:
if file.endswith(('.md', '.ipynb')) and not root.endswith('.ipynb_checkpoints'):
print('evaluating: ', os.path.join(root, file))
if file.endswith('.md'):
update = False
# read file contents
with open(os.path.join(root, file), 'r') as reader:
content = reader.readlines()
# check for ga4
if content[0].startswith('![ga4](https://www.google-analytics.com'):
content = content[1:]
update = True
# update file if ga4 present
if update:
print('make change here: ', file)
with open(os.path.join(root, file), 'w') as writer:
writer.writelines(content)
elif file.endswith('.ipynb'):
update = False
# read file contents
with open(os.path.join(root, file), 'r') as reader:
content = json.loads(reader.read())
# check for ga4
for cell in content['cells']:
if cell['cell_type'] == 'markdown':
if cell['source'][0].startswith('![ga4](https://www.google-analytics.com'):
cell['source'] = cell['source'][1:]
update = True
# only review the first markdown cell the break for loop
break
# update file if ga4 present
if update:
print('make change here: ', file)
with open(os.path.join(root, file), 'w') as writer:
writer.write(json.dumps(content))
print('done with evaluation.')