|
<?php |
|
|
|
// Load the Google API PHP Client library. |
|
require_once( __DIR__ . '/../vendor/autoload.php' ); |
|
|
|
// Query Analytics API for the pages with the most views. |
|
function get_most_viewed_post_slugs() { |
|
try { |
|
$analytics = establish_client_connection(); |
|
} |
|
catch ( Exception $error ) { |
|
error_log( $error ); |
|
return; |
|
} |
|
|
|
// View this query in the Query Explorer: https://ga-dev-tools.appspot.com/query-explorer/?ids=ga%3ATODO&start-date=7daysAgo&end-date=today&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews&filters=ga%3ApagePath%3D~%5B%5Cd%2F%5D%7B10%7D&max-results=5 |
|
$results = $analytics->data_ga->get( |
|
'ga:TODO', // view |
|
'7daysAgo', // start date |
|
'today', // end date |
|
'ga:pageviews', // metrics |
|
array( // options |
|
// We only need to look at the page path. |
|
'dimensions' => 'ga:pagePath', |
|
|
|
// Sort by the most pageviews in descending order. |
|
'sort' => '-ga:pageviews', |
|
|
|
// Get only pages with a URL that has a string of ten numbers and slashes, |
|
// which is what blog post URLs look like, e.g.: /2020/04/14/foo-bar-baz |
|
'filters' => 'ga:pagePath=~[\d/]{10}', |
|
|
|
// Limit results. |
|
'max-results' => 5, |
|
) |
|
); |
|
|
|
if ( ! is_object( $results ) ) { |
|
throw new Exception( 'Could not retrieve data from Google Analytics.' ); |
|
} |
|
|
|
// Extract the results we want. |
|
$posts = $results->rows; |
|
|
|
if ( empty( $posts) ) { |
|
throw new Exception( 'No rows found in Google Analytics data.' ); |
|
} |
|
|
|
// Get post slugs from the results. |
|
$post_slugs = extract_post_slugs( $posts ); |
|
|
|
return $post_slugs; |
|
} |
|
|
|
// Query Analytics API for pages that have the most 'share via email' events. |
|
function get_most_emailed_post_slugs() { |
|
try { |
|
$analytics = establish_client_connection(); |
|
} |
|
catch ( Exception $error ) { |
|
error_log( $error ); |
|
return; |
|
} |
|
|
|
// View this query in the Query Explorer: https://ga-dev-tools.appspot.com/query-explorer/?ids=ga%3A56434181&start-date=7daysAgo&end-date=today&metrics=ga%3AtotalEvents&dimensions=ga%3ApagePath%2Cga%3AeventLabel&sort=-ga%3AtotalEvents&filters=ga%3AeventLabel%3D~email&max-results=5 |
|
$results = $analytics->data_ga->get( |
|
'ga:56434181', // view |
|
'7daysAgo', // start date |
|
'today', // end date |
|
'ga:totalEvents', // metrics |
|
array( // options |
|
// We need to look at the page path and the event label. |
|
'dimensions' => 'ga:pagePath,ga:eventLabel', |
|
|
|
// Sort by the most pageviews in descending order. |
|
'sort' => '-ga:totalEvents', |
|
|
|
// Get only pages with a URL that has a string of ten numbers and slashes, |
|
// which is what blog post URLs look like, e.g.: /2020/04/14/foo-bar-baz |
|
'filters' => 'ga:eventLabel=~email', |
|
|
|
// Limit results. |
|
'max-results' => 5, |
|
) |
|
); |
|
|
|
if ( ! is_object( $results ) ) { |
|
throw new Exception( 'Could not retrieve data from Google Analytics.' ); |
|
} |
|
|
|
// Extract the results we want. |
|
$posts = $results->rows; |
|
|
|
if ( empty( $posts) ) { |
|
throw new Exception( 'No rows found in Google Analytics data.' ); |
|
} |
|
|
|
// Get post slugs from the results. |
|
try { |
|
$post_slugs = extract_post_slugs( $posts ); |
|
} |
|
catch ( Exception $error ) { |
|
error_log( $error ); |
|
return; |
|
} |
|
|
|
return $post_slugs; |
|
} |
|
|
|
// Extract post slugs from the list of page URLs. |
|
function extract_post_slugs( $posts ) { |
|
if ( empty( $posts ) ) { |
|
throw new Exception( 'No posts provided to extract slugs from.' ); |
|
} |
|
|
|
// Get just the slugs from each post. |
|
$post_slugs = array(); |
|
|
|
foreach ( $posts as $post ) { |
|
$post_path = $post[0]; |
|
|
|
// The page URLs look like this: /2020/04/14/foo-bar-baz. We just want the |
|
// 'foo-bar-baz' part, which is 12 characters from the start of the URL. |
|
$post_slug = substr( $post_path, 12 ); |
|
|
|
// Remove query string if one exists. |
|
if ( stripos( $post_slug, '?' ) ) { |
|
$post_slug = substr( $post_slug, 0, stripos( $post_slug, '?' ) ); |
|
} |
|
|
|
// If there's a trailing slash, remove it. |
|
$post_slug = rtrim( $post_slug, '/' ); |
|
|
|
$post_slugs[] = $post_slug; |
|
} |
|
|
|
if ( empty( $post_slugs ) ) { |
|
throw new Exception( 'Could not extract page slugs from the results.' ); |
|
} |
|
|
|
return( $post_slugs ); |
|
} |