繁体   English   中英

Google Analytics PHP API(GAPI) - 获取页面查看次数

[英]Google Analytics PHP API (GAPI) - Getting number of page views

现在已经工作了两天,似乎无处可去。

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

我是谷歌分析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,我们可以创建应用程序专用密码。

就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将为您提供(PageViews,访问次数,跳出率,网站时间花费,新访问次数)

第二种方法referralCount会给你(推荐网址和该网址的总点击次数)

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

最后一个方法sourceCountPer将提供与第三个相同的信息,其中一个区别在于信息将以百分比形式显示。

希望它会有所帮助,如果有任何错误,请告诉我。

  <?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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM