PHP - Google AnalyticsAPI - 单页面视图/会话

时间:2018-04-05 10:36:25

标签: php google-analytics google-api google-analytics-api google-api-php-client

我想获取特定网址的网页浏览量,比方说123456.html。如果我在分析上创建细分,我可以看到2018-03-012018-03-29之间有17个会话和79个页面浏览量。

我已尝试过以下操作,但据我所知,它没有返回任何会话数据:

function getReport($analytics) {

  // Replace with your view ID, for example XXXX.
  $VIEW_ID = "XXX";
  $_date = "2018-03-01";
  $_end = "2018-03-29";

  // Create the DateRange object.
  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate($_date);
  $dateRange->setEndDate($_end);

  // Create the Metrics object.
  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:sessions");
  $sessions->setAlias("sessions");

  //Create the Dimensions object.
  $pagePath = new Google_Service_AnalyticsReporting_Dimension();
  $pagePath->setName("ga:browser");

  //Create the DimensionFilter.
  $dimensionFilter = new Google_Service_AnalyticsReporting_DimensionFilter();
  $dimensionFilter->setDimensionName('ga:pagePath');
  $dimensionFilter->setOperator('ENDS_WITH');
  $dimensionFilter->setExpressions(array('123456.html'));

  //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($VIEW_ID);
  $request->setDateRanges($dateRange);

  //$request->setDimensions(array($pagePath));
  $request->setDimensionFilterClauses(array($dimensionFilterClause));
  $request->setMetrics(array($sessions));

  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $request) );

  $response = $analytics->reports->batchGet( $body );

  print_r($response);

}

0 个答案:

没有答案