WordPress.com Stats via JetPack
In case you’re not familiar with JetPack, it’s a plugin that includes a variety of different features for your WordPress website. Among these features are things like the JSON API, comment form, markdown, mobile theme, and a bunch more.
Most of JetPacks features, I don’t use. However, I love the site stats component. This allows me to connect my blog to my WordPress.com account and preview statistics about my traffic right from within the WordPress dashboard. While I use Google Analytics as well, the JetPack site stats gives me information that I want and presents it in a way that I prefer. For me, Google Analytics throws more at me than I typically care about.
Retrieving Site Stats
Before starting, I’m assuming you’ve connected your website to your WordPress.com account. This is a necessary step because your request for your site’s stats (coming up in a moment) will return an error if not connected.
To retrieve your site’s stats, you need to make a call to WordPress.com’s site stats API. The primary piece of information you need for this is a WordPress.com API key. If you’ve signed up to use Akismet, you were given an API key at that time. You can head to the Akismet settings in your dashboard and retrieve the API key from there.
Now that you have your WordPress.com API key and your site is connected to your WordPress.com account, you’re ready to request your site’s traffic statistics. This request is carried out in the form of an HTTP GET request. Here’s the code, which I’ll explain below.
Here’s a rundown of what’s going on:
- To start, we’re assembling the URL to which we send our request. The query arguments are added to the https://stats.wordpress.com/csv.php URI. The arguments contain the data we need to validate the request and tell WordPress.com what we want. First, make sure to insert your API key in the “key” argument. Next, if you’re retrieving stats for a specific post rather than your entire site, uncomment the “post_id” argument. I wrote this part to pull stats for the current post but you can change that if needed. Lastly, if you want to request stats for more than just the current day, change the “days” argument to however many days you wish (1 retrieves the current day). For a full list of parameters you can include, see the http://stats.wordpress.com/csv.php without any query arguments.
- Next up, we send that request. With the response, we extract and decode the JSON, which puts it into an array we can easily work with. We also check to make sure that our data is an array and bail if it’s not because we don’t want to show the error received with a failed request.
- The last part prints the array to the top of the screen for you to see what’s going on and how the data is organized. It checks to see if the current user is an administrator and, if not, it won’t print the data. However, this part is only for testing. Once you’ve adjusted the code to get the data you want, simply place it wherever you want in your theme.
That’s all there is to retrieving your site’s stats via WordPress.com and the JetPack plugin. This can be a cool feature for things like displaying a post’s lifetime traffic to author or showing off today’s visitor count to your readers (if you roll that way). Enjoy!
Eden Smith says
Is the same process above to follow for retrieving a period of 3 weeks?
Eden Smith says
For instance, retrieving a gap period of full stats.