2011-05-01 227 views
6

我已經成功使用得到一些數據獲取特定數據:谷歌Analytics(分析)API:使用PHP

$ga->requestReportData(44030755,array('pagePath'),array('pageviews','uniquePageviews')); 和使用的foreach在它迭代。

這裏所返回的內容:

object(gapiReportEntry)[7] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '//[email protected]=' (length=19) 

object(gapiReportEntry)[12] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '//?facebookwww-static/assets/images/highlight.png=' (length=50) 

object(gapiReportEntry)[8] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '//?faktanyaadalah=&fb_xd_fragment=' (length=34) 

object(gapiReportEntry)[13] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '//?faktanyadalah=' (length=17) 

object(gapiReportEntry)[15] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '//?uniqpost=&fb_xd_fragment=' (length=28) 

object(gapiReportEntry)[14] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/adamramadhan?offset=0' (length=22) 

object(gapiReportEntry)[16] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/adamramadhan?offset=2' (length=22) 

object(gapiReportEntry)[18] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/adamramadhan?offset=3' (length=22) 

object(gapiReportEntry)[19] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/adamramadhan?offset=4' (length=22) 

object(gapiReportEntry)[20] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/admin/blog?e=2' (length=15) 

object(gapiReportEntry)[21] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/admin/blog?e=4' (length=15) 

object(gapiReportEntry)[22] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/admin/blog?e=5' (length=15) 

object(gapiReportEntry)[23] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/blog?id=23' (length=11) 

object(gapiReportEntry)[24] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/gembelabis' (length=11) 

object(gapiReportEntry)[25] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/messages?id=7' (length=14) 

object(gapiReportEntry)[26] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/messages?id=9' (length=14) 

object(gapiReportEntry)[27] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/messages?mid=15' (length=16) 

object(gapiReportEntry)[28] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/messages?mid=17' (length=16) 

object(gapiReportEntry)[29] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/messages?mid=18' (length=16) 

object(gapiReportEntry)[30] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/product?id=16' (length=14) 

object(gapiReportEntry)[31] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/product?id=18' (length=14) 

object(gapiReportEntry)[32] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/product?id=2' (length=13) 

object(gapiReportEntry)[33] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/product?id=44' (length=14) 

object(gapiReportEntry)[34] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/products?tag=hosting' (length=21) 

object(gapiReportEntry)[35] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/verify/adamramadhan' (length=20) 

object(gapiReportEntry)[36] 
    private 'metrics' => 
    array 
     'pageviews' => int 1 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/verify/uniqpost' (length=16) 

object(gapiReportEntry)[37] 
    private 'metrics' => 
    array 
     'pageviews' => int 2 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '//?facebookwww-static/assets/images/highlight-1.png=' (length=52) 

object(gapiReportEntry)[38] 
    private 'metrics' => 
    array 
     'pageviews' => int 2 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/LVUStore' (length=9) 

object(gapiReportEntry)[39] 
    private 'metrics' => 
    array 
     'pageviews' => int 2 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/adamramadhan?offset=1' (length=22) 

object(gapiReportEntry)[40] 
    private 'metrics' => 
    array 
     'pageviews' => int 2 
     'uniquePageviews' => int 1 
    private 'dimensions' => 
    array 
     'pagePath' => string '/product?id=22' (length=14) 
  1. 我怎樣才能頁面訪問量,並從uniqpageviews只使用一個自定義頁面路徑?像$eachdata->path('/LVUstore');返回數組pageview 2,uniqpageview 1等

  2. 是否有任何最佳實踐?例如緩存對象等

感謝,

回答

4

我想你在找什麼是過濾器。您可以使用filter參數來限制結果集。谷歌也表示這是處理大型數據集的好方法。所以,基本上你可以用篩選器截取你想要的特定路徑,或者你可以抓住它們並相應地組織它們。

$path = "/path-goes-here"; 

/* 
* if you only want to get one path, use a filter: 
*/ 

$filter = "pagePath == '$path'"; 

$ga->requestReportData(ga_profile_id,array('pagePath'), 
    array('pageviews','uniquePageviews'),'',$filter); 

$result = $ga->getResults(); 
if (count($result)) { 
    $result = array('pageviews' => $result[0]->getPageviews(), 
     'unique_pageviews' => $result[0]->getUniquePageviews() 
    ); 
} 
print_r($result); 

/* 
* of if you want to get them all and then grab one specific: 
*/ 

$ga->requestReportData(ga_profile_id,array('pagePath'),array('pageviews','uniquePageviews')); 

$results = array(); 
foreach($ga->getResults() as $result) { 
    $results[$result->getPagePath()] = array('pageviews' => $result->getPageviews(), 
     'unique_pageviews' => $result->getUniquePageviews()); 
} 

if(array_key_exists($path,$results)) { 
print_r($results[$path]); 
} 
+0

我怎麼能得到$路徑? – 2016-08-22 11:30:51

0

這將返回只要你想格式化的數組(如果我的理解)

function cleanTheData($what){ 
    $ret = array(); 
    foreach($what as $one){ 
     $ret[$uno->dimensions['pagePath']] = array(
      'pageviews'  => $uno->metrics['pageviews'], 
      'uniquePageviews' => $uno->metrics['uniquePageviews'] 
     ); 
    } 
    return $ret; 
} 

print_r(cleanTheData($initialArray)) 
相關問題