2012-10-09 55 views
0

我必須獲取有關我的Google Analytics目標的主要信息。GAPI輸出與Google Analytics網站不匹配

我使用GAPI lib下,使用此代碼:

<?php 
require_once 'conf.inc'; 
require_once 'gapi.class.php'; 

$ga = new gapi(ga_email,ga_password); 

$dimensions = array('pagePath', 'hostname'); 
$metrics = array('goalCompletionsAll', 'goalConversionRateAll', 'goalValueAll'); 

$ga->requestReportData(ga_profile_id, $dimensions, $metrics, 
     '-goalCompletionsAll', '', '2012-09-07', '2012-10-07', 1, 500); 
$gaResults = $ga->getResults(); 

foreach($gaResults as $result) 
{ 
    var_dump($result); 
} 

切這段代碼輸出:

object(gapiReportEntry)[7] 
    private 'metrics' => 
    array (size=3) 
     'goalCompletionsAll' => int 12031 
     'goalConversionRateAll' => float 206.93154454764 
     'goalValueAll' => float 0 
    private 'dimensions' => 
    array (size=2) 
     'pagePath' => string '/catalogs.php' (length=13) 
     'hostname' => string 'www.example.com' (length=13) 
object(gapiReportEntry)[6] 
    private 'metrics' => 
    array (size=3) 
     'goalCompletionsAll' => int 9744 
     'goalConversionRateAll' => float 661.05834464043 
     'goalValueAll' => float 0 
    private 'dimensions' => 
    array (size=2) 
     'pagePath' => string '/price.php' (length=10) 
     'hostname' => string 'www.example.com' (length=13) 

我在谷歌Analytics(分析)的網站上看到的網址目標頁面同期什麼的日期是:

Goal Completion Location Goal Completions Goal Value 
1. /price.php      9,396   $0.00 
2. /saloni.php      3,739   $0.00 

正如你可以看到輸出不匹配。爲什麼?怎麼了?

回答

1

您需要將目標表達式與pagePath匹配。首先你必須從這個網站下載gManagement擴展http://www.seerinteractive.com/blog/google-analytics-management-api-phpinterface-for-php。然後,按照此步驟:

1)下面的代碼從GAPI類添加到accountObjectMapper方法:

foreach ($entry->children('http://schemas.google.com/ga/2009')->goal as $goal){ 
    if ($goal->attributes()->active=='true'){ 
     $properties['name'] = strval($goal->attributes()->name); 
     $properties['number'] = strval($goal->attributes()->number); 
     foreach ($goal->children('http://schemas.google.com/ga/2009')->destination as $destination){ 
      $properties['expression'] = strval($destination->attributes()->expression); 
      $properties['matchType'] = strval($destination->attributes()->matchType); 
     } 
    } 

這必須是加載項循環中。

2)通過gManagement類獲取目標的表達和比對類型:

$gm = new gManagementApi($account, $password); 
     $profiles = $gm->requestAccountFeed('~all', '~all'); 

     $account_id = ''; 
     $property_id = ''; 

     foreach ($profiles as $profile) { 
      if ($profile->getProfileId() == $profile_id) { 
       $account_id = $profile->getAccountId(); 
       $property_id = $profile->getWebPropertyId(); 
       break; 
      } 
     } 

     $goals = $gm->requestAccountFeed($account_id, $property_id, '~all'); 

3)建立過濾器爲您PAGEPATH查詢:

$filter = ''; 
     foreach ($goals as $goal) { 
      if ($goal->getMatchType() == 'head') { 
       $filter .= "ga:pagePath=~^" . $goal->getExpression() . ","; 
      } else if ($goal->getMatchType() == 'exact') { 
       $filter .= "ga:pagePath==" . $goal->getExpression() . ","; 
      } else { 
       $filter .= "ga:[email protected]" . $goal->getExpression() . ","; 
      } 
     } 

     return substr($filter,0,-1); 

4)讓你這樣做將相同的查詢內置過濾器:

$ ga-> requestReportData(ga_profile_id,$ dimensions,$ metrics, '-goalCompletionsAll',$ filter,'2012-09-07','2012-10-07', 1,500);

這是很多步驟,但爲我工作。希望對你有效。

0

也確保你沒有取樣。如果您有以下情況,Google會自動進行採樣和推斷:a)在您查詢的時間段內有超過50,000次的觀看次數和b)沒有分析次數溢價

有一個簡單的解決方法,只需要一個循環查詢每天和結束循環遞增日期變量。

相關問題