簡體   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