I am building a Google Analytics Dashboard and have it pretty completed, but am just totally stumped on this one thing that is really hanging me up.
When I query the GA Core Reporting API for a dataset, I successfully get and am able to display results and everything works fine EXCEPT I am not able to query the JSON object for the “start-index”. That is, the first row that it will display (default = 1, but GA only returns 10k rows at a time, so if you have a data set with >10k rows, this becomes critically important!).
To attempt to get this integer, I use
$start = $results->query.start-index;
and as a check to make sure I am not insane, “max-results” is right next to “start-index” in the JSON object and this works fine:
$max = $results->totalResults;
Here is the GA JSON object (per https://developers.google.com/analytics/devguides/reporting/core/v3/reference#startIndex)
{
"kind": "analytics#gaData",
"id": string,
"selfLink": string,
"containsSampledData": boolean,
"query": {
"start-date": string,
"end-date": string,
"ids": string,
"dimensions": [
string
],
"metrics": [
string
],
"sort": [
string
],
"filters": string,
"segment": string,
"start-index": integer,
"max-results": integer
},
"itemsPerPage": integer,
"totalResults": integer,
"previousLink": string,
"nextLink": string,
"profileInfo": {
"profileId": string,
"accountId": string,
"webPropertyId": string,
"internalWebPropertyId": string,
"profileName": string,
"tableId": string
},
"columnHeaders": [
{
"name": string,
"columnType": string,
"dataType": string
}
],
"rows": [
[
string
]
],
"totalsForAllResults": [
{
metricName: string,
...
}
]
}
Help! Thanks in advance
So I am still not entirely sure why this works the way it does, but I’ve successfully pulled the correct start-index number using the following function:
This essentially loops through the entire $query associative array until it finds one where the parameter matches “start-index” and then returns the value.