google 分析 api 需要准确的日期、小时和分钟

google analytics api need exact date, hour and minute

我正在试验 google 报告 api (v4),我使用 goalCompletionsAll 指标,并带来我需要的信息,我作为参数传递 ga:medium,ga:date, ga:hour 然而,当我根据请求传递 ga:minute 参数时,它会抛出以下错误: "Selected dimensions and metrics cannot be queried together." 我知道指标有自己的一组维度,但我发现这很奇怪,因为当我使用查询浏览器时,我添加了所有这些维度并且它 returns 是正确的信息,但是一旦我输入代码,它就会显示错误...

我在这里附上我的代码,看看是否有人可以为此指出正确的方向。

<?php
require_once 'google-api-php-client/src/Google/autoload.php';

session_start();

$client = new Google_Client();
$client->setAuthConfigFile('xxxxx');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);


// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  // Set the access token on the client.
  $client->setAccessToken($_SESSION['access_token']);

  // Create an authorized analytics service object.
  $analytics = new Google_Service_AnalyticsReporting($client);

  // Call the Analytics Reporting API V4.
  $response = getReport($analytics);

  // Print the response.
  printResults($response);

} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/api/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}


function getReport(&$analytics) {

  // Replace with your view ID. E.g., XXXX.
  $VIEW_ID = "xxxxxxxx";

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

  // Create the Metrics object.
  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:goalCompletionsAll");
  $sessions->setAlias("objetivos");
  
  //Create the Dimensions object.
  $medium = new Google_Service_Analyticsreporting_Dimension();
  $medium->setName("ga:medium");
  
  $minute = new Google_Service_Analyticsreporting_Dimension();
  $minute->setName("ga:minute");
  
  $hour = new Google_Service_Analyticsreporting_Dimension();
  $hour->setName("ga:hour");

  $date = new Google_Service_Analyticsreporting_Dimension();
  $date->setName("ga:date");

  $avgpageloadtime = new Google_Service_Analyticsreporting_Metric();
  $avgpageloadtime->setExpression("ga:avgpageloadtime");
  $avgpageloadtime->setAlias("average load time");

  $goals = new Google_Service_Analyticsreporting_Metric();
  $goals->setExpression("ga:goalStartsAll");

  // Create the ReportRequest object.
  $request = new Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setDimensions(array($medium, $date, $hour, $minute));
  $request->setMetrics(array($sessions, $avgpageloadtime, $goals));

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


function printResults(&$reports) {
  for ( $reportIndex = 0; $reportIndex < count( $reports ); $reportIndex++ ) {
    $report = $reports[ $reportIndex ];
    $header = $report->getColumnHeader();
    $dimensionHeaders = $header->getDimensions();
    $metricHeaders = $header->getMetricHeader()->getMetricHeaderEntries();
    
    $rows = $report->getData()->getRows();

    for ( $rowIndex = 0; $rowIndex < count($rows); $rowIndex++) {
      $row = $rows[ $rowIndex ];
      $dimensions = $row->getDimensions();
      $metrics = $row->getMetrics();
      for ($i = 0; $i < count($dimensionHeaders) && $i < count($dimensions); $i++) {
        print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");

      }

      echo "<br>";

      for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) {
        $entry = $metricHeaders[$j];

        $values = $metrics[$j];
        //print("Tipo de metrica: " . $entry->getType() . "\n" );
        for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) {
          $value = $values->getValues()[ $valueIndex ];
          //echo $values->getValues()[$valueIndex]."<br>";
          //print($entry->getName() . ": " . $value . "<br>");
        }
      }
    }
  }
}

?>

提前致谢。

您看到的问题是,在查询资源管理器中,您正在查询 ga:goalCompletionsAll,而 V4 API 您正在查询 ga:gaolStartsAll,这恰好与 ga:avgPageLoadTime:

有关哪些维度与其他维度不兼容的详细信息,请参阅 Dimensions and Metrics Explorer

去掉ga:avgPageLoadTime,你应该可以得到你需要的信息。

TLDR:您的 V4 API 请求具有与查询浏览器请求不同的指标。