Google Analytics PHP API(GAPI)-获取页面浏览量

3

我已经在做这个项目两天了,但貌似一点进展也没有。

我正在使用GAPI Google analytics PHP类。以下是我目前的代码:

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

我想要做的是获取过去七天内的“页面浏览量”数量。因此输出结果应该类似于:
<?php foreach($ga->getResults() as $result) { ?>
    Date: <?php echo $result; ?>
    Page Views: <?php echo $result->getPageviews(); ?>
<?php } ?>

我刚接触Google Analytics API,不太清楚该从哪里开始。谢谢任何帮助!

3个回答

9

这应该会对你有所帮助

   <?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 账户的两步验证。如果您没有关闭,尽管您的账户信息有效,但它会抛出一个错误的请求错误。


嗨,我是谷歌分析的新手,请问你能告诉我在哪里可以关闭双重验证吗? - Sonal Khunt
没有必要关闭两步验证,因为您可以为您的应用程序设置一个自定义密码。 - ashraf mohammed
你从哪里获取到 gapi.class.php 文件的? - jhnferraris

1

想要补充一下 @ladiesMan217 的话,如果我们开启了两步验证,我们可以创建应用程序专用密码。

就 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将为您提供(页面浏览量,访问量,跳出率,网站停留时间,新访客)

第二种方法referralCount将为您提供(引荐网址和来自该网址的总点击次数)

第三种方法sourceCountNum将为您提供流量来源,如(直接流量,有机流量,引荐流量,Feed,电子邮件和其他)

最后一种方法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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接