Google Analytics API:使用 php 获取特定数据

发布于 2024-11-04 01:28:23 字数 7515 浏览 2 评论 0原文

我设法使用

以下方法获取一些数据: $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 '//?@faktanyaadalah=' (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. 有什么最佳实践吗?例如缓存对象等

谢谢,

I've managed to get some data using:

$ga->requestReportData(44030755,array('pagePath'),array('pageviews','uniquePageviews'));
and iterating over it using foreach.

Here's what's returned:

    object(gapiReportEntry)[7]
  private 'metrics' => 
    array
      'pageviews' => int 1
      'uniquePageviews' => int 1
  private 'dimensions' => 
    array
      'pagePath' => string '//?@faktanyaadalah=' (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. How can i get page views, and uniqpageviews from just using a custom page path? something like $eachdata->path('/LVUstore'); returns an array pageview 2, uniqpageview 1, etc

  2. Are there any best practices? e.g. caching the object etc

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

壹場煙雨 2024-11-11 01:28:23

我认为您正在寻找的是过滤器。您可以使用 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]);
}

I think what you are looking for are filters. You can use the filter parameter to limit your result set. Google also says this is a good way to deal with large datasets. So, essentially you can just snag the one specific path that you want with a filter, or you can grab them all and organize them accordingly.

$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]);
}
夏雨凉 2024-11-11 01:28:23

这将返回一个按照您想要的格式格式化的数组(如果我理解的话)

 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))   

this returns an array formated as you want (if I had understood)

 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))   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文