Google Analytics(分析)从API获取用户报告的数据

时间:2018-08-02 19:43:03

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

我想生成一个报告,例如“用户报告”(在Audience => User Explorer上),其会话持续时间每天。

如何通过使用PHP的Google Analitycs API获取此数据?

我将尝试维度= ga:sessionDurationBucket和指标= ga:sessionDuration。但是我无法生成相同的报告。

1 个答案:

答案 0 :(得分:0)

您尝试过PHP快速入门吗?调整所需的内容并不难: https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php

  // Create the DateRange object.
  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("7daysAgo");
  $dateRange->setEndDate("today");

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

  // Create the ReportRequest object.
  $request = new Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setMetrics(array($sessions));

  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests( array( $request) );
  var $result = $analytics->reports->batchGet( $body );

关于如何创建自定义维度,请参见以下示例: https://developers.google.com/analytics/devguides/reporting/core/v4/samples#dimensions_and_metrics

//Create the Dimensions object.
Dimension browser = new Dimension()
    .setName("ga:browser");

// Create the ReportRequest object.
ReportRequest request = new ReportRequest()
    .setViewId("XXXX")
    .setDateRanges(Arrays.asList(dateRange))
    .setDimensions(Arrays.asList(browser))
    .setMetrics(Arrays.asList(sessions));