In the Google Analytics API, there are two important ways to focus in on a particular slice of your analytics data. Segments and filters. In many cases, the result of these two very different functions are the same. But they are fundamentally different, and it is important to understand how they work particularly when looking deep into your data using the Google Analytics API.
What are you talking about? The result is the same!
In a number of cases, it makes no difference if you use a Segment or a Filter. If the query is at a session (visit) level, then it will tend to return the same result.
If you query:
ga:visitors with a segment pagePath contains X
You will get exactly the same result if you query:
ga:visitors with a filter pagePath contains X
But there are other examples where you will get very different results- that’s what we’ll explain in detail here.
When should I use a segment and when should I use a filter?
You want to use a segment if you are selecting entire visits and you want to use a filter if you are looking at specific events, pageviews etc. for all visits.
Segment – For a segment, every visit is checked to see if it satisfies the conditions of the segment. For sessions that satisfy the condition, all rows are returned. For sessions that do not, no rows are returned.
Filter – For a filter all the rows for ALL visits are considered, then only the rows that satisfy the conditions of the filter will be returned.
For the above example it makes sense that you get the same result- either way you are counting visitors and looking at visits or pageviews will arrive at the same point.
But there are cases where you will get very different results. A very good example is when looking at page views and pagePath so we’ll use that in our example.
A laboratory experiment
For this post, we’re going to work in a purely artificial data set- so we know EXACTLY what visits were recorded on our website, and therefore can understand without any gray areas what results segments give us compared to filters.
I’ve set up Google Analytics tracking on a web site, and for the period there are only three unique visitors to the site. I’m using Analytics Canvas (AC), as an easy way to create a bunch of different queries and see the differences between them. For those not familiar with AC, a green block on the canvas represents a query from the Google Analytics API.
For this post, I’ve created a canvas that has a number of different queries on a single profile for a single day that has 3 known visits. I’m using our Acme fruit and veg test site- so we’re looking at customers buying fruits and vegetables online (an under-developed eCommerce use case for certain).
To make the data perfectly clear, and create three clean unique visitors, each visit made to the site was done with a different browser– so we can see in a query including browser, exactly which pages each “unique” visit viewed. You of course won’t be able to do this on your site, but in our example it lets us really understand the data;
OK, so we know exactly what visits happened, and which pages each visitor looked at. Lets compare some Segmenting with some Filtering!
Comparing the exact same segment and filter- pagePath contains tomatoes
To illustrate lets look at a segment and a filter each one with the “same” definition – pagePath contains tomatoes.
If you refer back to the table above, we can see that only the Chrome visitor viewed the tomato page.
In Analytics Canvas we can easily set these both up with a couple of query blocks:
Segment – give me the pages looked at by visits that looked at the Tomato page
So if we look at pageviews, and set the segment as “pagePath Contains tomatoes” then the segment first discards any visits that did not visit the tomatoes page, then gives us ALL the rows from the remaining segments (even pageviews on pages other than the tomatoes page).
Filter – give me all the tomatoes page views from all the visits
The filter gives us ONLY the rows that match. As a result, we get this:
Conclusion? Sometimes segment, sometimes filter, sometimes both.
This shows us that there is a fundamental difference between segments and filters. What is even more interesting is when we combine the two. Download Analytics Canvas today to segment and filter your data.