Getting to grips with Google Analytics API

Getting to grips with Google Analytics API


This shows the top 15 regions / by source / by number of visits decending

<?php

require 'gapi.class.php';

$username='your gmail email address';
$password='your password';
$profile_id='your profile ID, you can get this from the url when you view the standard report. Just look for id on the url';

$ga = new gapi($username,$password);

$ga->requestReportData($profile_id,array('source','region'),array('pageviews','visits'), '-visits','',
 '2011-11-01', // Start Date
                       '2011-11-30', // End Date
                       1,  // Start Index
                       15 // Max results
                       );

echo '<strong>Top 20 Region/Source by Visits</strong><br><br>';
echo '<table><tr>';

echo '<td>Region</td><td>Source</td><td>Page Views</td><td>Visits</td></tr>';
foreach($ga->getResults() as $result)
{
echo '<tr>';
  echo '<td>' . $result->getRegion() . '</td>';
  echo '<td>' . $result->getSource() . '</td>';
  echo '<td>' . $result->getPageViews() . '</td>';
  echo '<td>' . $result->getVisits() . '</td></tr>';
}

echo '</tr></table>';
echo '<p>Total pageviews: ' . $ga->getPageviews() . ' total visits: ' . $ga->getVisits() . '</p>';


?>

Comments

Popular posts from this blog

ActiveMQ, easy to use open source message oriented middleware (MOM)

Basic Send Message to MQ with Java and IBM MQ JMS

MySQL Error Invalid Table or Database Name