Google报告api不是按时间顺序排列的

时间:2018-02-13 21:55:02

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

我在我的对象中使用此代码来获取Google AnalyticsAPI数据

$this->end_date = date('Y-m-d', strtotime('-1 days'));
$this->start_date = date('Y-m-d', strtotime('-27 days', strtotime($this->end_date)));

$this->ga->requestReportData(xxxx,array('date'), array('sessions'), null, '', $this->start_date, $this->end_date);

foreach($this->ga->getResults() as $result) {
  $this->date = $result->getDate();
  $this->visits = $result->getSessions();

  $this->date = strtotime($this->date);
  $this->date = date("d-m-Y", $this->date);
  $i++;

  $this->dateArray .= "{date: '". $this->date ."', visits: ". $this->visits . $i ."},";

}

$this->dateArray =  substr($this->dateArray, 0, -1);

出于某种原因某些日期不合适,我做错了什么或这是正常的,是否有不同类型的修复?

这是$ this-> ga-> getResults()

的输出
Array ( [20180117] => 0 [20180118] => 0 [20180119] => 0 [20180120] => 0 [20180121] => 0 [20180122] => 0 [20180124] => 0 [20180125] => 0 [20180127] => 0 [20180128] => 0 [20180129] => 0 [20180131] => 0 [20180202] => 0 [20180204] => 0 [20180207] => 0 [20180208] => 0 [20180209] => 0 [20180210] => 0 [20180212] => 0 [20180126] => 1 [20180201] => 1 [20180203] => 1 [20180205] => 1 [20180211] => 1 [20180123] => 3 [20180206] => 5 [20180213] => 5 [20180130] => 31 )

1 个答案:

答案 0 :(得分:1)

除非您专门使用sort operator,否则Google Analytics报告API并不保证对结果进行排序。

尝试在您的请求中添加sort=ga:date,这应该有效。

或者,编写代码以假设数据未排序,并自行处理排序。