2016-07-11 47 views
1

當使用Google Analytics API(PHP客戶端庫 - 作爲服務器服務器的服務器)多次返回空結果集時。 我的代碼如下所示:谷歌分析API有時會返回空結果

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('[email protected](), 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 [email protected] 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 [email protected]Details 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次),還惦記着一定的成效。

任何想法爲什麼以及如何解決它?

回答

1

你爲什麼每5分鐘發一次?

核心報告API中的數據在24-48小時內不穩定。在那之後,它永遠不會改變,所以假設你將它保存在某個地方,沒有理由再次請求它。

Data processing latency

處理等待時間是24-48小時。每天向Google Analytics發送超過200,000次會話的標準帳戶將導致報告 每天僅刷新一次。這可以延遲最多兩天的報告和 指標的更新。要恢復日內處理,請將您帳戶發送至<每天200,000次的會話數減少爲 。對於 高級帳戶,此限制每月可擴展至20億次點擊。

所以處理還沒有完成,這可能是爲什麼有時你沒有看到任何數據。

如果你需要實時信息,你應該使用real-time api

+1

感謝隊友,可能錯過了這部分。 – benjah