Add a pageview column with Jetpack data source

I decided to get with the times and give Jetpack a try. If you’re unfamiliar with Jetpack, it’s essentially a WordPress plugin which ties your self-hosted WordPress blog to WordPress.com’s servers and allows nifty features like an image CDN, Gravatar hovercards, centralized comments, and a multitude of other features. Personally, I love the WordPress.com stats the most.

I missed my views column on my posts page in admin though (which I used to source from my Better Postviews plugin). So I wrote up this little function to get the data from Jetpack servers and happily place it where it belongs. Here is the code to do so:

<?php // Add a Views columns to insert jetpack postviews data into function rdc_add_views_column( $cols ) { $cols['pageviews'] = 'Views'; return $cols; } add_filter( 'manage_edit-post_columns', 'rdc_add_views_column' ); // Grab and display the postviews data from Jetpack for each post function rdc_add_views_col_data( $colname ) { global $post; // Make sure we're inserting into the correct column if ( 'pageviews' !== $colname ) return false; // Make sure jetpack and stats are available if ( ! ( class_exists( 'Jetpack' ) &amp;&amp; Jetpack::is_module_active( 'stats' ) ) ) { echo 'Error'; return false; } // Make sure stats_get_csv is available if ( ! function_exists( 'stats_get_csv' ) ) { echo 'Error'; return false; } // Try to get view count from post meta "cache" $view_count = get_post_meta( $post->ID, '_jetpack_post_views_count', true );
	$view_count_created = absint( get_post_meta( $post->ID, '_jetpack_post_views_count_created', true ) );

	// No "cache" value, hit the API for the value
	if ( ! $view_count || time() > $view_count_created + 3600 ) {
		// Get the post data from Jetpack
		$postviews = stats_get_csv( 'postviews', "post_id={$post->ID}" );

		// We have a problem if there was no data returned
		if ( ! $postviews ) {
			echo 'Error';
			return false;
		}

		// Get view count from returned results
		$view_count = absint( $postviews[0]['views'] );

		// Store the value and time as post meta
		update_post_meta( $post->ID, '_jetpack_post_views_count', absint( $view_count ) );
		update_post_meta( $post->ID, '_jetpack_post_views_count_created', absint( time() ) );
	}

	// Print Jetpack post views
	echo '<strong>' . number_format( absint( $view_count ) ) . '</strong>';
}
add_action( 'manage_posts_custom_column', 'rdc_add_views_col_data' );

Drop that code into your functions.php or another theme file. Also, if you’re REALLY scaling up, you’re probably using something like Varnish cache and the Better Postviews Plugin may not register an accurate view count. If that’s the case, Jetpack may be for you since WordPress.com stats are javascript based.

There are a ton of other metrics which you can pull from Jetpack also – check out George Stephanis’ slides from WordCamp Boston 2013 for some more info.

Enjoy!

4 thoughts on “Add a pageview column with Jetpack data source”

  1. Minor tweak — just so you’re not lagging out your admin page loads, it may be best if you shove the result in a transient for about 12 hours or so — especially with the number of calls you’re making — probably 20 per page?

    Also, the post_id argument to stats_get_csv() will accept a comma-delimited list of integers, so you could actually pull them all in via one API call — either with a pre-loop check preloading them into memory, or what I’d recommend — an AJAX call that checks the post IDs showing on the current page, and pings admin-ajax.php which fires off the request — that way your page loads won’t be sluggish while the stats pageviews load in from the API.

    Did that make sense? Happy to help with any code snippets if it’d be helpful.

    Cheers!

  2. Minor tweak — just so you’re not lagging out your admin page loads, it may be best if you shove the result in a transient for about 12 hours or so — especially with the number of calls you’re making — probably 20 per page?

    Also, the post_id argument to stats_get_csv() will accept a comma-delimited list of integers, so you could actually pull them all in via one API call — either with a pre-loop check preloading them into memory, or what I’d recommend — an AJAX call that checks the post IDs showing on the current page, and pings admin-ajax.php which fires off the request — that way your page loads won’t be sluggish while the stats pageviews load in from the API.

    Did that make sense? Happy to help with any code snippets if it’d be helpful.

    Cheers!

Leave a Reply