我必须获取有关我的Google Analytics目标的主要信息。
我正在使用 GAPI 库,代码如下:
<?php
require_once 'conf.inc';
require_once 'gapi.class.php';
$ga = new gapi(ga_email,ga_password);
$dimensions = array('pagePath', 'hostname');
$metrics = array('goalCompletionsAll', 'goalConversionRateAll', 'goalValueAll');
$ga->requestReportData(ga_profile_id, $dimensions, $metrics,
'-goalCompletionsAll', '', '2012-09-07', '2012-10-07', 1, 500);
$gaResults = $ga->getResults();
foreach($gaResults as $result)
{
var_dump($result);
}
剪切此代码输出:
object(gapiReportEntry)[7]
private 'metrics' =>
array (size=3)
'goalCompletionsAll' => int 12031
'goalConversionRateAll' => float 206.93154454764
'goalValueAll' => float 0
private 'dimensions' =>
array (size=2)
'pagePath' => string '/catalogs.php' (length=13)
'hostname' => string 'www.example.com' (length=13)
object(gapiReportEntry)[6]
private 'metrics' =>
array (size=3)
'goalCompletionsAll' => int 9744
'goalConversionRateAll' => float 661.05834464043
'goalValueAll' => float 0
private 'dimensions' =>
array (size=2)
'pagePath' => string '/price.php' (length=10)
'hostname' => string 'www.example.com' (length=13)
我在Google Analytics网站上的目标网址页面上看到的具有相同日期的是:
Goal Completion Location Goal Completions Goal Value
1. /price.php 9,396 $0.00
2. /saloni.php 3,739 $0.00
如您所见,输出不匹配。为什么?怎么了?
将目标表达式与 pagePath 匹配。 首先,您必须做的是 http://www.seerinteractive.com/blog/google-analytics-management-api-phpinterface-for-php 从此站点下载gManagement扩展。 然后按照以下步骤操作:
1) 将以下代码添加到 gapi 类的 accountObjectMapper 方法中:
foreach ($entry->children('http://schemas.google.com/ga/2009')->goal as $goal){
if ($goal->attributes()->active=='true'){
$properties['name'] = strval($goal->attributes()->name);
$properties['number'] = strval($goal->attributes()->number);
foreach ($goal->children('http://schemas.google.com/ga/2009')->destination as $destination){
$properties['expression'] = strval($destination->attributes()->expression);
$properties['matchType'] = strval($destination->attributes()->matchType);
}
}
这必须在加载输入循环内。
2) 通过 gManagement 类获取目标表达式和匹配类型:
$gm = new gManagementApi($account, $password);
$profiles = $gm->requestAccountFeed('~all', '~all');
$account_id = '';
$property_id = '';
foreach ($profiles as $profile) {
if ($profile->getProfileId() == $profile_id) {
$account_id = $profile->getAccountId();
$property_id = $profile->getWebPropertyId();
break;
}
}
$goals = $gm->requestAccountFeed($account_id, $property_id, '~all');
3) 为页面路径查询构建过滤器:
$filter = '';
foreach ($goals as $goal) {
if ($goal->getMatchType() == 'head') {
$filter .= "ga:pagePath=~^" . $goal->getExpression() . ",";
} else if ($goal->getMatchType() == 'exact') {
$filter .= "ga:pagePath==" . $goal->getExpression() . ",";
} else {
$filter .= "ga:pagePath=@" . $goal->getExpression() . ",";
}
}
return substr($filter,0,-1);
4) 添加构建的过滤器时,进行相同的查询:
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, '-goalCompletionsAll', $filter, '2012-09-07', '2012-10-07', 1, 500);
这是很多步骤,但对我有用。 希望它对你有用。
还要确保你没有采样。Google 会自动抽样和推断,如果您 a) 在您查询的时间段内有超过 50,000 次观看次数,并且 b) 没有分析溢价
有一个简单的解决方法,只需有一个每天查询的循环,并在循环结束时递增日期变量。