Filtering Results from the Google Analytics Reporting API

I successfully download results from Google Analytics using the reporting API (version 4) with the PHP client library. But I did not understand how to properly filter these results.

I see how this will work through cURL, but not through the client library. I looked at the client library code, and there is a class method:

apiclient-services/Google/Service/AnalyticsReporting/ReportRequest.php: public function setMetricFilterClauses($metricFilterClauses) 

I do not see any documentation or any use of the associated get method:

 public function getMetricFilterClauses() 

Are there any examples of using filters through the PHP client library?

+5
source share
2 answers

Background

Google API client libraries are created from Google Apps for discovery . And the PHP client library generates setProperty and getProperty for each resource property.

Google V4 Reporting Reporting API

References V4 Reporting API reports in Google Analytics describe the API API exhustively. The developer guide provides a basic example of JSON that will be generated by client libraries:

 POST https://analyticsreporting.googleapis.com/v4/reports:batchGet { "reportRequests": [ { "viewId": "XXXX", "dateRanges": [ {"endDate": "2014-11-30", "startDate": "2014-11-01"} ], "metrics": [ {"expression": "ga:pageviews"}, {"expression": "ga:sessions"} ], "dimensions": [{"name": "ga:browser"}, {"name": "ga:country"}], "dimensionFilterClauses": [ { "filters": [ { "dimensionName": "ga:browser", "operator": "EXACT", "expressions": ["Chrome"] } ] } ] } ] } 

And the Samples page provides many sample queries in Python, Java, PHP and JavaScript that should give you a good idea of โ€‹โ€‹how to work with individual client libraries, But you are right, there is no explicit example of PHP using a filter.

PHP Filter Example

Below is the example above:

 // Create the DateRange object. $dateRange = new Google_Service_AnalyticsReporting_DateRange(); $dateRange->setStartDate("2014-11-01"); $dateRange->setEndDate("2014-11-30"); // Create the Metrics object. $pageviews = new Google_Service_AnalyticsReporting_Metric(); $pageviews->setExpression("ga:pageviews"); $sessions = new Google_Service_AnalyticsReporting_Metric(); $sessions->setExpression("ga:sessions"); //Create the Dimensions object. $browser = new Google_Service_AnalyticsReporting_Dimension(); $browser->setName("ga:browser"); $country = new Google_Service_AnalyticsReporting_Dimension(); $country->setName("ga:country"); // Create the DimensionFilter. $dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter(); $dimensionFilter->setDimensionName('ga:browser'); $dimensionFilter->setOperator('EXACT'); $dimensionFilter->setExpressions(array('Chrome')); // Create the DimensionFilterClauses $dimensionFilterClause = new Google_Service_AnalyticsReporting_DimensionFilterClause(); $dimensionFilterClause->setFilters(array($dimensionFilter)); // Create the ReportRequest object. $request = new Google_Service_AnalyticsReporting_ReportRequest(); $request->setViewId("XXXX"); $request->setDateRanges($dateRange); $request->setDimensions(array($browser, $country)); $request->setDimensionFilterClauses(array($dimensionFilterClause)); $request->setMetrics(array($pageviews, $sessions)); $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); $body->setReportRequests( array( $request) ); return $analyticsreporting->reports->batchGet( $body ); 

As you probably noticed, I never used $object->getProperty() . Basically all he would do was give me the current value. When calling the API, you always only need $object->setProperty($value); Therefore, why I gave you the background created by the client libraries.

Conclusion

The Analytics Reporting API itself is complex and there are many client library languages. It is not always possible to give an example of using the API in all possible languages โ€‹โ€‹of the client library. That is why it is necessary to understand how to look at reference documents and understand how client libraries are created from the described structure.

+19
source

There is a problem with the DimensionFilter () class in the script above, I get the error that it is not defined, but I changed it to the Google_Service_AnalyticsReporting_DimensionFilter () class, and now it is hoping this will help someone.

-1
source

All Articles