-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreport_active_users.sh
executable file
·53 lines (47 loc) · 1.79 KB
/
report_active_users.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Script to query GA4 for active users per day for BRAVO
# Configure application default credentials:
# https://cloud.google.com/docs/authentication/provide-credentials-adc
# https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-cli
#
# Use the credentials of Oauth Client "GA Reports CLI"
#
# gcloud auth application-default login \
# --scopes=https://www.googleapis.com/auth/analytics.readonly \
# --client-id-file=/PATH/TO/credentials.json
# Default to freeze8 property id
GA4_PROPERTY_ID=${GA4_PROPERTY_ID:-361544710}
# Date calculation adapted from https://unix.stackexchange.com/a/249794
# First day of current month
THIS_MONTH=$(date +%Y%m01)
# Report start day is one month prior to THIS_MONTH
START_DATE=$(date -d "${THIS_MONTH} -1 month" +%Y-%m-%d)
# Report end day is one day prior to THIS_MONTH
END_DATE=$(date -d "${THIS_MONTH} -1 day" +%Y-%m-%d)
# Interpolate dates into analytics query
read -r -d '' QUERY <<-GA_EOF
{
"dimensions":[{"name":"nthDay"}],
"metrics":[{"name":"active1DayUsers"}],
"dateRanges":[{"startDate":"${START_DATE}",
"endDate":"${END_DATE}"}]
}
GA_EOF
# Make request to analytics api
# JQ to parse and process resulting json
GA_ACCESS_TOKEN=$(gcloud auth application-default print-access-token)
curl -X POST \
-H "Authorization: Bearer ${GA_ACCESS_TOKEN}" \
-H "Content-Type: application/json; charset=utf-8" \
https://analyticsdata.googleapis.com/v1beta/properties/$GA4_PROPERTY_ID:runReport \
-d "${QUERY}" | \
jq --arg start_dt $START_DATE --arg end_dt $END_DATE \
'.rows |
map_values(.metricValues[0]) |
map_values(.value | tonumber) |
{"project": "BRAVO Freeze 8",
"metric": "Active Users Per Day",
"start": $start_dt,
"end": $end_dt,
"mean": (add / length),
"min": min,
"max": max}'