Using custom filters with Google Analytics

Filters are short statements that you can apply to a Google Analytics widget to refine the data that’s displayed and drill down to particular metric you're interested in.

Where available, filters can be applied from within your widget’s setup screen by adding a Custom filter statement to the Filter by field.

Note

While our Google Analytics Number and Leaderboard widgets also offer pre-built filters, you may find this article useful for understanding filters in general and how to use them.

What is a filter statement?

A filter statement consists of several elements that tell the Google Analytics API exactly what data needs to be displayed:

  • Dimensions and Metrics
  • Operators
  • Strings and Values

Example of a Google Analytics filter

Let’s say you are interested in building a Number widget that displays the number of visitors this month to the contact page on your website. By default, the widget will pull through the total number of visitors across the whole of the website you’re tracking with Google Analytics. However, applying the filter ga:pageTitle==Contact would allow you to refine this and display only the visitors to your page titled Contact.

Core Reporting filters vs Real-Time Reporting filters

Google Analytics offers two categories of data - Core Reporting metrics and Real-Time Reporting metrics. The filter statements you write will differ slightly depending on the widgets you use.

Core Reporting filter statements

Google Analytics' Core Reporting API lets you track longer-term data and trends (eg. data for time periods of Today, This Week, Past 28 days, Past 365 days, etc.)

Core Reporting filter statements always begin with ga: and use the dimensions and metrics available through the Core Reporting API .

The following Geckoboard Google Analytics widgets request this data:

  • Number widget
  • Leaderboard
  • Top Sources List
  • Top Platform List
  • Top Location List
  • Top Pages List
  • Top Products
  • Product Sales Bar Chart
  • Goals Line Chart
  • Events Line Chart
  • Visitors Line Chart
  • Campaign Line Chart
  • E-commerce Line Chart

Example of a filter statement for tracking Core Reporting data

Where ga:source is the dimension or metric you want to filter on, the operator is how you want the field to be compared (equals ==, not equals !=, contains =@, etc.), and twitter is the string or value of the comparison.

Real-time Reporting filter statements

Google Analytics' Real Time Reporting API lets you track data and events occurring currently on your website (e.g. number of visitors currently on your website, number of people currently performing a particular Event Action on your pricing page, etc).

In Geckoboard, Google Analytics widgets and metrics with the words Current or real-time in their title or description use this API and let you track real-time data.

Tip

Widgets with the word Current in their title also refresh at a faster rate than others, as they serve only real-time metrics.

Real-Time Reporting filters always begin with rt: and use the dimensions and metrics available through the Real-Time Reporting API .

The following Geckoboard Google Analytics widgets request real-time data:

  • Current Sources List
  • Current Platform List
  • Current Location List
  • Current Page List
  • Current Visitor Gauge
  • Current Visitor Map

Example of a filter statement for real-time data

Where rt:source is the dimension or metric you want to filter on, the operator is how you want the field to be compared (equals ==, not equals !=, contains =@, etc.), and twitter is the string or value of the comparison.

Dimensions & Metrics

The cornerstone of the filter statement are the dimension(s) or metric(s) that the widget is supposed to filter, such as the source of traffic or the URL of a specific page.

Almost the full range of dimensions and metrics can be added and even combined within a filter statement to create a very specific set of data within a widget.

Note

Google Analytics doesn't allow for certain combinations of dimensions and metrics to be queried together.

For example, AdWords related metrics Cost, Cost per Conversion, Cost per Goal Conversion, Cost per Transaction) and Event Tracking dimension-based filters (Event Action, Event Category, Event Label) cannot be used together.

If you've used a forbidden combination, you may see this error message:


Examples of dimensions and metrics include:

Dimensions

  • browser
  • campaign
  • city
  • country
  • date
  • day
  • goalPreviousStep1
  • keyword
  • language
  • medium
  • month
  • pagePath
  • pageTitle
  • productBrand
  • screenResolution
  • source
  • userAgeBracket
  • year

Metrics

  • bounceRate
  • goalXXCompletions
  • impressions
  • itemRevenue
  • sessions
  • timeOnPage
  • transactions
  • totalEvents

Operators

Operators work with dimensions and metrics to allow you to specify how your data should be manipulated.

Let's say you’ve selected city as the dimension you’re interested in: do you want to include only data from a specific city, exclude data from a specific city, include only data from a specific list of cities, or display data from cities that contain a specific word? Operators let you take the basic city data and do much more with it than simply showing it.

Dimension filter operators

Operator Description Example
== Exact match Aggregate metrics where the city is London. ga:city==London
!= Does not match Show visitor data for all sources apart from Geckoboard. ga:source!=geckoboard
=@ Contains substring Show data for any page whose URL contains the word ‘learn’. ga:pagePath=@learn
!@ Does not contain substring ga:pagePath!@404
=~ Contains a match for the regular expression Show data for visitors from countries starting with ‘united’. ga:country=~%5Eunited.* (%5E is the URL encoded from of the ^ character that anchors a pattern to the beginning of the string.)
!~ Does not match regular expression Exclude data for visitors from countries starting with ‘united’. ga:country=~%5Eunited.*

Metric filter operators

Operator Description Example
== Equals Show data for visitors who have had exactly 8 sessions. ga:sessions==8
!= Does not equal Show data for non-first time sessions. ga:sessions!=1
> Greater than Show data for visitors who have over 9 sessions. ga:sessions>9
< Less than Show data for visitors who have fewer than 3 sessions. ga:sessions<3
>= Greater than or equal to Show data for visitors who have 9 sessions or more. ga:sessions>=9
<= Less than or equal to Return results where the time on the page is 10 seconds or less. ga:timeOnPage<=10

How to combine filters using AND and OR operators

Filters can also be combined using AND as well as OR logic to create a powerful tool for monitoring very specific elements of your website.

AND operator

The AND operator is defined using a semi-colon (;). It is preceded by the OR operator and can be used to combine dimensions and metrics in the same expression.

Example

Country is United Kingdom AND the source is Facebook AND sessions are greater than 5: ga:country==United Kingdom;ga:source==Facebook;ga:sessions>5

OR operator

The OR operator is defined using a comma (,). It takes precedence over the AND operator, but cannot be used to combine dimensions and metrics in the same expression.

Example

Visitors using either Windows OR Macintosh operating systems: ga:operatingSystem==Windows,ga:operatingSystem==Macintosh

Strings & Values

The final part of a filter statement to be aware of is the string or value. Set at the end, the string or value should match those available for the dimension or metric you’ve defined in the first part of the statement, i.e if your dimension is city you must choose the name of a city to filter or else you will trigger an error.

Example filters

Here are some example Google Analytics filters to get you started:

Filter statement Description
ga:source==twitter.com Traffic source is Twitter.
ga:keyword==geckoboard Referring keyword is ‘geckoboard’.
ga:pageTitle==Contact Title of page is ‘Contact’ (exact match).
ga:pagePath=~contact URL of page contains ‘contact’ anywhere.
ga:country==United Kingdom Visitor country is United Kingdom.
ga:country==United Kingdom;ga:city!=London Country is United Kingdom and City is not London.
ga:eventCategory==Reports,ga:eventCategory==Studies Show all events with the category Reports or Studies.
ga:eventCategory==Reports;ga:eventLabel!=Share Show all events with the category Reports but exclude those with event label Share.
ga:browser==Firefox;ga:operatingSystem==Windows,
ga:operatingSystem==Macintosh
Firefox users on (Windows OR Macintosh) operating systems.
ga:operatingSystem==Windows,
ga:operatingSystem==Macintosh;
ga:browser==Firefox,ga:browser==Chrome
Operating system is (Windows OR Macintosh) AND browser is (Firefox OR Chrome).

Tip for testing your Google Analytics filters

Google Analytics' Query Explorer is a useful tool that, amongst other things, lets you test filters.

Was this article helpful?

Awesome! 👍  Thanks so much for your feedback!