0

Google アナリティクスの目標に関する主な情報を取得する必要があります。

私はこのコードで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 ウェブサイトの目標 URL ページに表示されるのは次のとおりです。

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

ご覧のとおり、出力は一致しません。なんで?どうしたの?

4

2 に答える 2

1

ゴール式を 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 クラスを使用して、ゴール式と 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->requestReportData(ga_profile_id, $dimensions, $metrics, '-goalCompletionsAll', $filter, '2012-09-07', '2012-10-07', 1, 500);

それは多くのステップですが、私にとってはうまくいきました。それがうまくいくことを願っています。

于 2012-10-18T17:33:16.603 に答える