如何在 Google Analytics API 中使用参数

How to use parameters in Google Analytics API

如何在getResults函数中使用参数?我需要获取有关我的网站用户正在观看哪些网址的信息。如果我使用 ga:sessions,我会看到那里有多少用户。如果我将其更改为 ga:pageviews,则数字(在结果数组中)会发生变化。所以这意味着 API 是 "alive"。

如何获取人们开始观看我网站的网址 "points of enter"。以及如何在这个地方发送参数 'ga:sessions'); ? API 我正在阅读的说明是 here

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 printResults(&$results) {
  // Parses the response from the Core Reporting API and prints
  // the profile name and total sessions.
  if (count($results->getRows()) > 0) {

    // Get the profile name.
    $profileName = $results->getProfileInfo()->getProfileName();

    // Get the entry for the first entry in the first row.
    $rows = $results->getRows();
//    $sessions = $rows[0][0];

    // Print the results.

    echo '<pre>';
    print_r($rows);


  } else {
    print "No results found.\n";
  }
}

目前结果是:

Array
(
    [0] => Array
        (
            [0] => 3585
        )

)

当你运行你的请求

$analytics->data_ga->get('ga:' . $profileId,    
                         '7daysAgo',
                         'today',
                         'ga:sessions');

您正在做的是请求 Google Analytics 为您提供从今天到 7 天前的个人资料会话数。它实际上在做什么。在那段时间内有 3585 次会话

现在,如果您查看 dimensions and metrics explorer,您会发现一大堆维度和指标。 ga:sessions 是一个指标 ga:pageviews 是一个维度,因此您需要将维度添加到您的请求中。

$params = array('dimensions' => 'ga:pageviews');    
$analytics->data_ga->get('ga:' . $profileId,    
                         '7daysAgo',
                         'today',
                         'ga:sessions',
                         $params);

现在 运行 您的请求,您应该会得到每个页面的列表以及该页面的会话总数。

提示:

foreach ($results->getRows() as $row) {         
    print $row[0]." - ".$row[1];
}