logo

Sue Feng Design

â€č Back to blog

Using Google Analytics and GAPI to Get Site Data

If you have a Web site, Google Analytics is a powerful tool for giving you information such as Total Visitors, Total PageViews, Bounce Rate, Browsers (that are used to view your site), and much more. You may also want to use that data in your Web site, such as if you want to post links to your top ten entries, create a counter that keeps track of total visits or total unique visits, you can do that with GAPI.

Google Analytics

It’s easy to set up a Google Analytics Account, and if you already have a gmail account, you can use that. Once you have an account, you’ll want to add a web account under the "Admin" tab. That’s where you will enter your site URL. Just follow the instructions on the screen. Below is a screen shot of where the Add Account button is.

After that, download a page such as "google.html" and put it on your Web site root directory. That’s to let Google know that you are for sure the owner of that Web address.

Now, you’ll want to go to the Google Analytics Web site and under your account, click on "Admin", then click on your account name. If it shows up again, click on it again. Then click on "Tracking Code". The screenshot below shows where it’s located.

After that, just follow the directions on that page, and place the code before the tag on all the pages you want the analytics to keep track of. If you have a separate file for the header information that you use to link to all your pages, you can put it in there. After that you’re all set for Google Analytics to do its job.

GAPI 

Setting up GAPI can be a bit tricky. I’ll try my best to help with that. First, go to the GAPI Web site and download and unzip the files. Then, you’re ready to have fun. You can look at the example pages that they have and test those out first.

Important: The first file you’ll want to edit is example.account.php. It’s important to do that because it has your **profile id, **which is needed along with your username and password for your analytics account. After getting your profile id, you’re ready to roll. Create a page such as this:

<?php
include( 'gapi.class.php'); // include the path to the gapi class

define('ga_email','[email protected]'); // enter your username and password.
define('ga_password','your password');
$ga = new gapi(ga_email,ga_password);

$report_id = 'enter your id';
$filter = 'ga:pagePath==/page-path.php';
$start_date = 'YYYY-MM-DD'; // example: 2011-01-31
// if you leave $end_date as null, it's today's date
// you may create variables for each parameter value or enter them into the parenthesis directly.

$ga->requestReportData(
    $report_id, // $report_id
        array('pagePath'), // $dimensions
    array('UniquePageviews'), // $metrics
   '-UniquePageviews', // $sort_metric
 $filter, // $filter
   $start_date, // $start_date
   null, // $end_date
    1, // $start_index
    null // $max_results
);
foreach($ga->getResults() as $result) {
  $pageview = $result->getUniquePageviews();
 echo 'path: '.$result->getPagePath().'<br/>';
    echo $pageview." unique views";
}
?>

The above code displays the total number of unique views for a page. Read the comments in the code snippet for more information. The Documentation page lists the variables you can use. If you don’t fill in the variables, the gapi.class.php file will generate the default values for each variable. The example.report.php page shows how you can grab the total visitors and total pageviews. Below is simplified version of it in case you want to use it as a page counter.

<?php
define('ga_email','[email protected]');
define('ga_password','your password');
define('ga_profile_id','your id');

require '/PATH/TO/gapi.class.php';

$ga = new gapi(ga_email,ga_password);
//$dimensions, $metrics, $sort_metric=null, $filter=null, $start_date=null, $end_date=null, $start_index=1, $max_results=30)
$ga->requestReportData(
 ga_profile_id,
    array('browser'),
   array('pageviews','visits'),
  array('-visits'),
   null,
 '2011-05-31',
   null,
 1,
    30
);
?>

<table>
<tr>
  <th>Total Pageviews</th>
  <td><?php echo $ga->getPageviews() ?>
</tr>
<tr>
  <th>Total Visits</th>
  <td><?php echo $ga->getVisits() ?></td>
</tr>
<tr>
  <th>Results Updated</th>
  <td><?php echo $ga->getUpdated() ?></td>
</tr>
</table>

That’s it for the basics. Happy coding!

Posted on: June 3, 2012Categories: TutorialsTags: coding
â€č Back to blog