Filtering results from Google Analytics Reporting API

后端 未结 2 2093
你的背包
你的背包 2021-02-08 16:07

I am successfully downloading results from Google Analytics using the reporting API (version 4), with the PHP client library. But I have not figured out how to correctly filter

相关标签:
2条回答
  • 2021-02-08 17:07

    Background

    The Google API Client libraries are generated from the Google Discovery Service. And the PHP client library generates a setProperty and getProperty for every property of a resource.

    Analytics Reporting API V4

    The Analytics Reporting API V4 reference docs exhustively describe the API. The Developer Guide gives the underlying JSON example which the client libraries will generate:

    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 gives many examples requests in Python, Java, PHP and JavaScript, which should give you a good sense of how to work with the individual client libraries. But you are correct there is not an explicit example of PHP using a filter.

    PHP Filter Example

    Below is the same example as the request 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 once used a $object->getProperty(). Basically All it would do is give me its current value. When Calling the API you should only ever need to $object->setProperty($value); Hence why I gave you the background that the client libraries are generated.

    Conclusion

    The Analytics Reporting API itself is complex and there are many client library languages. It is not always possible to give an example every possible usage of an API in every possible client library language. That is why it is necessary to understand how to look at the reference docs and understand how the client libraries are generated from the structure described.

    0 讨论(0)
  • 2021-02-08 17:10

    There is an issue with DimensionFilter() class in a script above, i get error that it is not defined, but i have changed it to Google_Service_AnalyticsReporting_DimensionFilter() class and now it is working hope that will help to someone.

    0 讨论(0)
提交回复
热议问题