Last active
December 29, 2015 23:59
-
-
Save pryley/7746379 to your computer and use it in GitHub Desktop.
Frontend google analytics snippet
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
<?php | |
try { | |
if( $access_token ) { | |
// Set access token | |
$client->setAccessToken( $access_token ); | |
// Assign variables | |
$slug_array = $filter_path_array = $filter_array = $data = array(); | |
// Populate title and permalink arrays | |
foreach( $query as $post ) : | |
array_push( $slug_array, $post->post_name ); | |
endforeach; | |
// Populate filter_path array | |
if( is_array( $slug_array ) ) { | |
foreach( $slug_array as $link ) { | |
array_push( $filter_path_array, 'ga:pagepath==/listings/' . $link . '/' ); | |
} | |
// Divide the array into sub-arrays (gets past the char limit the API imposes) | |
$filter_array = array_chunk( $filter_path_array, 10 ); | |
} | |
// Get posts-per-page option value | |
$ppp = of_get_option( 'account_number' ); | |
$ppp = $ppp ? (int)$ppp : 12; | |
// Set date range | |
$date_start = date( 'Y-m-d', mktime( 0, 0, 0, 6, 11, 2013 ) ); | |
$date_end = date( 'Y-m-d', time() - 86400 ); | |
foreach( $filter_array as $array ) { | |
// Convert Filter path array to a (comma seperated values) string | |
$filter_string = implode( ',', $array ); | |
//request analytics data | |
$report_data = $analytics->data_ga->get( | |
'ga:' . $ganalytics_settings['ga_profile_id'] | |
,$date_start | |
,$date_end | |
,'ga:pageviews,ga:avgTimeOnPage' | |
,array( | |
'dimensions' => 'ga:pagePath' | |
,'max-results' => $ppp | |
,'filters' => $filter_string | |
) | |
); | |
if( is_array( $report_data->getRows() ) ) { | |
foreach( $report_data->getRows() as $value ) { | |
$data[] = $value; | |
} | |
} | |
} | |
if( $description ) { | |
$table .= "<p>{$description}</p>"; | |
} | |
$table .= "" . | |
"<table class='table all_posts' cellspacing='0'>" . | |
"<thead>" . | |
"<tr>" . | |
"<th width='55%'>" . __( 'Event', 'gemini' ) . "</th>" . | |
"<th class='right'>" . __( 'Status', 'gemini' ) . "</th>" . | |
"<th class='right'>" . __( 'Views', 'gemini' ) . "</th>" . | |
"<th class='right'>" . __( 'Avg View Time', 'gemini' ) . "</th>" . | |
"</tr>" . | |
"</thead>" . | |
"<tbody>"; | |
foreach( $query as $event ) : | |
$views = 0; | |
$average = '00:00:00'; | |
foreach( $data as $key => $report ) { | |
// parse URL and return only path component | |
// remove surrounding "/" | |
// explode parts into array | |
// return last element | |
$slug = parse_url( $report[0], PHP_URL_PATH ); | |
$slug = end( explode( '/', trim( $slug, '/' ) ) ); | |
if( $slug == $event->post_name ) { | |
$views = number_format( (int) $report[1] ); | |
$average = gmdate( 'H:i:s', (int) $report[2] ); | |
break; | |
} | |
} | |
$table .= "" . | |
"<tr>" . | |
"<td><a class='title' href='" . get_permalink( $event->ID ) . "' title='" . sprintf( esc_attr__( 'Permalink to %s', 'gemini' ), the_title_attribute( 'echo=0' ) ) . "' rel='bookmark'>" . get_the_title( $event->ID ) . "</a></td>" . | |
"<td class='right'>" . self::display_post_status( $event->post_status ) . "</td>" . | |
"<td class='right'>{$views}</td>" . | |
"<td class='right'>{$average}</td>" . | |
"</tr>"; | |
endforeach; | |
$table .= "" . | |
"</tbody>" . | |
"</table>"; | |
} | |
else { | |
$table = "<p class='center'>Coming soon.</p>"; | |
} | |
} | |
catch( Exception $e ) { | |
$table = "<p class='center'>Could not connect to Google Analytics. :(</p>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment