在Google AnalyticsAPI中获取功能 [英] Get function in Google Analytics API

查看:203
本文介绍了在Google AnalyticsAPI中获取功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function getResults(

)我试图使用分析API获取一些数据,我的例子是这样的: & $ analytics,$ profileId){
//调用Core Reporting API并查询最近七天的会话数
//的数量。
return $ analytics-> data_ga-> get(
'ga:'。$ profileId,
'7daysAgo',
'今天',
'ga :会话);
}

并且在Analytics.php文件中的函数是:

  public function get($ ids,$ metrics,$ optParams = array())
{
$ params = array( 'ids'=> $ ids,'metrics'=> $ metrics);
$ params = array_merge($ params,$ optParams);
return $ this-> call('get',array($ params),Google_Service_Analytics_RealtimeData);


code


$ b

如何修改该示例以便沿着某些尺寸返回与会话,例如,pagePath?



谢谢 解决方案

所以这个问题不是很清楚,但是您的问题的第一部分是正确的,该示例有效,并且是从Google Analytics API获取数据的方式。然而,您不需要触摸或修改Analytics.php。



以下是您的代码的样子:

  $ ga_profile_id = xxxxxxx; //插入你的
$ from = date('Y-m-d',time() - 2 * 24 * 60 * 60); //最近2天
$ to = date('Y-m-d'); //今天

$ metrics ='ga:访问,ga:访客,ga:网页浏览';
$ dimensions ='ga:date';
$ sort =-ga:visits;
$ data = $ service-> data_ga-> get('ga:'。$ ga_profile_id,$ from,$ to,$ metrics,array('dimensions'=> $ dimensions,'sort'= > $排序));

这些都是您开始所需的基本元素。访问 https://developers.google.com/analytics/devguides/ reporting / core / v3 / common-queries 查看常见查询配方列表。



Analytics API查询浏览器 API查询浏览器 https://ga-dev-tools.appspot.com/query-资源管理器/ )是一个很好的玩法,发现度量和维度名称。例如,您会发现Page Path的 ga:pagePath 然后,例如,如果您希望按页面路径获取访问次数和综合浏览量,则只需在代码中插入正确的参数,你会得到如下所示的内容:

  $ ga_profile_id = xxxxxx; //在这里插入你的
$ from = date('Y-m-d',time() - 2 * 24 * 60 * 60); //最近2天
$ to = date('Y-m-d'); // today

$ metrics ='ga:visits,ga:pageviews';
$ dimensions ='ga:pagePath';
$ sort =-ga:visits;
$ data = $ service-> data_ga-> get('ga:'。$ ga_profile_id,$ from,$ to,$ metrics,array('dimensions'=> $ dimensions,'sort'= > $排序));

其中的基本含义:
获取指标访问次数和综合浏览量 ,使用页面路径作为尺寸,并按访问次数对其进行排序 - 最后2天!希望这一切都有道理。


I'm trying to fetch some data using the analytics API, the example i have is this:

function getResults(&$analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions');
}

and the function in the Analytics.php file is:

public function get($ids, $metrics, $optParams = array())
  {
    $params = array('ids' => $ids, 'metrics' => $metrics);
    $params = array_merge($params, $optParams);
    return $this->call('get', array($params), "Google_Service_Analytics_RealtimeData");
  }
}

How do I adapt that example to return some dimensions along with the sessions, for example, pagePath?

Thanks

解决方案

So the question is little unclear, but the first part of your question is correct, that example works and is the way to get data from the Google Analytics API. You do NOT need to touch or modify Analytics.php however.

Here is what your code should look like:

$ga_profile_id = xxxxxxx; // insert yours
$from = date('Y-m-d', time()-2*24*60*60); // last 2 days
$to = date('Y-m-d'); // today

$metrics = 'ga:visits,ga:visitors,ga:pageviews';
$dimensions = 'ga:date';
$sort = "-ga:visits";
$data = $service->data_ga->get('ga:'.$ga_profile_id, $from, $to, $metrics, array('dimensions' => $dimensions,'sort'=>$sort));

These are all the basic elements you need to get started. Visit https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries for a list of Common Query recipes. Replace the metrics, dimensions and sort parameters in my example above with the ones listed there to run the common report scenarios they cover.

The Analytics API Query explorer (https://ga-dev-tools.appspot.com/query-explorer/) is a great to play around and discover the metric and dimension names. For example, you'll find that that the dimension for Page Path is: ga:pagePath.

So then, for example, if you want to get visits and pageviews by page path, you simply insert the correct parameters in the code, and you get something that looks like this:

$ga_profile_id = xxxxxx; //insert yours here
$from = date('Y-m-d', time()-2*24*60*60); // last 2 days
$to = date('Y-m-d'); // today

$metrics = 'ga:visits,ga:pageviews';
$dimensions = 'ga:pagePath';
$sort = "-ga:visits";
$data = $service->data_ga->get('ga:'.$ga_profile_id, $from, $to, $metrics, array('dimensions' => $dimensions,'sort'=>$sort));

Which basically means: Get the metrics visits and pageviews, using page path as the dimension, and sort it by visits - for the last 2 days! Hopefully this all makes sense.

这篇关于在Google AnalyticsAPI中获取功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆