ShintoTuna
ShintoTuna

Reputation: 3787

Google Analytics posts monitoring addon for WordPress

I'm assigned to create a WordPress plugin that displays recent visits graph under each post in WordPress. I'm struggling with the authentication of the data and general structure of plugin. My first question would be whats the most efficient way to authenticate with Google API for this kind of task? I was looking into the AuthSub but i cant understand the concept of next parameter in the query. It should be the landing page after authorization, but how can i make it work dynamically with all the different posts? In OAuth i'm getting a bit lost in terminology.

Second question is how often should the Google API be queried for the results. I mean is it rational to make smaller request every time visitor opens a page or maybe its rater optimal to download the data for the pages once and keep it in local xml and refresh it on some intervals?

As an idea of the plugin overall structure i was thinking of making a php file which would generate the graph out of the GA feed and would do it so when called from post hook via ajax. This would be controlled by passing parameters of the post to that php file.
Would that structure make sense or there is an easier way to preform the task?

I would really appreciate if someone pointed me in right direction especially in authentication problem.

Upvotes: 0

Views: 69

Answers (1)

Eduardo
Eduardo

Reputation: 22832

The most important thing to do first is to lay out your design. You don;t want the user of your site to authenticate on the GA API. Because he doesn't have access to your data. So you'll have to login with your own credentials on the backend. And just cache the metrics you want to display on the screen. So, from the user perspective, there's no way to tell if you're using Google Analytics or any other web analytics product.

Since you're working with Wordpress and you'll need to do the data pull on the backend you probably want to do that using PHP.

You should take a look at this PHP library. http://code.google.com/p/gapi-google-analytics-php-interface/

Even though it's not google officially supported it's pretty good and you won't have to worry about the process of authentication.

It doesn't make sense to query GA every time the user visits your site. Besides impacting the load heavily the GA API has some latency and GA is not a real time tool. The data freshness on your GA data depends on how much data you get. If you are a small blog it can take 2-4 hours to process the data. If you're a big blog it can take up to 48h. So for that reason I'd query the API every 4 hours or so and just cache the data for all your pages. When you render the page it's just a matter of getting it from the cache/db.

To plot that data there are plenty of options around there. I'd suggest you to start with Google Chart Tools.

Upvotes: 1

Related Questions