Home / Using filters with the Google Analytics API and PHP

Using filters with the Google Analytics API and PHP

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):

https://www.google.com/analytics/feeds/data?ids=ga:7426158
    &dimensions=&metrics=ga:visits,ga:pageviews
    &sort=-ga:visits,ga:pageviews&start-date=2009-04-29
    &end-date=2009-05-28&max-results=10&start-index=1

To filter this so it only contains data for the United States add the following "filters" parameter to the request URL:

&filters=ga:country%3D%3DUnited+States

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:

$filters = "&filters=" . urlencode("ga:country==United States");

Using my PHP Class you could pass ‘urlencode("ga:country==United States")’ as the $filters parameter like so:

$data = $api->data($id, '', 'ga:visits,ga:pageviews', false, 
    false, false, 10, 1, urlencode("ga:country==United States"));

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:

$filters = new analytics_filters('ga:country', '==', 'United States');
$data = $api->data($id, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, $filters);

If you wanted to find e.g. the data for Firefox users in the United States you would do this:

$filters = new analytics_filters('ga:country', '==', 'United States');
$filters->add_and('ga:browser', '=@', 'Firefox');
$data = $api->data($id, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, $filters);

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:

$filters = new analytics_filters('ga:country', '==', 'United States');
$filters->add_or('ga:country', '==', 'Canada');
$data = $api->data($id, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, $filters);

If you have some more examples you would like to see please send me an email and I can create another post with some more ideas of what can be done.