Skip to content

Instantly share code, notes, and snippets.

@hiltmon
Created June 15, 2013 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiltmon/5789089 to your computer and use it in GitHub Desktop.
Save hiltmon/5789089 to your computer and use it in GitHub Desktop.
<?php
define('ga_email','hiltmon@gmail.com');
define('ga_password','i_aint_saying');
define('which_profile',0); // The first profile
define('days_to_report', 0); // No of days excluding today!
define('hours_to_report', 3);
define('ga_title','Top Pages 3 Hours');
require 'gapi.class.php';
date_default_timezone_set('America/New_York');
$ga = new gapi(ga_email,ga_password);
$ga->requestAccountData();
// Run this to see available accounts
// foreach($ga->getResults() as $result)
// {
// echo $result . ' (' . $result->getProfileId() . ")<br />";
// }
// And this to get the graph
$results = $ga->getResults();
$result = $results[which_profile];
$ga_profile_id = $result->getProfileId();
$start_date = date("Y-m-d", strtotime('-' . days_to_report . ' hours'));
$end_date = date("Y-m-d");
$metrics = array('pageviews');
$ga->requestReportData($ga_profile_id, array('PageTitle', 'date', 'hour'), $metrics, array('-pageViews'), null, $start_date, $end_date, null, 500);
$array1 = array();
foreach($ga->getResults() as $result2) {
$title = $result2->getPageTitle();
$key = date('Y-m-d', strtotime($result2->getDate())) . ' ' . $result2->getHour() . ':00';
$lower = strtotime("- " . hours_to_report . " hours");
if ((strtotime($key) <= time()) && (strtotime($key) >= $lower) ) {
if (array_key_exists($title, $array1)) {
$array1[$title] = $array1[$title] + $result2->getPageviews();
} else {
$array1[$title] = $result2->getPageviews();
}
}
}
arsort($array1);
echo '<table id="analytics_pages">';
foreach($array1 as $key => $value) {
echo '<tr>';
echo '<td class="pageViews" style="width:15%;text-align:right;">' . $value . '</td>';
echo '<td class="pageTitle" style="width:85%;">' . $key . '</td>';
echo '</tr>';
}
echo '</table>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment