谷歌分析API需要确切的日期,小时和分钟

时间:2016-08-26 16:25:09

标签: google-analytics google-analytics-api google-analytics-v4

我正在尝试google报告api(v4),即时通讯使用goalCompletionsAll指标,并带来我需要的信息,我传递的参数ga:medium,ga:date,ga:hour然而当我通过ga时:分钟参数作为请求它会抛出以下错误:“无法一起查询所选维度和指标。”我知道指标有自己的维度集,但我发现这很奇怪,因为当我使用查询资源管理器时,我添加所有这些维度并返回正确的信息,但只要我输入代码,它就会显示错误...

enter image description here

enter image description here

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

<?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>");
        }
      }
    }
  }
}

?>

提前致谢。

1 个答案:

答案 0 :(得分:3)

您看到的问题是,在查询资源管理器中,您要查询ga:goalCompletionsAll以及您要查询ga:gaolStartsAll的V4 API,这恰好与ga:avgPageLoadTime不兼容:

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

删除ga:avgPageLoadTime,您应该能够获得所需的信息。

TLDR:您的V4 API请求具有与查询资源管理器请求不同的指标。