2011-11-15 37 views
2

我想請求(使用gapi源代碼)從鏈接引用到用戶網站。例如,如果他們從Facebook上點擊,並且有多少推介通過。php for谷歌分析API(使用gapi獲得引用)

我的gapi代碼鏈接頁面查看日期目前看起來像這樣。

$ga = new gapi(ga_email,ga_password); 
$filter = 'country == United Kingdom && browser == Firefox || browser == Chrome'; 
$ga->requestReportData(ga_profile_id,array('browser','browserVersion'),array('pageviews','visits'),'-visits',$filter, $start_date=$date, $end_date=$date); 

,並回蕩在頁面

<?php echo $ga->getVisits() ?> 

我如何獲得的過濾器來顯示從第二回聲它通過頁面的列表refered?

回答

2

以下是我的腳本,其唯一目的是獲取引薦網址和總瀏覽量和訪問次數。我認爲這就是你想要的。

<?php 
define('ga_email','[email protected]'); 
define('ga_password','password'); 
define('ga_profile_id','profile_id'); 

require 'gapi.class.php'; 

$ga = new gapi(ga_email,ga_password); 

$filter = 'medium==referral && referralPath != /'; 

/*** // << add '/' to uncomment 
$date_start = '2011-11-01'; 
$date_end = '2011-11-13'; 
//**/$date_start = $date_end = null; 

$ga->requestReportData(
    ga_profile_id, 
    array('source','referralPath'),//what field you are looking for 
    array('pageviews','visits'),//what metric you want to calculate 
    '-visits',//sort order, prefix - means descending 
    $filter,//filter query 
    $date_start,//yyyy-mm-dd or null 
    $date_end,//yyyy-mm-dd or null 
    1,//offset lookup 
    100);//max result 
?> 
<table> 
<tr> 
    <th>Referral URL</th> 
    <th>Pageviews</th> 
    <th>Visits</th> 
</tr> 
<?php 
foreach($ga->getResults() as $result): 
?> 
<tr> 
    <td><?php echo $result->getSource() . $result->getReferralPath() ?></td> 
    <td><?php echo $result->getPageviews() ?></td> 
    <td><?php echo $result->getVisits() ?></td> 
</tr> 
<?php 
endforeach 
?> 
</table> 
+0

非常感謝你 – meohmy

+0

謝謝我一直在試圖做一段時間!真棒 – JasonDavis

+1

這是偉大的,但你知道如何使顯示一個特定頁面的頂級推介?我正在嘗試在WordPress的網站上爲每個博客文章頂部推薦頂級推薦人?我嘗試將我的過濾器更改爲這個...'$ filter ='pagePath ==/articles/50-web-developer-documentation-manuals-you-need-about-about-about/&& medium == referral && referralPath!= /';'但是,當我這樣做的時候,它似乎只顯示了一些結果,這是不準確的! – JasonDavis