1

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

$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 )
4

1 回答 1

1

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

尝试添加sort=ga:date到您的请求中,这应该可以。

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

于 2018-02-14T02:16:17.040 回答