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

16,591

Solution 1

This should help you

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

 $gaEmail = '[email protected]';
 $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; 
  }

 ?>

Keep in mind to turn off your 2-step verification for the google account. If you don't , it will throw you a bad request error despite the validity of your account info.

Solution 2

  <?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>

Solution 3

Will like to make an addition to @ladiesMan217 , we can create application specific passwords if we have 2 steps verification on.

As far as GAPI is concerned i have created a class which will give lot of information but by using couple of methods. You can download the class here 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();

?>

First Method trafficCount will give you ( PageViews , Visits, Bounce Rate, Time spend of site, New Visits )

Second Method referralCount will give you ( referral url and total number of hits from that url )

Third Method sourceCountNum will provide you traffic source like ( Direct Traffic, Organic ,Referral, Feed, Emails and Others )

Last Method sourceCountPer will provide same info as the 3rd one with one difference here the information will be in Percentage.

Hope it will be of some help and please let me know in case of any bugs.

Share:
16,591
Allister
Author by

Allister

Updated on June 05, 2022

Comments

  • Allister
    Allister almost 2 years

    been working on this for two days now and seem to be getting nowhere.

    I am using the GAPI Google analytics PHP class. This is the current code I have the now:

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

    What I want to do is get the number of "pageviews" from the "past 7 days". So output would be something like:

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

    I am new to Google analytics API so not sure where to start. Thanks for any help!

  • Sonal Khunt
    Sonal Khunt almost 12 years
    Hi, i new to google analytics, can you please tell me where we can turn off 2 step verification
  • ashraf mohammed
    ashraf mohammed over 10 years
    there is no need to turn off two steps verification because you can use a (custom password) for your app.
  • jhnferraris
    jhnferraris over 10 years
    Where did you get your gapi.class.php?