Google Analytics(分析)API - 按网址获取网页浏览量

时间:2017-09-04 14:33:22

标签: php api google-analytics

我能够从google analytics api运行de“hello”应用程序:

t2.nano

返回(整个)帐户的页面浏览量。 我想获取特定网址的页面浏览量(针对整个范围)

我正试图找到如何做到这一点,但我找到的similar page最多,似乎是用不同的语法编写的。所以我不知道如何在当前应用程序中输入这些参数。

有什么想法吗?

- 编辑 -

有关更多信息,请使用上一代码打印:

但是这段代码(我之前提供的链接的“语法”)

function getFirstProfileId($analytics) {
    // Get the user's first view (profile) ID.

    // Get the list of accounts for the authorized user.
    $accounts = $analytics->management_accounts->listManagementAccounts();

    if (count($accounts->getItems()) > 0) {
        $items = $accounts->getItems();
        $firstAccountId = $items[0]->getId();

        // Get the list of properties for the authorized user.
        $properties = $analytics->management_webproperties
                ->listManagementWebproperties($firstAccountId);

        if (count($properties->getItems()) > 0) {
            $items = $properties->getItems();
            $firstPropertyId = $items[0]->getId();

            // Get the list of views (profiles) for the authorized user.
            $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstPropertyId);

            if (count($profiles->getItems()) > 0) {
                $items = $profiles->getItems();

                // Return the first view (profile) ID.
                return $items[0]->getId();

            } else {
                throw new Exception('No views (profiles) found for this user.');
            }
        } else {
            throw new Exception('No properties found for this user.');
        }
    } else {
        throw new Exception('No accounts found for this user.');
    }
}

function getResults($analytics, $profileId) {
    // Calls the Core Reporting API and queries for the number of sessions
    // for the last seven days.
    return $analytics->data_ga->get(
        'ga:' . $profileId,
        '7daysAgo',
        'today',
        'ga:sessions'
    );
}

打印出来:

function getReport($analytics) {

  // Replace with your view ID, for example XXXX.
  $VIEW_ID = "40xxxyyy9"; // I got it from The Google Account Explorer 

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

      for ($j = 0; $j < count( $metricHeaders ) && $j < count( $metrics ); $j++) {
        $entry = $metricHeaders[$j];
        $values = $metrics[$j];
        print("Metric type: " . $entry->getType() . "\n" );
        for ( $valueIndex = 0; $valueIndex < count( $values->getValues() ); $valueIndex++ ) {
          $value = $values->getValues()[ $valueIndex ];
          print($entry->getName() . ": " . $value . "\n");
        }
      }
    }
  }
}

如果我按照控制台链接到我的项目。它让我知道已经启用(否则问题中的第一个代码不起作用)

1 个答案:

答案 0 :(得分:3)

您的示例实际上返回的是会话数,而不是网页浏览量。因此,要获取网址的网页浏览量,您需要a)将ga更改为会话:ga:pageViews和b)应用将数据限制为预期网址的过滤器。

过滤器可以通过将它们作为可选参数数组的一部分传递来应用,因此您需要稍微更改一下您的函数(this basically taken from the documentation):

function getResults($analytics, $profileId) {

    $optParams = array(
          'filters' => 'ga:pagePath=@/my/url'
    );

    return $analytics->data_ga->get(
        'ga:' . $profileId,
        '7daysAgo',
        'today',
        'ga:pageViews',
        $optParams
    );
}

IIRC =@应该是过滤器中的部分匹配(请参阅文档),当然您需要将“/ my / url”更改为您的实际网址。

另请注意我对不同API版本的评论。