Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php GAPI输出不';与谷歌分析网站不匹配_Php_Api_Google Analytics_Google Api - Fatal编程技术网

Php GAPI输出不';与谷歌分析网站不匹配

Php GAPI输出不';与谷歌分析网站不匹配,php,api,google-analytics,google-api,Php,Api,Google Analytics,Google Api,我必须获得有关我的谷歌分析目标的主要信息 我正在使用GAPI lib,代码如下: <?php require_once 'conf.inc'; require_once 'gapi.class.php'; $ga = new gapi(ga_email,ga_password); $dimensions = array('pagePath', 'hostname'); $metrics = array('goalCompletionsAll', 'goalConversionRateA

我必须获得有关我的谷歌分析目标的主要信息

我正在使用GAPI lib,代码如下:

<?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);
}
我在Google Analytics网站的Goals URL页面上看到的相同日期是:

    Goal Completion Location    Goal Completions    Goal Value
1.  /price.php                        9,396            $0.00
2.  /saloni.php                       3,739            $0.00

正如您所看到的,输出不匹配。为什么?怎么了?

您需要将目标表达式与页面路径匹配。你要做的第一件事就是从这个网站下载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\U配置文件\U id、$dimensions、$metrics、, “-goalCompletionsAll”,“$filter”,“2012-09-07”,“2012-10-07”,1500)


这是很多步骤,但对我来说很有效。希望它对您有效。

同时确保您没有采样。如果您在查询的时间段内有a)超过50000次的浏览量,并且b)没有analytics premium,谷歌会自动进行采样和推断

有一个简单的解决方法,就是有一个每天查询的循环,循环结束时增加日期变量

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