Google 分析 API 有时 return 空结果

Google analytics API sometimes return empty results

当使用 Google Analytics API(PHP 客户端库 - 作为服务器到服务器的服务)很多时候它 returns 空结果集。 我的代码如下所示:

public function getLeadDetails($view, $gauid, $lead_id,$refresh_token,$dates = [])
{
    Log::info("*****************GA Service - GAUID: ".$gauid." **********************************");
    $options['dimensions'] = implode(',',config('analytics.dimensions'));
    $options['filters'] = "ga:eventAction==".$gauid;
    $metrics = "ga:users";
    $view = "ga:".$view;
    $yesterday = date('Y-m-d',strtotime('yesterday'));
    $tomorrow = date("Y-m-d",strtotime('tomorrow'));
    if(count($dates)){
        $yesterday = $dates['yesterday'];
        $tomorrow = $dates['tomorrow'];
    }
    Log::info('GA_Service@getLeadDetails(), Request Params: ?ids='.$view.'&start-date='.$yesterday.'&end-date='.$tomorrow.'&metrics='.$metrics.'&dimensions='.$options['dimensions'].'&filters='.$options['filters']);
    try{
        $this->client->setAccessType('offline');
        $this->client->setAccessToken($refresh_token);
        $this->client->setClientId(config('analytics.client_id'));
        $this->client->setScopes([
            \Google_Service_Analytics::ANALYTICS,
            \Google_Service_Analytics::ANALYTICS_EDIT,
        ]);
        $analytics = new \Google_Service_Analytics($this->client);
        $data = $analytics->data_ga->get($view,$yesterday,$tomorrow,$metrics,$options);
        if($data['totalResults'] && $data['totalResults'] > 0){
            Log::info('Inside GA_Service@getLeadDetails has totalResults');
            $rtn = [];
            $i = 0;
            foreach ($data['rows'] as $row) {
                $rtn['uacid'] = $gauid;
                $rtn['user_type'] = $row[0];
                $rtn['device_category'] = $row[1];
                $rtn['source'] = $row[2];
                $rtn['medium'] = $row[3];
                $rtn['campaign'] = $row[4];
                $rtn['ad_group'] = $row[5];
                $rtn['path'] = $row[6];
                $i++;
            }
            Log::info('Inside GA_Service@getLeadDetails lead '.$lead_id.' should be update');
            return $this->insertLeadDetails($rtn , $lead_id);
        }
        Log::info('GA_Service::getLeadDetails() returned empty results for lead: '.$lead_id);
        if(!count($dates)){
            $this->throwException('empty results for lead: '.$lead_id.' in GA_Service::getLeadDetails()',275);
            return false;
        }
        return 'empty results for lead: '.$lead_id.' in GA_Service::getLeadDetails()';
    }catch (\Google_Service_Exception $e){
        Log::info('Google service exception from GA_Service::getLeadDetails() - '. $e->getErrors()[0]['message']);
        $this->throwException($e->getErrors()[0]['message'], 335);
    }
    return false;
}

如果结果为空(5 次)并且仍然遗漏一些结果,则此函数每 5 分钟再次触发。

知道为什么以及如何解决它吗?

你为什么每 5 分钟发射一次?

核心报告中的数据 API 不会在 24 - 48 小时内保持稳定。在那之后它永远不会改变,所以假设您将它保存在某个地方就没有理由再次请求它。

Data processing latency

Processing latency is 24-48 hours. Standard accounts that send more than 200,000 sessions per day to Analytics will result in the reports being refreshed only once a day. This can delay updates to reports and metrics for up to two days. To restore intra-day processing, reduce the number of sessions your account sends to < 200,000 per day. For Premium accounts, this limit is extended to 2 billion hits per month.

所以处理尚未完成,这可能就是为什么有时您看不到任何数据的原因。

如果您想要实时信息,您应该使用 real-time api