Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zero results in Google Analytics Reporting API V4?

Using the v4 API for PHP, I have noticed that zero results do not appear as rows in the results.

In using the ga:avgSessionDuration metric and ga:date dimension for a period of 1 week, days that have a 0 for the average session duration do not return in the data, so I do not have a whole week to chart.

According to the docs, v3 will return 0 lines, and in the Query Explorer, the zero values show up since they are using the v3 API. But it seems that v4 does not, at least not without additional configuration, unless I missed something.

Has anyone else come across this, and is there a solution?

like image 555
xclntDesign Avatar asked Sep 05 '25 03:09

xclntDesign


1 Answers

The V3 include-empty-rows parameter corresponds to the includeEmptyRows field in V4. The V3 parameter defaults to true, while in V4 the field defaults to false. If you do not have the value set in V3 you will need to set the value to true in V4.

The following example shows how to set the includeEmptyRows to be True:

PHP

// Create the Metrics object.
$avgsessionduration = new Google_Service_Analyticsreporting_Metric();
$avgsessionduration->setExpression("ga:avgSessionDuration");

//Create the Dimensions object.
$date = new Google_Service_Analyticsreporting_Dimension();
$date->setName("ga:date");

// Create the ReportRequest object.
$request = new Google_Service_Analyticsreporting_ReportRequest();
$request->setViewId("XXXX");
$request->setDimensions(array($date));
$request->setMetrics(array($avgsessionduration));
$request->setIncludeEmptyRows(true); // <-- Ensure this value to be true.

// Construct the request body.
$body = new Google_Service_Analyticsreporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analyticsreporting->reports->batchGet( $body );

JSON

{
  "reportRequests":[
  {
    "viewId":"XXXX",
    "metrics":[{"expression":"ga:avgSessionDuration"}],
    "dimensions": [{"name":"ga:date"}],
    "includeEmptyRows": "true"
  }]
}

See the Migration guide for details on migrating from the Core Reporting API V3 to the Analytics Reporting API V4.

like image 200
Matt Avatar answered Sep 07 '25 23:09

Matt