views:

151

answers:

2

I'm using a Google Analytics API Class in PHP made by Doug Tan to retrieve Analytics data from a specific profile.

Check the url here: http://code.google.com/intl/nl/apis/analytics/docs/gdata/gdataArticlesCode.html

When you create a new instance of the class you can add the profile id, your google account + password, a daterange and whatever dimensions and metrics you want to pick up from analytics.

For example i want to see how many people visited my website from different country's in 2009.

//make a new instance from the class
$ga = new GoogleAnalytics($email,$password);

//website profile example id
$ga->setProfile('ga:4329539');

//date range
$ga->setDateRange('2010-02-01','2010-03-08');

//array to receive data from metrics and dimensions
$array = $ga->getReport(
        array('dimensions'=>('ga:country'),
        'metrics'=>('ga:visits'),
        'sort'=>'-ga:visits'
        )
);

Now you know how this API class works, i'd like to adress my problem.

Speed. It takes alot of time to retrieve multiple types of data from the analytics database, especially if you're building different arrays with different metrics/dimensions. How can i speed up this process?

Is it possible to store all the possible data in a cache so i am able to retrieve the data without loading it over and over again?

+1  A: 

You can load the data in a cache sure, precisely how/where the data is cached is entirely up to you. You can use anything from per-request caching (which will be pretty useless for this particular problem) to things like APC, memcached, a local database or even just saving the raw results to files. These will not make the actual retrieval of the data from Google any quicker of course.

On that note, it is likely (not having seen the code) that the requests over to Google are probably being executed sequentially. It is likely possible to extend the PHP class to allow requesting multiple sets of data from Google in parallel (e.g. with cURL Multi).

salathe
Thanks, using cURL is indeed a good idea! I'll check it out.
Paulo
A: 

Take a look at this article about simple PHP caching.

fire