質問

これに2日間取り組んでおり、どこにも行かないようです。

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

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

私がやりたいのは、「過去7日間」から「PageViews」の数を取得することです。したがって、出力は次のようなものです。

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

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

役に立ちましたか?

解決

これはあなたを助けるはずです

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

 $gaEmail = 'youremail@email.com';
 $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段階の検証をオフにすることに留意してください。そうしないと、アカウント情報の有効性にもかかわらず、悪い要求エラーが発生します。

他のヒント

@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();

?>

最初の方法 トラフィックカウント あなたに与えるでしょう(PageViews、訪問、直帰率、サイトの時間の費用、新規訪問)

2番目の方法 referralCount あなたに与えます(紹介URLとそのURLからのヒットの総数)

3番目の方法 sourcecountnum (直接トラフィック、オーガニック、紹介、フィード、電子メールなど)などのトラフィックソースを提供します

最後の方法 sourcecountper ここで1つの違いを持つ3番目の情報と同じ情報を提供します。情報はパーセンテージになります。

それがいくつかの助けになることを願っています、そして、バグの場合に私に知らせてください。

  <?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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top