Created
March 14, 2014 12:33
-
-
Save wturrell/9546836 to your computer and use it in GitHub Desktop.
Wordpress & Google Analytics - retrieve most popular posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
/* | |
Ampp3d - Ampp3d Google Analytics | |
Version: 0.1 | |
Author: William Turrell | |
Author URI: wturrell.co.uk | |
Description: Use Google Analytics API to retrieve most popular pages and construct an HTML fragment we can use | |
Adapted from http://www.techpunch.co.uk/development/oauth2-google-analytics-api-service-account-php and | |
https://github.com/lobsterdore/analytics-api-oauth2-example/blob/master/mostPopularContentExample.php | |
*/ | |
class Ampp3d_Analytics | |
{ | |
public $service; | |
function __construct() | |
{ | |
// We're using Google API PHP Client | |
require_once('../../themes/boilerplate/libraries/google-api-php-client/src/Google_Client.php'); | |
require_once('../../themes/boilerplate/libraries/google-api-php-client/src/contrib/Google_AnalyticsService.php'); | |
// create client object and set app name | |
$client = new Google_Client(); | |
$client->setApplicationName('foo'); // name of your app | |
// set assertion credentials | |
$client->setAssertionCredentials( | |
new Google_AssertionCredentials( | |
'[email protected]', // email you added to GA | |
array('https://www.googleapis.com/auth/analytics.readonly'), | |
file_get_contents('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12') // keyfile you downloaded | |
) | |
); | |
// other settings | |
$client->setClientId('xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'); // from API console | |
$client->setAccessType('offline_access'); // this may be unnecessary? | |
// create service and get data | |
$this->service = new Google_AnalyticsService($client); | |
} | |
/* Get the analytics statistics, then call render | |
* $duration = string - length of period of data we want in a format that strtotime() can understand */ | |
function get_daily_stats($duration = '3 days') | |
{ | |
$rows_wanted = 30; // Grab more than we need because we'll drop non-posts (i.e. pages) and also any where the URL doesn't match the permalink | |
$end_date = date('Y-m-d'); | |
$start_date = date('Y-m-d', strtotime('-'.$duration)); | |
// https://developers.google.com/analytics/devguides/reporting/core/v3/reference | |
$response = $this->service->data_ga->get( | |
'ga:xxxxxxxx', // profile id | |
$start_date, | |
$end_date, | |
'ga:uniquePageviews', | |
array( | |
'dimensions' => 'ga:pagePath', | |
'sort' => '-ga:uniquePageviews', | |
'max-results' => $rows_wanted)); | |
$ga_list = $response['rows']; // Set of URLs and view counts | |
if (count($ga_list) != $rows_wanted) { | |
echo "Potential problem with results. \n"; | |
exit; | |
} | |
$data = []; | |
foreach ($ga_list as $k => $v) | |
{ | |
// $v[0] = relative path (/2013/12/11/free-schools... etc.) | |
// $v[1] = page view counter | |
if (! $postid = url_to_postid($v[0]) ) | |
{ | |
continue; | |
} | |
$data[] = get_post($postid); | |
} | |
$this->render($data); | |
} | |
/* Generate HTML ordered-list of links to popular posts | |
* $data = array of WordPress post objects */ | |
function render($data = array(), $limit = 10) | |
{ | |
$output = "<ol>\n"; | |
$c = 0; | |
foreach(array_values($data) as $v) | |
{ | |
$c++; | |
if ($c > $limit) | |
{ | |
break; | |
} | |
$output .= sprintf('<li data-post-id="%d"><a href="%s">%s</a></li>', $v->ID, get_permalink($v->ID), $v->post_title); | |
$output .= "\n"; | |
} | |
$output .= "</ol>\n"; | |
$cwd = getcwd(); | |
$fh = fopen($cwd.'/most-read.html', 'w'); | |
if ($fh === false) { | |
echo "Problem opening output file. \n"; | |
return; | |
} | |
fwrite($fh, $output); | |
fclose($fh); | |
} | |
} | |
// Load Wordpress core (NB: if you put this inside the class, it *won't* work) | |
$cwd = getcwd(); | |
$path = substr($cwd, 0, strpos($cwd, 'wp-content/')); | |
require $path.'wp-load.php'; | |
$ga = new Ampp3d_Analytics(); | |
$ga->get_daily_stats(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment