【问题标题】:Google Analytics PHP API (GAPI) - Getting number of page viewsGoogle Analytics PHP API (GAPI) - 获取页面浏览量
【发布时间】:2014-01-28 21:15:31
【问题描述】:

现在已经为此工作了两天,但似乎无济于事。

我正在使用 GAPI Google 分析 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 分析 API 的新手,所以不知道从哪里开始。感谢您的帮助!

【问题讨论】:

    标签: php google-analytics google-analytics-api


    【解决方案1】:

    这应该对你有帮助

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

    【讨论】:

    • 嗨,我是谷歌分析的新手,你能告诉我在哪里可以关闭两步验证
    • 无需关闭两步验证,因为您可以为您的应用使用(自定义密码)。
    • 你的gapi.class.php是从哪里得到的?
    【解决方案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 将为您提供(页面浏览量、访问量、跳出率、网站花费时间、新访问量)

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

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

    最后一个方法 sourceCountPer 将提供与第 3 个相同的信息,但有一个区别,此处信息将以百分比表示。

    希望对您有所帮助,如有任何错误请告诉我。

    【讨论】:

      【解决方案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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多