Reputation: 11
How to get page views count for few (10) URLs from google analytics API?
$requests = [];
foreach($urls as $url) {
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($start_date);
$dateRange->setEndDate($end_date);
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:pageviews");
$sessions->setAlias("pageviews");
$dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
$dimensionFilter->setDimensionName('ga:pagePath');
$dimensionFilter->setOperator('BEGINS_WITH');
$dimensionFilter->setExpressions([$url]);
$dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause();
$dimensionFilterClause->setFilters([$dimensionFilter]);
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($this->view_id);
$request->setDateRanges($dateRange);
$request->setMetrics([$sessions]);
$request->setDimensionFilterClauses([$dimensionFilterClause]);
$requests[] = $request;
}
// Finally, we perform one large query for every URL address mentioned
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$requests]);
$reports = $this->analytics->reports->batchGet( $body );
If URLs count is more than 5 throwing exception with message:
There are too many requests in the batch request. The max allowed is 5
How I can do it? GA API version 4.
Upvotes: 1
Views: 1169
Reputation: 116958
Answer: You can have a max of 5 reports per batch. There is no way to send 10 reports in a single batch request.
reportRequests[] object(ReportRequest) Requests, each request will have a separate response. There can be a maximum of 5 requests. All requests should have the same dateRanges, viewId, segments, samplingLevel, and cohortGroup.
Possible work abounds:
This means that if you have 10 your probably going to have to split it into two requests. You could possibly look at your filter expressions you can add more then one there I think. (tip: add ga:pagepath dimension so you can map the data response to the page it hit in the filter)
Strings or regular expression to match against. Only the first value of the list is used for comparison unless the operator is IN_LIST. If IN_LIST operator, then the entire list is used to filter the dimensions as explained in the description of the IN_LIST operator.
Note: I haven't actually tested multiple filters but I would think it should work.
Upvotes: 2