Skip to content

Commit 85169c1

Browse files
authored
Merge c000323 into f36f4b2
2 parents f36f4b2 + c000323 commit 85169c1

File tree

8 files changed

+50
-32
lines changed

8 files changed

+50
-32
lines changed
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
name: Update User Metrics
22

33
on:
4-
workflow_dispatch:
4+
workflow_call:
55

66
env:
7-
portal_id: ${{ secrets.GA4_PORTAL_ID }}
8-
foundations_id: ${{ secrets.GA4_FOUNDATIONS_ID }}
9-
cookbooks_id: ${{ secrets.GA4_COOKBOOKS_ID }}
7+
PORTAL_ID: ${{ secrets.PORTAL_ID }}
8+
FOUNDATIONS_ID: ${{ secrets.FOUNDATIONS_ID }}
9+
COOKBOOKS_ID: ${{ secrets.COOKBOOKS_ID }}
10+
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
11+
PRIVATE_KEY_ID: ${{ secrets.PRIVATE_KEY_ID }}
1012

1113
jobs:
1214
main:
1315
runs-on: macos-latest
1416
steps:
1517
- uses: actions/checkout@v3
16-
- name: Set Credentials
17-
uses: mobiledevops/secret-to-file-action@v1
18-
with:
19-
base64-encoded-secret: ${{ secrets.GOOGLE_ANALYTICS_API_CREDENTIALS }}
20-
filename: "credentials.json"
21-
is-executable: true
22-
working-directory: "."
23-
- run: export GOOGLE_APPLICATION_CREDENTIALS="credentials.json"
24-
2518
- name: Set-Up Virtual Environment
2619
run: |
2720
python -m venv analytics-api
2821
source analytics-api/bin/activate
2922
pip install google-analytics-data
3023
31-
- name: Get and Write Metrics to JSON
24+
- name: Write Metrics to JSON
3225
run: |
33-
curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/metrics-api/.github/workflows/get-metrics.py
34-
python get_metrics.py
26+
source analytics-api/bin/activate
27+
curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/get-metrics.py
28+
python get-metrics.py
3529
3630
- name: Write Markdown File
37-
run: python write-metrics-md.py
31+
run: |
32+
source analytics-api/bin/activate
33+
curl -O https://raw.githubusercontent.com/jukent/projectpythia.github.io/main/.github/workflows/write-metrics-md.py
34+
python write-metrics-md.py

.github/workflows/get-metrics.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
import json
22
import os
3+
import base64
4+
import hashlib
35

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

7-
PORTAL_ID = os.environ['portal_id']
8-
FOUNDATIONS_ID = os.environ['foundations_id']
9-
COOKBOOKS_ID = os.environ['cookbook_id']
9+
PORTAL_ID = os.environ.get('PORTAL_ID')
10+
FOUNDATIONS_ID = os.environ.get('FOUNDATIONS_ID')
11+
COOKBOOKS_ID = os.environ.get('COOKBOOKS_ID')
1012

13+
PRIVATE_KEY_ID = os.environ.get('PRIVATE_KEY_ID')
14+
PRIVATE_KEY = os.environ.get('PRIVATE_KEY').replace('$','\n')
1115

12-
def _run_total_users_report(property_id):
16+
credentials_dict = {
17+
"type": "service_account",
18+
"project_id": "cisl-vast-pythia",
19+
"private_key_id": PRIVATE_KEY_ID,
20+
"private_key": PRIVATE_KEY,
21+
"client_email": "[email protected]",
22+
"client_id": "113402578114110723940",
23+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
24+
"token_uri": "https://oauth2.googleapis.com/token",
25+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
26+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/pythia-metrics-api%40cisl-vast-pythia.iam.gserviceaccount.com",
27+
"universe_domain": "googleapis.com"
28+
}
29+
30+
client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict)
1331

14-
client = BetaAnalyticsDataClient()
1532

33+
def _run_total_users_report(property_id):
1634
request = RunReportRequest(
1735
property=f'properties/{property_id}',
1836
dimensions=[],
@@ -29,15 +47,15 @@ def _run_total_users_report(property_id):
2947
return total_users
3048

3149

32-
def get_metrics(portal_id, foundations_id, cookbooks_id):
50+
def get_metrics():
3351
metrics_dict = {}
34-
metrics_dict['Portal'] = _run_total_users_report(str(portal_id))
35-
metrics_dict['Foundations'] = _run_total_users_report(str(foundations_id))
36-
metrics_dict['Cookbooks'] = _run_total_users_report(str(cookbooks_id))
52+
metrics_dict['Portal'] = _run_total_users_report(str(PORTAL_ID))
53+
metrics_dict['Foundations'] = _run_total_users_report(str(FOUNDATIONS_ID))
54+
metrics_dict['Cookbooks'] = _run_total_users_report(str(COOKBOOKS_ID))
3755

3856
with open('user_metrics.json', 'w') as outfile:
3957
json.dump(metrics_dict, outfile)
4058

4159

4260
if __name__ == '__main__':
43-
get_metrics(PORTAL_ID, FOUNDATIONS_ID, COOKBOOKS_ID)
61+
get_metrics()

.github/workflows/nightly-build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
update-metrics:
10-
uses: automate-metrics.yaml@main
10+
uses: ./.github/workflows/automate-metrics.yaml
1111

1212
build:
1313
if: ${{ github.repository_owner == 'ProjectPythia' }}

.github/workflows/publish-site.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
jobs:
1111
update-metrics:
12-
uses: automate-metrics.yaml@main
12+
uses: ./.github/workflows/automate-metrics.yaml
1313

1414
build:
1515
uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main

.github/workflows/trigger-preview.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
- completed
99

1010
jobs:
11+
update-metrics:
12+
uses: ./.github/workflows/automate-metrics.yaml
13+
1114
find-pull-request:
1215
uses: ProjectPythia/cookbook-actions/.github/workflows/find-pull-request.yaml@main
1316
deploy-preview:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44

55
jobs:
66
update-metrics:
7-
uses: automate-metrics.yaml@main
7+
uses: ./.github/workflows/automate-metrics.yaml
88

99
build:
1010
uses: ProjectPythia/cookbook-actions/.github/workflows/build-book.yaml@main

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ def process_user_data(user_data_file, markdown_file):
2828

2929
if __name__ == '__main__':
3030
user_data_file = 'user_metrics.json'
31-
markdown_file = '../../portal/metrics.md'
31+
markdown_file = 'portal/metrics.md'
3232
process_user_data(user_data_file, markdown_file)

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
- id: isort
3131

3232
- repo: https://github.com/nbQA-dev/nbQA
33-
rev: 1.7.1
33+
rev: 1.8.4
3434
hooks:
3535
- id: nbqa-black
3636
- id: nbqa-pyupgrade

0 commit comments

Comments
 (0)