0

我正在尝试使用 Google Analytics API 获取过去 X 年的每周和每月访问者总数。我设置:

metrics=ga:visitors
dimensions=ga:nthWeek (or nthMonth)

它返回我想要的数据:

0 week : 100 visitors
1 week : 200 visitors
2 week : 300 visitors

0我只希望它显示为而不是星期01/01/2012。如何将第 N 周(或月)转换为日期?

我试过了:

  • 作为指标传递ga:date(这将是一个合乎逻辑的事情,类似于它在 SQL 中的完成方式),但它不会将其识别为有效的指标。
  • ga:date作为第二个维度传递可以防止每周分组。
4

1 回答 1

0

你没有提到你正在使用什么语言,所以我不知道你可以使用什么样的日期争论界面,但ga:week,ga:year应该为你提供一年中可以插入的一周并允许你适当地建立日期。在 PHP 中,它可能类似于以下内容:

<?php
// assuming use of the official PHP API for Google services, and
// that you've appropriately initialized the connection, and that
// you've chosen to "$client->setUserObjects(true)", and that you've
// initialized the necessary variables...
$results = $service->data_ga->get($analytics_id, $start_date, $end_date, 'ga:visitors',
    array('dimensions' => 'ga:week,ga:nthWeek,ga:year'));
$columns = array();
foreach ($results->columnHeaders as $headidx => $col) {
    $columns[$col->name] = $headidx;
}
$traffic = array();
foreach ($results->rows as $idx => $week) {
    $date = new DateTime;
    $date->setISODate($week[$columns['ga:year']], $week[$columns['ga:week']]);
    $traffic[$week[$columns['ga:nthWeek']]] = array('date' => $date, 'visitors' => $week[$columns['ga:visitors']], 'visits' => $week[$columns['ga:visits']], 'pageviews' => $week[$columns['ga:pageviews']]);
}
ksort($traffic);

// $traffic now holds a sorted array of your week by week traffic, with the full
// date in the DateTime object that you can access and format however you please
?>
于 2013-06-03T16:33:28.190 回答