2012-01-15 14 views
3

これで2日間作業していて、どこにもいないようです。GoogleアナリティクスPHP API(GAPI) - ページビュー数を取得する

私はGAPI Google analytics PHPクラスを使用しています。これは私が今持っている現在のコードです:私は何をしたいか

$ga->requestReportData("[UID]",array('day'),array('visits'), array("day")); 

は「過去7日間」から「ページビュー」の数を取得することです。出力は次のようになります:

<?php foreach($ga->getResults() as $result) { ?> 
    Date: <?php echo $result; ?> 
    Page Views: <?php echo $result->getPageviews(); ?> 
<?php } ?> 

私はGoogle Analytics APIを初めて使うので、どこから始めたらいいのか分かりません。助けてくれてありがとう!

答えて

9

これは、あなたが

<?php 
    require 'gapi.class.php'; 

$gaEmail = '[email protected]'; 
$gaPassword = 'your password'; 
$profileId = 'your profile id'; 

$dimensions = array('pagePath','country', 'region', 'city'); 
$metrics = array('visits'); 
$sortMetric=null; 
$filter=null; 
$startDate='2011-02-01'; 
$endDate='2011-02-28'; 
$startIndex=1; 
$maxResults=10000; 

$ga = new gapi($gaEmail, $gaPassword); 

$ga->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter,  $startDate, $endDate, $startIndex, $maxResults); 

$totalPageviews = $ga->getPageviews(); 

foreach ($ga->getResults() as $result) { 
    $visits = $result->getVists(); 
    print $visits; 
    } 

?> 

のGoogleアカウントの2段階認証プロセスをオフにすることを覚えておいてください役立つはずです。そうしないと、アカウント情報の有効性にもかかわらず、悪いリクエストエラーが発生します。

+0

こんにちは、私たちはあなたの[Googleアカウント](https://www.google.com/settings/)ページで2段階認証プロセス –

+0

をオフにすることができますどこで教えてくださいすることができ、分析をGoogleに新しいです。 – jmishra

+0

アプリに(カスタムパスワード)を使用できるため、2つのステップの確認をオフにする必要はありません。 –

1

@ ladiesMan217に追加したいのですが、2段階の検証がある場合は、アプリケーション固有のパスワードを作成できます。

GAPIに関する限り、たくさんの情報を与えるクラスを作成しましたが、いくつかのメソッドを使用しています。あなたはここにクラスhttp://www.thetutlage.com/post=TUT217

<?php 
error_reporting(0); // it is important as filtering tend to leave some unwanted errors 
include_once('class.analytics.php'); 
define('ga_email','your_analytics_email'); 
define('ga_password','your_analytics_password'); 
define('ga_profile_id','your_analytics_profile_id'); 

// Start date and end date is optional 
// if not given it will get data for the current month 
$start_date = '2012-05-28'; 
$end_date = '2012-06-27'; 

$init = new fetchAnalytics(ga_email,ga_password,ga_profile_id,$start_date,$end_date); 

$trafficCount = $init->trafficCount(); 
$referralTraffic = $init->referralCount(); 
$trafficCountNum = $init->sourceCountNum(); 
$trafficCountPer = $init->sourceCountPer(); 

?>

まずメソッドtrafficCount(ページビュー、訪問、直帰率、サイトのタイム支出、新規セッション)あなたを与えるだろう

第2の方法をダウンロードすることができますreferralCount(あなたの紹介URLとそのURLからの総ヒット数)

第3の方法sourceCountNumは(ノーリファラー、オーガニック、紹介、フィード、電子メールやその他)のようにあなたのトラフィックソースを提供します

最後の方法、ここで一つの違いパーセンテージになります情報と第三のものと同じ情報を提供しますsourceCountPer

ご迷惑をおかけして申し訳ございませんが、ご報告ください。

1
<?php 
    define('ga_email','you email'); 
    define('ga_password','passworkd'); 
    define('ga_profile_id','profile ID or View ID'); 

    require 'gapi.class.php'; 

    // pars to pass on Google Server Analytic Api 

    $start_date='2013-12-01'; 
    $end_date='2013-12-31'; 

    $ga = new gapi(ga_email,ga_password); 

    try { 

     $ga->requestReportData(ga_profile_id, 
     array('browser','browserVersion'), 
     array('pageviews','visits','visitors','visitBounceRate'), 
     $sort_metric=null, $filter=null, 
     $start_date,$end_date, 
     $start_index=1, $max_results=30); 

    } catch (Exception $e) { 
     echo 'Caught exception: ', $e->getMessage(), "\n"; 
    } 

    ?> 
    <table width='60%'> 
    <tr style="background-color:#00ff00;"> 
     <th>Browser &amp; Browser Version</th> 
     <th>Page Views</th> 
     <th>Visits</th> 
     <th>Visitors</th> 
     <th>Visit Bounce Rate</th> 

    </tr> 
    <?php 
    $i = 0; 
    foreach($ga->getResults() as $result): 
     //$ga->printfs($result); 
     if($i%2 == 0) $color = "#d3d3d3"; 
     else $color = "#FFFFF"; 
    ?> 
    <tr style="background-color:<?php echo $color ?>"> 
     <td><?php echo $result ?></td> 
     <td><?php echo $result->getPageviews() ?></td> 
     <td><?php echo $result->getVisits() ?></td> 
     <td><?php echo $result->getVisitors() ?></td> 
     <td><?php echo $result->getVisitBounceRate() ?></td> 

    </tr> 
    <?php 
    $i++; 
    endforeach 
    ?> 
    </table> 

    <table> 
    <tr> 
     <th>Total Results</th> 
     <td><?php echo $ga->getTotalResults() ?></td> 
    </tr> 
    <tr> 
     <th>Total Page views</th> 
     <td><?php echo $ga->getPageviews() ?> 
    </tr> 
    <tr> 
     <th>Total Visits</th> 
     <td><?php echo $ga->getVisits() ?></td> 
    </tr> 
    <tr> 
     <th>Total Visitors</th> 
     <td><?php echo $ga->getVisitors() ?></td> 
    </tr> 
    <tr> 
     <th>Visit Bounce Rate</th> 
     <td><?php echo $ga->getVisitBounceRate() ?></td> 
    </tr> 
    <tr> 
     <th>Results Updated</th> 
     <td><?php echo $ga->getUpdated() ?></td> 
    </tr> 
    </table> 
関連する問題