php - Google Analytics PHP API(GAPI)-获取直接流量数据

标签 php google-analytics google-analytics-api

我是Google Analytics(分析)的新手,但仍能找到解决方法。我正在尝试找到一种方法来在仪表板上的直接流量(流量来源->来源->下)下检索数据(着陆页,访问次数,页面/访问次数,平均访问持续时间,新访问百分比,跳出率)直接)。我试过通过使用define('filter', 'source == direct');作为their website states对其进行过滤,但是没有运气。我看到过类似的帖子here,但我想加深搜索范围。我想念什么吗?

我当前的代码只能从所有流量中检索数据。

require_once('gapi.class.php');

define('email', 'email address');
define('password', 'password');
define('profileID', profileID);
define('dimensions', 'source');
define('metrics', 'visits');
define('sortMetric', '-visits');
define('filter', null);
define('startDate', '2013-01-18');
define('endDate', '2013-02-17');
define('startIndex', 1);
define('maxResult', 10);

$ga = new gapi(email, password);
$ga->requestReportData(profileID,dimensions,metrics,sortMetric,filter,startDate,endDate,startIndex,maxResult);

foreach($ga->getResults() as $result){
    echo '<strong>'.$result.'</strong><br />';
    echo 'Source: ' . $result->getSource() . ' ';
    echo 'Visits: ' . $result->getVisits() . '<br /><br />';
}

最佳答案

我找到了答案,只是想与大家分享,以防万一有人遇到相同的问题。

基本上,我的错误是我没有用括号direct()引起来。我只是将define('filter', 'source == direct');替换为define('filter', 'source==(direct)');和中提琴!我得到我想要的。

这是我的代码,以防万一您需要它,您可能会注意到,我已经替换了维度和指标,因为我需要它们成为数组。

require_once('gapi.class.php');

define('email', 'email address');
define('password', 'password');
define('profileID', profileID);
$dimensions = array('landingPagePath');
$metrics = array('visits','pageviewsPerVisit','avgTimeOnSite','percentNewVisits','visitBounceRate');
define('sortMetric', '-visits');
define('filter', 'ga:source==(direct)');
define('startDate', '2013-01-18');
define('endDate', '2013-02-17');
define('startIndex', 1);
define('maxResult', 10);

$ga = new gapi(email, password);
$ga->requestReportData(profileID,$dimensions,$metrics,sortMetric,filter,startDate,endDate,startIndex,maxResult);

关于php - Google Analytics PHP API(GAPI)-获取直接流量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14953919/

相关文章:

PHP 循环 X 次

php - 面向 Web 的语言 : C-like syntax, 强静态类型。任何人?

javascript - Google 广告管理系统 (DFP) 中是否会阻止分析事件?

google-analytics - 有没有办法阻止 AMP Analytics 将开发工作计为点击?

google-analytics - 如何在 Google Analytics 中创建自定义数据源

php - 我可以在声明 PHP 类常量时使用 storage_path() 吗?

php - 如何使用 google chart api 在 x 轴上使用日期

Javascript:如何阻止谷歌分析?

javascript - 如何禁用谷歌分析调试js?

java - Google AnalyticsService 抛出 java.lang.reflect.UndeclaredThrowableException