Filter Google Analytics report

29 Nov 2022 in TIL

I'm trying out Radar to keep track of analytics at a glance. It's Google Analytics collection works great, but the way that we've set up Google Analytics the data for multiple sites ends up in the same property.

I wanted to filter the report to only traffic to a specific subdomain. It took me too long to work out that I needed a dimensionFilterClauses entry with the ga:pagePath (via the schema).

Here's how to filter your reports in case you (or I) need to do it in the future:

json
{
"reportRequests": [
{
"viewId": "YOUR_VIEW_ID",
"dateRanges": [
{
"startDate": "today",
"endDate": "today"
}
],
"dimensions": [
{
"name": "ga:date"
}
],
"metrics": [
{
"expression": "ga:pageviews"
}
],
"dimensionFilterClauses": [
{
"filters": [
{
"dimensionName": "ga:pagePath",
"operator": "PARTIAL",
"expressions": ["docs.konghq.com"]
}
]
}
]
}
]
}

Other things that may be useful:

Endpoint: https://analyticsreporting.googleapis.com/v4/reports:batchGet
Method: POST
Auth: Bearer [token]

Then transform the results using the following JS snippet:

javascript
const pageCount = Number(
data.reports[0].data.totals[0].values[0]
).toLocaleString();