Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect a PHP backend to Google Analytics API

I am trying to present some basic data from our Google Analytics accounts directly on our company's backend. I am using the Google API PHP Client.

I have followed the Hello Analytics API tutorial but it leaves me with the obligation to connect from a browser. I need the info to be available from the server itself (without browser client connection) as I would like to automate the queries from within a Cron task.

Is what I want to do possible? Am I using the correct PHP API? Is there a different way to get back GA data?

like image 544
Sébastien Avatar asked Mar 09 '13 00:03

Sébastien


3 Answers

Why won't you replace your google analytics service with something like Piwik!

like image 149
mkungla Avatar answered Oct 16 '22 10:10

mkungla


I skimmed through the links you provided and near as I can tell, it doesn't obligate you to connect from a browser. First off, are you sure you looked at the "php" tab in that tutorial? The walkthrough uses browser interaction, but you can easily make it do something else instead of output to the browser.. I'm guessing maybe 2.6 might be where you got confused...but that's just an example of prompting the user to login, under the assumption that you are making a web interface. For your purposes, you would be authenticating with 2.4, and as far as 2.6 is concerned, you could have your script do something else if that condition is true (like log a "failed to connect" error). And instead of printResults echoing out stuff (because the tutorial assumes browser interface) you could do something else with it, like write to a db or something. IOW I don't see anything in that tutorial that requires browser interaction, other than they just did that as an example for the sake of the tutorial.

like image 42
Crayon Violent Avatar answered Oct 16 '22 08:10

Crayon Violent


The answer has been given elsewhere by an actual genius, @rkarbowski.

Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

I believe if you follow the Hello Analytics API Tutorial and then read @rkarbowski's answer, and especially the update, carefully, you will be able to make it work. I have, and I am no genius.

like image 2
Sébastien Avatar answered Oct 16 '22 08:10

Sébastien