Skip to content

Commit e3439f5

Browse files
Metrics api use curl (#410)
* Create automate-metrics.yaml * outline for automate-metrics.yaml * Create get-metrics.py * Update automate-metrics.yaml * Update automate-metrics.yaml * Update footer-menu.html with metrics * Create metrics.md * add json_extract to extensions * Update get-metrics.py to also write file * Update get-metrics.py with returned condition * Update automate-metrics.yaml * pre-commit * use os to access passed in keys * fix double quoted strings * add jsonextract to environment.yml * write metrics.md in a python file * rm from config * use env files * update comment * state with a table * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * rm table because it won't format and add to toc tree * add dispatch * starting metrics file, and - vs _ * pre-commit * fix write-metrics * make a metrics folder for holding future graphs * missed a path herr * add metrics automation as a step before site building * rm automate-metrics schedule * Add last updated time to write-metrics * pre-commit * use curl * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5ac1362 commit e3439f5

7 files changed

+26
-70
lines changed

.github/workflows/automate-metrics.yaml

+3-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ name: Update User Metrics
22

33
on:
44
workflow_dispatch:
5-
schedule:
6-
# Weekly Every Monday at 1PM UTC
7-
- cron: '0 13 * * 1'
85

96
env:
107
portal_id: ${{ secrets.GA4_PORTAL_ID }}
@@ -32,17 +29,9 @@ jobs:
3229
pip install google-analytics-data
3330
3431
- name: Get and Write Metrics to JSON
35-
run: python get-metrics.py
32+
run: |
33+
curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/metrics-api/.github/workflows/get-metrics.py
34+
python get_metrics.py
3635
3736
- name: Write Markdown File
3837
run: python write-metrics-md.py
39-
40-
- name: Push Changes
41-
if: steps.Get-and-Write-Metrics.outputs == 1 # Conditional step execution
42-
run: |
43-
git config --global user.name "${{ github.actor }}"
44-
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
45-
git add user_metrics.json
46-
git add ../portal/metrics.md
47-
git commit -m "Update user metrics"
48-
git push

.github/workflows/get-metrics.py

+3-45
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010

1111

1212
def _run_total_users_report(property_id):
13-
"""Fetches total users for a given property ID
14-
15-
Args:
16-
property_id: The Google Analytics 4 property ID
17-
18-
Returns:
19-
int: The total number of active users
20-
"""
2113

2214
client = BetaAnalyticsDataClient()
2315

@@ -38,48 +30,14 @@ def _run_total_users_report(property_id):
3830

3931

4032
def get_metrics(portal_id, foundations_id, cookbooks_id):
41-
"""Retrieves total users for specified GA4 properties and writes to file if changes are significant."""
42-
4333
metrics_dict = {}
4434
metrics_dict['Portal'] = _run_total_users_report(str(portal_id))
4535
metrics_dict['Foundations'] = _run_total_users_report(str(foundations_id))
4636
metrics_dict['Cookbooks'] = _run_total_users_report(str(cookbooks_id))
4737

48-
return metrics_dict # Return the metrics dictionary
49-
50-
51-
def write_metrics(metrics_dict):
52-
"""Reads existing metrics, compares for significant change, and writes to file if necessary."""
53-
54-
# Read existing user counts (handle potential file absence)
55-
try:
56-
with open('user_metrics.json') as f:
57-
user_data = json.load(f)
58-
except FileNotFoundError:
59-
user_data = {}
60-
61-
# Define a threshold for significant change (adjust as needed)
62-
threshold = 100
63-
has_significant_change = False
64-
for property, user_count in metrics_dict.items():
65-
existing_count = user_data.get(property, 0)
66-
if abs(existing_count - user_count) > threshold:
67-
user_data[property] = user_count
68-
has_significant_change = True
69-
70-
# Write to file if significant change detected
71-
if has_significant_change:
72-
with open('user_metrics.json', 'w') as outfile:
73-
json.dump(metrics_dict, outfile)
74-
return 1 # Signals significant change
75-
else:
76-
return 0 # Signals no significant change
38+
with open('user_metrics.json', 'w') as outfile:
39+
json.dump(metrics_dict, outfile)
7740

7841

7942
if __name__ == '__main__':
80-
metrics = get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID)
81-
exit_code = write_metrics(metrics)
82-
if exit_code == 1:
83-
print('Significant change detected in user metrics.')
84-
else:
85-
print('No significant change in user metrics.')
43+
get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID)

.github/workflows/nightly-build.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
- cron: '0 0 * * *' # Daily “At 00:00”
77

88
jobs:
9+
update-metrics:
10+
uses: automate-metrics.yaml@main
11+
912
build:
1013
if: ${{ github.repository_owner == 'ProjectPythia' }}
1114
uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main
@@ -20,3 +23,10 @@ jobs:
2023
uses: ./.github/workflows/sphinx-link-checker.yaml
2124
with:
2225
path_to_source: 'portal'
26+
27+
deploy:
28+
needs: build
29+
uses: ProjectPythia/cookbook-actions/.github/workflows/deploy-book.yaml@main
30+
with:
31+
cname: projectpythia.org
32+
publish_dir: 'portal/_build/html'

.github/workflows/publish-site.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11+
update-metrics:
12+
uses: automate-metrics.yaml@main
13+
1114
build:
1215
uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main
1316
with:

.github/workflows/trigger-site-build.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ on:
33
pull_request:
44

55
jobs:
6+
update-metrics:
7+
uses: automate-metrics.yaml@main
8+
69
build:
710
uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main
811
with:

.github/workflows/write-metrics-md.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import json
23

34

@@ -9,25 +10,23 @@ def process_user_data(user_data_file, markdown_file):
910
user_data_file: Path to the JSON file containing user data.
1011
markdown_file: Path to the output markdown file.
1112
"""
13+
now = datetime.datetime.now()
1214

1315
with open(user_data_file, 'r') as f:
1416
user_data = json.load(f)
1517

16-
# table_header = '| Portal | Foundations | Cookbooks |\n'
17-
# table_row = f"| {' | '.join([str(user_data[key]) for key in user_data])} |\n"
18-
# table = table_header + table_row
19-
2018
# Write processed data to markdown file
2119
with open(markdown_file, 'w') as f:
2220
f.write('# Metrics \n\n')
2321
f.write('Total Users:\n\n')
2422
for key in user_data:
2523
f.write(f'{key}: {user_data[key]}\n')
24+
f.write('\n')
25+
f.write(f'Last Updated: {now}\n')
2626
f.close()
2727

2828

2929
if __name__ == '__main__':
3030
user_data_file = 'user_metrics.json'
3131
markdown_file = '../../portal/metrics.md'
3232
process_user_data(user_data_file, markdown_file)
33-
print('User data report generated: ', markdown_file)

portal/metrics.md

-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
11
# Metrics
2-
3-
Total Users:
4-
5-
Portal: 0
6-
Foundations: 0
7-
Cookbooks: 0

0 commit comments

Comments
 (0)