Skip to content

Instantly share code, notes, and snippets.

@mcritchlow
Last active March 28, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcritchlow/0fc999de486182065b1d2a5402e33ecd to your computer and use it in GitHub Desktop.
Save mcritchlow/0fc999de486182065b1d2a5402e33ecd to your computer and use it in GitHub Desktop.
Analytics Query Testing

Summary

https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet

This gist is for doing some batch query testing against GA. The data we need is:

Page level

  1. pageviews, users, newUsers => pageview-query.js

setting pageSize to 100 to get the nextPageToken in the response users = "The total number of users for the requested time period" newUsers = "The number of sessions marked as a user's first sessions"

  1. downloads => download_query.js

setting pageSize to 100 to get the nextPageToken in the response

Hoping we can filter on the eventCategory or eventAction so we can do more targeted querying

Site level

  1. users, newUsers => sitewide-query.js

setting pageSize to 100 to get the nextPageToken in the response

  1. unique_visitors
  2. returning_visitors

Notes on batches

  1. We can set the pageSize up to 10000
  2. Any additional results not returned can be accessed by sending another query using the nextPageToken from the response as the pageToken in the next request

Questions

  1. Can we get pageviews, visitors (both types) and downloads back in a single query? Probably not downloads.. but worth a shot. Maybe if we send downloads as a virtual pageview??
  2. How do we calculate "unique_visitors" and "returning_visitors" from the GA metrics newUsers and users??
  3. unique_visitors == users ?
  4. returning_visitors == users - newUsers ?
{
"reportRequests": [
{
"viewId": "15625106",
"dateRanges": [
{
"startDate": "2015-01-01",
"endDate": "2018-01-01"
}
],
"dimensions": [
{
"name": "ga:pagePath"
},
{
"name": "ga:date"
}
],
"metrics": [
{
"expression": "ga:sessions"
},
{
"expression": "ga:users"
},
{
"expression": "ga:pageviews"
}
],
"pageSize": 15,
"pageToken": "0",
"orderBys": [
{
"fieldName": "ga:date"
}
],
"filtersExpression": "ga:pagePath=~/concern/generic_works,ga:pagePath=~/concern/namespaced_works/nested_works,ga:pagePath=~/concern/images,ga:pagePath=~file_sets;ga:pagePath!~/concern/generic_works/*/edit,ga:pagePath!~/concern/namespaced_works/nested_works/*/edit,ga:pagePath!~/concern/images/*/edit,ga:pagePath!~file_sets/*/edit"
}
]
}
{
"reportRequests": [
{
"viewId": "15625106",
"dateRanges": [
{
"startDate": "2015-01-01",
"endDate": "2018-01-01"
}
],
"dimensions": [
{
"name": "ga:date"
}
],
"metrics": [
{
"expression": "ga:sessions"
},
{
"expression": "ga:users"
}
],
"pageSize": 5,
"orderBys": [
{
"fieldName": "ga:date"
}
]
}
]
}
{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:sessions",
"type": "INTEGER"
},
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20150101"
],
"metrics": [
{
"values": [
"1508",
"1318"
]
}
]
},
{
"dimensions": [
"20150102"
],
"metrics": [
{
"values": [
"2256",
"1864"
]
}
]
},
{
"dimensions": [
"20150103"
],
"metrics": [
{
"values": [
"2054",
"1816"
]
}
]
},
{
"dimensions": [
"20150104"
],
"metrics": [
{
"values": [
"2980",
"2315"
]
}
]
},
{
"dimensions": [
"20150105"
],
"metrics": [
{
"values": [
"9117",
"6933"
]
}
]
}
],
"totals": [
{
"values": [
"5920281",
"4413140"
]
}
],
"rowCount": 1097,
"minimums": [
{
"values": [
"843",
"736"
]
}
],
"maximums": [
{
"values": [
"12679",
"11159"
]
}
],
"samplesReadCounts": [
"498707"
],
"samplingSpaceSizes": [
"5920292"
],
"isDataGolden": true
},
"nextPageToken": "5"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment