6

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?

hek2mgl
159k31 gold badges263 silver badges280 bronze badges
asked Mar 9, 2013 at 0:57

4 Answers 4

3

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

Moak
12.9k27 gold badges117 silver badges168 bronze badges
answered Mar 9, 2013 at 1:01
Sign up to request clarification or add additional context in comments.

2 Comments

First: never heard of it :). Also: I have years of data in GA, which I am interested in accessing now...
You can export your data from GA and import it do piwik but that's just example there is many other opensource solutions however I don't know others my self which are build in php
2

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.

answered Mar 9, 2013 at 3:53

7 Comments

Hi @crayon-violent. Thanks for your answer but I still don't follow. After re-reading the tutorial, I still am unable to connect without user interaction. I have to login to Google and then authorize the "app". Also: everything hinges on a PHP session, so how would I be able to run that script from a local Cron job?
well i don't really know what to tell you except maybe post your code as how you tried to apply it..if you basically just cut/pasted the tut then yeah, it "requires" interaction because that's the convention of the tut. You have to remove the "interaction" elements of it.
OK, but that's precisely where I'm stuck: how do I remove the interaction element? I have indeed copied and pasted from the tutorial, and what I have as a result works as advertised. But I cannot go from there to a no-interaction version. Can you help?
I would be retrieving basic data like number of unique visitors in a given month and then save that do a monthly stats MySQL table (on the same server that runs the GA query). Then I would use that local data to display past months statistics on my backend. Another use case would let users on my website (no more than 5-10 people a day) run a query to fetch live data from GA for a specific period of time. That would allow them to stay on our website without having to connect to GA, and also I could present on the same page some other business data on the same time period.
okay, so instead of querying GA and echoing out the results like in the tutorial, you'd query GA and then connect to your db and insert the data. The tutorial makes a call to printResults to output the browser. So make your own function saveResults that puts the data in your db and call saveResults instead. As far as your idea to allow your users to do live queries to GA...well that's actually exactly how the tutorial is already set up...you'd just alter the "query" to GA to be what you want, and alter how it's presented to the user by changing printResults
|
2

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.

answered Mar 10, 2013 at 0:43

Comments

0

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.

answered Dec 4, 2014 at 19:42

1 Comment

It's more than theoretical, as it's been working for me now for well over a year... Also I like to take days off and I can't always rely on having a browser on hand. So the server does the routine work for me :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.