From 74b6195e59c1ecc8247a7a4755a311aa6029dbc4 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 20 Mar 2024 07:03:40 -0600 Subject: [PATCH 1/5] Merge branch 'main' of https://github.com/jukent/projectpythia.github.io --- .github/workflows/get-metrics.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/get-metrics.py b/.github/workflows/get-metrics.py index b3103942..c8123e57 100644 --- a/.github/workflows/get-metrics.py +++ b/.github/workflows/get-metrics.py @@ -20,7 +20,8 @@ # Access Secrets PRIVATE_KEY_ID = os.environ.get('PRIVATE_KEY_ID') -PRIVATE_KEY = os.environ.get('PRIVATE_KEY') +# Ensure GH secrets doesn't intrudce extra '\' new line characters (related to '\' being an escape character) +PRIVATE_KEY = os.environ.get('PRIVATE_KEY').replace('\\n', '\n') credentials_dict = { 'type': 'service_account', @@ -41,7 +42,9 @@ except google.auth.exceptions.MalformedError as e: print('Malformed Error:', repr(e)) # Insight into reason for failure without exposing secret key - print('Length of PRIVATE_KEY:', len(PRIVATE_KEY)) # 0: Secret not found, ~734: Secret malformed + # 0: Secret not found, else malformed + # 706: extra quote, 732: extra '\', 734: both + print('Length of PRIVATE_KEY:', len(PRIVATE_KEY)) pre_project_date = '2020-03-31' # Random date before project start From 17590e20dd82a8741d21d67a6491a1a14d3710d5 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 20 Mar 2024 07:39:03 -0600 Subject: [PATCH 2/5] add metrics steps to contributing guide --- CONTRIBUTING.md | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4398fc02..e936a3bf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -118,3 +118,79 @@ Once a pull request has passed all tests, including the `preview-site` check, th ![CI-check](/portal/_static/images/deploy-site-CI-check.png) ![Netlify Preview](/portal/_static/images/netlify-preview.png) + + +## Instructions for intacting with the Google Analytics API + +### Setting up the Virtual Environment + +Analytics must be run on a virtual environment. To create and activate this environment, with the necessary `google-analytics-data` package, the terminal commands are: + +``` +python -m venv analytics-api-env +source analytics-api-env/bin/activate +pip install google-analytics-data +``` + +Replacing 'analytics-api-env' with any new environment name. Also `pip install` any other packages you may want for your analytics work. + +### Setting up Credentials + +To interact with the Google Analytics API locally you need to download the credentials file. This file has been uploaded to the ProjectPythia Google Drive and lives in the Analytics_API folder. It will have a name similar to `cisl-vast-pythia-{letters and numbers}.json`. + +One way to ensure that your Python script is using the correct credentials file is to read it as a dictionary and pass that into your API client at the begging of your script. + +``` +from google.analytics.data_v1beta import BetaAnalyticsDataClient +from google.analytics.data_v1beta.types import DateRange, Dimension, Metric, RunReportRequest + +with open('{credentials-file-path}') as json_file: + credentials_dict = json.load(json_file) + +client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict) +``` + +Recommended and commonly needed import statements are also shown at the script beginning. + +### Making a request + +Below is a sample function for calling an Analytics API request. + +``` +def _run_analytics_request(property_id): + request = RunReportRequest( + property=f'properties/{property_id}', + dimensions=[Dimension(name='date')], + metrics=[Metric(name='activeUsers')], + date_ranges=[DateRange(start_date='2024-01-01', end_date='today')], + ) + response = client.run_report(request) + return response +``` + +This function demonstrates how to format your `RunReportRequest()` arguments, notably the `dimensions` and `metrics` calls, as well as the expected date formatting in `date_ranges`. This [Google Analytics API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) documentation lists all of the available dimension and metric keys that can be passed into your request. + +`property_id` is a 9-digit number associated with the project you are interested in. This number can be found on the Analytics project page. For Project Pythia, our three different property IDs are: +``` +PORTAL_ID = '266784902' +FOUNDATIONS_ID = '281776420' +COOKBOOKS_ID = '324070631' +``` + +### Working with your request output + +Your Google Analytics response is formatted in a series of rows that each have the key `dimension_value` and `metric_value`. You may find it easier to work with your data in a dictionary or tuple. For the single dimension of "date" and metric of "activeUsers" as specified in our example function, here is what your data manipulation may look like before you can carry out additional analysis. + +``` +dates=[] +users=[] +for row in response.rows: + date_str = row.dimension_values[0].value + date = datetime.datetime.strptime(date_str, '%Y%m%d') + dates.append(date) + users.append(int(row.metric_values[0].value)) + +dates, users = zip(*sorted(zip(dates, user_counts), key=lambda x: x[0])) +``` + +One thing to note is that your Analytics response rows are not automatically chronological, so in this example we zipped our sorted tuple to ensure that the dates are in the expected order. From 3796cefe3141240f397faff4f178fb0f766d6808 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 20 Mar 2024 07:46:18 -0600 Subject: [PATCH 3/5] add note about security --- CONTRIBUTING.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e936a3bf..620402a1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -132,11 +132,15 @@ source analytics-api-env/bin/activate pip install google-analytics-data ``` -Replacing 'analytics-api-env' with any new environment name. Also `pip install` any other packages you may want for your analytics work. +Replace 'analytics-api-env' with any new environment name. Also, `pip install` any other packages you may want for your analytics work. ### Setting up Credentials -To interact with the Google Analytics API locally you need to download the credentials file. This file has been uploaded to the ProjectPythia Google Drive and lives in the Analytics_API folder. It will have a name similar to `cisl-vast-pythia-{letters and numbers}.json`. +To interact with the Google Analytics API locally you need to download the credentials file. This file has been uploaded to the ProjectPythia Google Drive and lives in the Analytics_API folder. + +**This credentials file needs to be kept secure**, especially the `private_key` field. **Do NOT share this file.** If you do not have access to our Google Drive and need access to this file, please reach out to the team on discourse or in a community meeting. + +The credentials file will have a name similar to `cisl-vast-pythia-{letters and numbers}.json`. This file may be replaced intermittently with a slightly differnt alphanumeric string for additional security. One way to ensure that your Python script is using the correct credentials file is to read it as a dictionary and pass that into your API client at the begging of your script. @@ -168,7 +172,9 @@ def _run_analytics_request(property_id): return response ``` -This function demonstrates how to format your `RunReportRequest()` arguments, notably the `dimensions` and `metrics` calls, as well as the expected date formatting in `date_ranges`. This [Google Analytics API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) documentation lists all of the available dimension and metric keys that can be passed into your request. +This function demonstrates how to format your `RunReportRequest()` arguments, notably the `dimensions` and `metrics` fields, as well as the expected date formatting in `date_ranges`. + +This [Google Analytics API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) documentation lists all of the available dimension and metric keys that can be passed into your request. `property_id` is a 9-digit number associated with the project you are interested in. This number can be found on the Analytics project page. For Project Pythia, our three different property IDs are: ``` From de3294b65fbfd6647b192d7e575b38474ba694d4 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 20 Mar 2024 07:46:47 -0600 Subject: [PATCH 4/5] pre-commit --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 620402a1..03b0be40 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -138,7 +138,7 @@ Replace 'analytics-api-env' with any new environment name. Also, `pip install` a To interact with the Google Analytics API locally you need to download the credentials file. This file has been uploaded to the ProjectPythia Google Drive and lives in the Analytics_API folder. -**This credentials file needs to be kept secure**, especially the `private_key` field. **Do NOT share this file.** If you do not have access to our Google Drive and need access to this file, please reach out to the team on discourse or in a community meeting. +**This credentials file needs to be kept secure**, especially the `private_key` field. **Do NOT share this file.** If you do not have access to our Google Drive and need access to this file, please reach out to the team on discourse or in a community meeting. The credentials file will have a name similar to `cisl-vast-pythia-{letters and numbers}.json`. This file may be replaced intermittently with a slightly differnt alphanumeric string for additional security. From 0e0bdb500e9359900d628fa5c81b0fc11aab15a7 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 20 Mar 2024 07:48:04 -0600 Subject: [PATCH 5/5] sp --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 03b0be40..2a942970 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -140,7 +140,7 @@ To interact with the Google Analytics API locally you need to download the crede **This credentials file needs to be kept secure**, especially the `private_key` field. **Do NOT share this file.** If you do not have access to our Google Drive and need access to this file, please reach out to the team on discourse or in a community meeting. -The credentials file will have a name similar to `cisl-vast-pythia-{letters and numbers}.json`. This file may be replaced intermittently with a slightly differnt alphanumeric string for additional security. +The credentials file will have a name similar to `cisl-vast-pythia-{letters and numbers}.json`. This file may be replaced intermittently with a slightly different alphanumeric string for additional security. One way to ensure that your Python script is using the correct credentials file is to read it as a dictionary and pass that into your API client at the begging of your script.