Skip to content

Instantly share code, notes, and snippets.

@jmk1ng
Last active August 29, 2015 13:57
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 jmk1ng/9442895 to your computer and use it in GitHub Desktop.
Save jmk1ng/9442895 to your computer and use it in GitHub Desktop.
An extract of code from a Drupal module that polls Google Analytics for data about specific nodes.
<?php
/* .. snip .. */
function MYMODULE_listing_stats($node) {
//If someone feels so inclined at some point, this can be integrated as a library for loading
//via the Libraries API module. http://drupal.org/node/1342238
require_once DRUPAL_ROOT.'/sites/all/libraries/googleapi/Google_Client.php';
require_once DRUPAL_ROOT.'/sites/all/libraries/googleapi/contrib/Google_AnalyticsService.php';
$node_path = url('node/'.$node->nid);
//This stuff is set up in the GoogleAPI console - https://code.google.com/apis/console
// Don't actually hard-code these values. Be a good dev and make a nice settings screen, and store/retrieve them via
// variable_get/set()
$client = new Google_Client();
$client->setClientId('############.apps.googleusercontent.com');
$client->setAssertionCredentials(new Google_assertionCredentials(
'############@developer.gserviceaccount.com',
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents(PATH_TO_PRIVATE_KEY)
));
// This module pulled data for real estate listings. We want to show view stats on a per-listing basis per week, and for all time
$analytics = new Google_AnalyticsService($client);
$analytics_id = 'ga:#######';
$lastWeek = date('Y-m-d', strtotime('-1 week'));
$today = date('Y-m-d');
$live_date = (!empty($node->field_listing_go_live_date) ? date('Y-m-d', strtotime($node->field_listing_go_live_date['und'][0]['value'])) : '2010-01-01');
$week = array();
for($d = 7; $d >= 0; $d--) {
$timeago = strtotime('-'.$d.' days');
$week[date('d', $timeago)] = array(date('M d', $timeago), 0, 0);
}
try {
// Query the GA API for the data
$weekresults = $analytics->data_ga->get($analytics_id, $lastWeek, $today,'ga:visitors', array(
'dimensions' => 'ga:day',
'metrics' => 'ga:uniquePageviews,ga:pageviews',
'filters' => 'ga:pagePath=='.$node_path
));
$totalresults = $analytics->data_ga->get($analytics_id, $live_date, $today,'ga:visitors', array(
'metrics' => 'ga:uniquePageviews,ga:pageviews',
'filters' => 'ga:pagePath=='.$node_path
));
// Massage the returned data a little to make it easier to chart
foreach($weekresults['rows'] as $row) {
$week[$row[0]][1] = (int)$row[1];
$week[$row[0]][2] = (int)$row[2];
}
$totalresults['rows'][0][0] = (int)$totalresults['rows'][0][0];
$totalresults['rows'][0][1] = (int)$totalresults['rows'][0][1];
$output['total'] = array(
array('Unique Visits', (int)$totalresults['rows'][0][0]),
array('Return Visits', (int)($totalresults['rows'][0][1] - $totalresults['rows'][0][0]))
);
$output['week'] = array_values($week);
drupal_json_output($output);
} catch (Exception $e) {
drupal_json_output(array('error' => 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment