Google Analytics Cron API Requests Using PHP

After reading the GA API documentation and looking at the PHP example code, I cannot verify how to authenticate without the user being present.

For our site, we want to pull the GA data in one hour and compare against the additional metrics on our site. We aim to do this on CRON, which will then inform us of the correlations between current site behavior and historical GA trend data.

Is there a way to emulate authentication in PHP so that it can be done with CRON, assuming the email and password are known to the GA account?

Many thanks

+3


source to share


1 answer


Ok, after asking, I think I got what you mean. Below is a huge generalization.

So, I assume you are using some kind of framework, but if not, that is not a problem. You can create a separate php file in your shared folder, which will check which ip we requested it from ($ _SERVER ['REMOTE_ADDR']), if it is local (Apache works fine by default, additional configuration is required if you use a proxy) server server like nginx), then you can proceed to fetch GA data.

As described here: http://code.google.com/p/gapi-google-analytics-php-interface/

Quite simple: include the GAPI class and then run

$ga = new gapi('email@yourdomain.com','password');
$ga->requestReportData(145141242,array('browser','browserVersion'),array('pageviews','visits'));

      




If the code above doesn't work, you will need to use OAuth2 to access GA.

Description on how to do it:

+2


source







All Articles