Ive been trying to get the metrics for all urls that contain a specific id in them. From this question: Adding ga:pagePath dimension to get page views for a particular URL using Google Analytics Reporting API v4 I saw the query approach and tried it instead of the object oriented version. In the query approach, the code works fantastic with the exception of it will only return the last metric sent (in this example it only returns unique page views since it is listed last for metrics). I need the values for all three returned without having to hit the api three separate times. Here is my code:
$query = [
"viewId" => $profileId,
"dateRanges" => [
"startDate" => "2018-01-25",
"endDate" => "2018-01-25"
],
"metrics" => [
"expression" => "ga:pageviews",
"expression" => "ga:avgTimeOnPage",
"expression" => "ga:uniquePageviews"
],
"dimensions" => [
"name" => "ga:pagepath"
],
"dimensionFilterClauses" => [
'filters' => [
"dimension_name" => "ga:pagepath",
"operator" => "PARTIAL",
"expressions" => $theId
]
]
];
// build the request and response
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests(array($query));
$report = $analytics->reports->batchGet($body);
Any thoughts on how I would do this correctly?
For those who want to keep building queries with an array, this should work:
$request = [
"viewId" => "123456789",
"dateRanges" => [
"startDate" => "2018-01-01",
"endDate" => "today"
],
"metrics" => [
"expression" => "ga:pageviews"
],
"dimensions" => [
["name" => "ga:browser"],
["name" => "ga:sessionDurationBucket"]
],
"dimensionFilterClauses" => [
'filters' => [
"dimension_name" => "ga:pagepath",
"operator" => "EXACT",
"expressions" => $url
]
]
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With