Skip to content

Instantly share code, notes, and snippets.

@CNG
Last active April 20, 2017 08:25
Show Gist options
  • Save CNG/7f6c2b140a3d93b398eb to your computer and use it in GitHub Desktop.
Save CNG/7f6c2b140a3d93b398eb to your computer and use it in GitHub Desktop.
Author page views PHP
<?php
// 2013-10-16 [email protected]: initial commit
/**
* Return Google Core Reporting API access token
* @return string|bool access token or false if embedded parameters invalid
*/
function getAccessToken()
{
// obtained from companion file at SERVER/oauth2callback.php
$refresh_token = 'REFRESH_TOKEN';
$client_id = 'CLIENT_ID.apps.googleusercontent.com';
$client_secret = 'CLIENT_SECRET';
// Retrieve access token using stored refresh token
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token' );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS,
'refresh_token=' . $refresh_token . '&client_id=' . $client_id
. '&client_secret=' . $client_secret . '&grant_type=refresh_token'
);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
$data = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $data, true );
return isset($result['access_token']) ? $result['access_token'] : false;
}
/**
* Query API and retrieve array of rows from JSON response(s)
* @param string $access_token Access token retrieved with getAccessToken()
* @param array $options Query parameters
* @return array Rows from API response
*/
function getNewData($access_token, $options)
{
date_default_timezone_set('America/New_York'); // avoid warnings if not set elsewhere
// "ids" value comes from this URL in the last portion of the URL, after the "p":
// https://www.google.com/analytics/web/#dashboard/default/a381759w192893p9122283/
// Or use http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html to
// show the GA ID for each your Analytics accounts
$options['ids'] = 'ga:XXXXXXX';
// API key that you'd set up in the Google APIs console, restricted to certain IP addresses
// $options['key'] = 'XXXXXXXXXXXXXXXXXXXXX'; // this is apparently not necessary anymore?
$options = array_filter($options); // remove empty elements
// if $options['max-results'] set, limit it to API max but store requested max
$max_results = null;
$api_results_limit = 10000; // API max
if (!empty($options['max-results'])) {
$max_results = $options['max-results']; // save requested maximum
if ($options['max-results'] > $api_results_limit) {
$options['max-results'] = $api_results_limit; // set each request to max API limit
}
}
// build URL for first request; subsequent URLs come from API response
$options = http_build_query($options); // also handles urlencode
$url = "https://www.googleapis.com/analytics/v3/data/ga?$options";
$rows = array(); // hold data from results while we potentially make additional requests
// loop till there is no next URL in response or we collected enough rows
while ($url && (is_null($max_results) || count($rows) < $max_results)) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_token ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
$data = curl_exec( $ch );
curl_close( $ch );
$decoded = json_decode($data, true);
$rows = array_merge($rows, $decoded['rows']);
$url = isset($decoded['nextLink']) ? $decoded['nextLink'] : false;
}
return array_slice($rows, 0, $max_results); // last loop might have put over max_results
}
/**
* Check for recent cached data and return that or newly fetched data. This function
* can fail if the cache needs to be refreshed but getAccessToken returns false.
* @param string $filename Name of cached file
* @param int $max_age_seconds Oldest allowed cache in seconds. API daily
* limit as of 2012-04-02 is 50K, which is 1 request every 1.728 seconds
* @param array $options Query parameters
* @return object JSON decoded API response
*/
function getData($filename, $max_age_seconds, $options)
{
$json_file = dirname(__FILE__) . '/' . pathinfo(__FILE__, PATHINFO_FILENAME) . ".$filename.json";
$data;
if( file_exists($json_file) && time() - filemtime($json_file) < $max_age_seconds) {
$data = file_get_contents($json_file);
} elseif ($access_token = getAccessToken()) {
$data = getNewData($access_token, $options);
$data = json_encode($data);
file_put_contents($json_file, $data);
}
return json_decode($data, true);
}
/**
* Get YTD pageviews by author
* @return array Keys are author names and values are number of pageviews this
* year for all articles published any time with that author in the author list.
* A one month allowance is made after a new year starts to allow viewing the
* data for previous year through January of the following year.
*/
function getYTDPageviews()
{
$data = array(); // hold return values
$options = array(
'start-date' => date('Y', strtotime('-1 month')) . '-01-01',
'end-date' => date('Y', strtotime('-1 month')) . '-12-31',
'dimensions' => 'ga:customVarName1,ga:customVarValue1',
'metrics' => 'ga:pageviews',
'filters' => 'ga:customVarName1==Author',
'segment' => '',
'sort' => '',
'max-results' => '',
);
$rows = getData('ytd_pageviews', time() - strtotime("today"), $options);
foreach($rows as $row) {
$authors = explode(',', $row[1]);
foreach($authors as $author) {
if (isset($data[$author])) {
$data[$author] = $data[$author] + $row[2];
} else {
$data[$author] = $row[2];
}
}
}
return $data;
}
/**
* Get 30 day pageviews by author
* @return array Keys are author names and values are number of pageviews in
* last 30 days for all articles published during the same period with that
* author in the author list.
*/
function get30DPageviews()
{
$data = array(); // hold return values
$options = array(
'start-date' => date('Y-m-d', strtotime('-30 day')),
'end-date' => date('Y-m-d', strtotime('-1 days')),
'dimensions' => 'ga:customVarName1,ga:customVarValue1,ga:customVarName4,ga:customVarValue4',
'metrics' => 'ga:pageviews',
'filters' => 'ga:customVarName1==Author,ga:customVarName4==Day',
'segment' => '',
'sort' => '',
'max-results' => '',
);
$rows = getData('30d_pageviews', time() - strtotime("today"), $options);
foreach($rows as $row) {
$row_ts = strtotime($row[3]); // timestamp of date from current row
$min_ts = strtotime($options['start-date']);
$max_ts = strtotime($options['end-date']);
if($row_ts <= $max_ts && $row_ts >= $min_ts) {
$authors = explode(',', $row[1]);
foreach($authors as $author) {
if (isset($data[$author])) {
$data[$author] = $data[$author] + $row[4];
} else {
$data[$author] = $row[4];
}
}
}
}
return $data;
}
$pageviewsYTD = getYTDPageviews();
//echo count($pageviewsYTD)."<br />";
$pageviews30D = get30DPageviews();
//echo count($pageviews30D)."<br />";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment