【问题标题】:How to use parameters in Google Analytics API如何在 Google Analytics API 中使用参数
【发布时间】:2016-06-16 08:25:38
【问题描述】:

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

如何获取人们开始观看我网站的 URL“进入点”。以及如何在'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
        )

)

【问题讨论】:

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


    【解决方案1】:

    当你运行你的请求时

    $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];
    }   
    

    【讨论】:

    • 谢谢!以及如何逐页显示结果页面?因为默认的最大结果是 1k
    • 抱歉,我没有代码。但是我“认为”您阅读了请求结果中的下一页标记,然后将其作为参数传递给新请求,就像您对尺寸所做的那样,只需添加一个新的 developers.google.com/api-client-library/php/guide/pagination 抱歉,我已经做了一段时间了。
    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 2013-07-06
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多