6
votes

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?

4

4 Answers

3
votes

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

2
votes

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.

2
votes

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.

0
votes

You can use the browser to connect to the app and execute the script. (If you followed the Hello tutorial and all is working well)

Then have the script port the results into Excel or a PDF and email it out.

I have several crons that run daily using the GA API and kick out emails of daily stats etc.

You could theoretically build a PHP application that calls the API but it would much more work than just running the script on a browser.