Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Metrics API test #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4926f78
Create automate-metrics.yaml
jukent Mar 11, 2024
7b0acfa
outline for automate-metrics.yaml
jukent Mar 11, 2024
b8511a2
Create get-metrics.py
jukent Mar 11, 2024
a9ed412
Update automate-metrics.yaml
jukent Mar 11, 2024
37cd664
Update automate-metrics.yaml
jukent Mar 11, 2024
aa0463b
Update footer-menu.html with metrics
jukent Mar 11, 2024
8b91ca4
Create metrics.md
jukent Mar 11, 2024
9f664fa
add json_extract to extensions
jukent Mar 11, 2024
2dafc21
Update get-metrics.py to also write file
jukent Mar 11, 2024
47fad36
Update get-metrics.py with returned condition
jukent Mar 11, 2024
9a4a657
Update automate-metrics.yaml
jukent Mar 11, 2024
f6dbf6d
pre-commit
jukent Mar 11, 2024
238edf2
use os to access passed in keys
jukent Mar 11, 2024
314b93d
fix double quoted strings
jukent Mar 11, 2024
ce7f60e
add jsonextract to environment.yml
jukent Mar 11, 2024
49ba11b
write metrics.md in a python file
jukent Mar 11, 2024
53c472c
rm from config
jukent Mar 11, 2024
99143ec
use env files
jukent Mar 11, 2024
7da6027
update comment
jukent Mar 11, 2024
e15d349
state with a table
jukent Mar 11, 2024
1d7c268
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 11, 2024
6e55f19
rm table because it won't format and add to toc tree
jukent Mar 11, 2024
73ad6f1
Merge branch 'metrics-api' of https://github.com/jukent/projectpythia…
jukent Mar 11, 2024
99f8acf
add dispatch
jukent Mar 11, 2024
ac5d69e
starting metrics file, and - vs _
jukent Mar 11, 2024
2345bb0
pre-commit
jukent Mar 11, 2024
fc92d94
fix write-metrics
jukent Mar 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/automate-metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Update User Metrics

on:
workflow_dispatch:
schedule:
# Weekly Every Monday at 1PM UTC
- cron: '0 13 * * 1'

env:
portal_id: ${{ secrets.GA4_PORTAL_ID }}
foundations_id: ${{ secrets.GA4_FOUNDATIONS_ID }}
cookbooks_id: ${{ secrets.GA4_COOKBOOKS_ID }}

jobs:
main:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Set Credentials
uses: mobiledevops/secret-to-file-action@v1
with:
base64-encoded-secret: ${{ secrets.GOOGLE_ANALYTICS_API_CREDENTIALS }}
filename: "credentials.json"
is-executable: true
working-directory: "."
- run: export GOOGLE_APPLICATION_CREDENTIALS="credentials.json"

- name: Set-Up Virtual Environment
run: |
python -m venv analytics-api
source analytics-api/bin/activate
pip install google-analytics-data

- name: Get and Write Metrics to JSON
run: python get-metrics.py

- name: Write Markdown File
run: python write-metrics-md.py

- name: Push Changes
if: steps.Get-and-Write-Metrics.outputs == 1 # Conditional step execution
run: |
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git add user_metrics.json
git add ../portal/metrics.md
git commit -m "Update user metrics"
git push
85 changes: 85 additions & 0 deletions .github/workflows/get-metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import json
import os

from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import DateRange, Metric, RunReportRequest

PORTAL_ID = os.environ['portal_id']
FOUNDATIONS_ID = os.environ['foundations_id']
COOKBOOKS_ID = os.environ['cookbook_id']


def _run_total_users_report(property_id):
"""Fetches total users for a given property ID

Args:
property_id: The Google Analytics 4 property ID

Returns:
int: The total number of active users
"""

client = BetaAnalyticsDataClient()

request = RunReportRequest(
property=f'properties/{property_id}',
dimensions=[],
metrics=[Metric(name='activeUsers')],
date_ranges=[DateRange(start_date='2020-03-31', end_date='today')],
)

response = client.run_report(request)

total_users = 0
for row in response.rows:
total_users += int(row.metric_values[0].value)

return total_users


def get_metrics(portal_id, foundations_id, cookbooks_id):
"""Retrieves total users for specified GA4 properties and writes to file if changes are significant."""

metrics_dict = {}
metrics_dict['Portal'] = _run_total_users_report(str(portal_id))
metrics_dict['Foundations'] = _run_total_users_report(str(foundations_id))
metrics_dict['Cookbooks'] = _run_total_users_report(str(cookbooks_id))

return metrics_dict # Return the metrics dictionary


def write_metrics(metrics_dict):
"""Reads existing metrics, compares for significant change, and writes to file if necessary."""

# Read existing user counts (handle potential file absence)
try:
with open('user_metrics.json') as f:
user_data = json.load(f)
except FileNotFoundError:
user_data = {}

# Define a threshold for significant change (adjust as needed)
threshold = 100
has_significant_change = False
for property, user_count in metrics_dict.items():
existing_count = user_data.get(property, 0)
if abs(existing_count - user_count) > threshold:
user_data[property] = user_count
has_significant_change = True

# Write to file if significant change detected
if has_significant_change:
with open('user_metrics.json', 'w') as outfile:
json.dump(metrics_dict, outfile)
return 1 # Signals significant change
else:
return 0 # Signals no significant change


if __name__ == '__main__':
metrics = get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID)
exit_code = write_metrics(metrics)
if exit_code == 1:
print('Significant change detected in user metrics.')
else:
print('No significant change in user metrics.')
1 change: 1 addition & 0 deletions .github/workflows/user_metrics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Portal": 0, "Foundations": 0, "Cookbooks": 0}
33 changes: 33 additions & 0 deletions .github/workflows/write-metrics-md.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json


def process_user_data(user_data_file, markdown_file):
"""
Reads user data from a JSON file and writes it to a markdown file.

Args:
user_data_file: Path to the JSON file containing user data.
markdown_file: Path to the output markdown file.
"""

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

# table_header = '| Portal | Foundations | Cookbooks |\n'
# table_row = f"| {' | '.join([str(user_data[key]) for key in user_data])} |\n"
# table = table_header + table_row

# Write processed data to markdown file
with open(markdown_file, 'w') as f:
f.write('# Metrics \n\n')
f.write('Total Users:\n\n')
for key in user_data:
f.write(f'{key}: {user_data[key]}\n')
f.close()


if __name__ == '__main__':
user_data_file = 'user_metrics.json'
markdown_file = '../../portal/metrics.md'
process_user_data(user_data_file, markdown_file)
print('User data report generated: ', markdown_file)
1 change: 1 addition & 0 deletions portal/_templates/footer-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ <h1>{{ _("About") }}</h1>
<li><a href="https://projectpythia.org/about.html">{{ _("About Project Pythia") }}</a></li>
<li><a href="https://foundations.projectpythia.org/preamble/how-to-use.html">{{ _("How to use Pythia Foundations") }}</a></li>
<li><a href="https://projectpythia.org/#how-to-cite">{{ _("How to Cite") }}</a></li>
<li><a href="https://projectpythia.org/metrics.html">{{ _("Metrics") }}</a></li>
</ul>
</div>
<div class="col-9 col-sm-4 col-md-4 col-lg-3 footer-menu-col">
Expand Down
1 change: 1 addition & 0 deletions portal/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@ contributing.md
code_of_conduct.md
cookbook-gallery.md
resource-gallery.md
metrics.md
```
7 changes: 7 additions & 0 deletions portal/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Metrics

Total Users:

Portal: 0
Foundations: 0
Cookbooks: 0