GAPI输出与Google Analytics网站不符

时间:2012-10-09 18:41:41

标签: php api google-analytics google-api

我必须获取有关我的Google Analytics目标的主要信息。

我正在使用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);
}

剪切此代码输出:

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

如您所见,输出不匹配。为什么?怎么了?

2 个答案:

答案 0 :(得分:1)

您需要将目标表达式与pagePath匹配。首先,您必须从此网站http://www.seerinteractive.com/blog/google-analytics-management-api-phpinterface-for-php下载g管理扩展程序。然后按照以下步骤操作:

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

这必须在Load Entry循环中。

2)通过gManagement类获取目标表达式和matchType:

$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)为您的PagePath查询构建过滤器:

$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-&gt; requestReportData(ga_profile_id,$ dimensions,$ metrics,       &#39; -goalCompletionsAll&#39;,$ filter,&#39; 2012-09-07&#39;,&#39; 2012-10-07&#39;,1,500);

它有很多步骤,但对我有用。希望它适合你。

答案 1 :(得分:0)

还要确保您没有抽样。如果您在查询的时间段内拥有a)超过50,000次观看,并且b)没有分析费用,则Google会自动进行抽样和推断

有一个简单的解决方法,只需要一个循环查询每天,并在循环结束时递增日期变量。