Skip to content

Commit 17590e2

Browse files
committedMar 20, 2024·
add metrics steps to contributing guide
1 parent 74b6195 commit 17590e2

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
 

Diff for: ‎CONTRIBUTING.md

+76
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,79 @@ Once a pull request has passed all tests, including the `preview-site` check, th
118118
![CI-check](/portal/_static/images/deploy-site-CI-check.png)
119119
120120
![Netlify Preview](/portal/_static/images/netlify-preview.png)
121+
122+
123+
## Instructions for intacting with the Google Analytics API
124+
125+
### Setting up the Virtual Environment
126+
127+
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:
128+
129+
```
130+
python -m venv analytics-api-env
131+
source analytics-api-env/bin/activate
132+
pip install google-analytics-data
133+
```
134+
135+
Replacing 'analytics-api-env' with any new environment name. Also `pip install` any other packages you may want for your analytics work.
136+
137+
### Setting up Credentials
138+
139+
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`.
140+
141+
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.
142+
143+
```
144+
from google.analytics.data_v1beta import BetaAnalyticsDataClient
145+
from google.analytics.data_v1beta.types import DateRange, Dimension, Metric, RunReportRequest
146+
147+
with open('{credentials-file-path}') as json_file:
148+
credentials_dict = json.load(json_file)
149+
150+
client = BetaAnalyticsDataClient.from_service_account_info(credentials_dict)
151+
```
152+
153+
Recommended and commonly needed import statements are also shown at the script beginning.
154+
155+
### Making a request
156+
157+
Below is a sample function for calling an Analytics API request.
158+
159+
```
160+
def _run_analytics_request(property_id):
161+
request = RunReportRequest(
162+
property=f'properties/{property_id}',
163+
dimensions=[Dimension(name='date')],
164+
metrics=[Metric(name='activeUsers')],
165+
date_ranges=[DateRange(start_date='2024-01-01', end_date='today')],
166+
)
167+
response = client.run_report(request)
168+
return response
169+
```
170+
171+
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.
172+
173+
`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:
174+
```
175+
PORTAL_ID = '266784902'
176+
FOUNDATIONS_ID = '281776420'
177+
COOKBOOKS_ID = '324070631'
178+
```
179+
180+
### Working with your request output
181+
182+
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.
183+
184+
```
185+
dates=[]
186+
users=[]
187+
for row in response.rows:
188+
date_str = row.dimension_values[0].value
189+
date = datetime.datetime.strptime(date_str, '%Y%m%d')
190+
dates.append(date)
191+
users.append(int(row.metric_values[0].value))
192+
193+
dates, users = zip(*sorted(zip(dates, user_counts), key=lambda x: x[0]))
194+
```
195+
196+
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.

0 commit comments

Comments
 (0)
Please sign in to comment.