The Google Analytics API allows filtering to be done on the result data before it is fetched so you could, for example, only get data for visitors from the United States. This post shows how to format the filters field in PHP and also how to use my Google Analytics API PHP Class with filters.
Read more about my Google Analytics API PHP Class.
A URL making a request to the API looks like this, where data is being fetched for the ga:vists and ga:pageviews dimensions for the period from 2009-04-29 to 2009-05-28 (I've broken the URL onto multiple lines for readability):
To filter this so it only contains data for the United States add the following "filters" parameter to the request URL:
The %3D%3D is the urlencoded version of == which tells the GA API to make an exact match. Some other possible matches are != to not match the string, =~ for regular expressions, and =@ where the data contains the string. For a full list of filtering types read the documentation.
To make sure the filters are correctly formatted when using PHP use the urlencode() function. If this is not used then the GA API will return a 400 error status because it cannot understand the data passed.
An example of encoding the country filter used above would be like this in PHP:
Using my PHP Class you could pass 'urlencode("ga:country==United States")' as the $filters parameter like so:
Another way to do this is to use my analytics_filters helper class which is in the same library file as the analytics class itself. You would then do something like this:
If you wanted to find e.g. the data for Firefox users in the United States you would do this:
If you wanted to find e.g. the data for visitors who have come from the United States or from Canada you would do this:
0 comments:
Post a Comment