The key difference between segments and filters

James Standen Analytics Canvas pro tips, Google Analytics API, Under the Hood 6 Comments

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

segment-vs-filter-query-overview

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;

difference between segments and filters - pageviews by browser

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:

google-analytics-segment-vs-filter

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

google-analytics-segment-with-page-path-tomatoes

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:

difference between segments and filters - filter

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.

Comments 6

  1. Great Post, a similar experience can be found in the interface with filters(the search.advanced search above the data table in reports) and advanced segments.

  2. What would be the steps to understand visits to an specific page from Mobile Device, here is the segment I’ve created but not sure if I am doing the right process:
    New segment: Device category = mobile and Advanced condition page contains “thankyou.html”

    Thanks

    1. Post
      Author

      Hi Oscar I think you are on the right track.

      To look at only Mobile visits, certainly you want to use a segment that specifies Device Category exact match mobile. The question then is if you want to look at all those visits or only the visits that went to your specific page.

      If you want to look at only those visits that also when to the page, then add another filter to the segment “and pagePath contains thankyou.html now you will see all the sessions where the device category was mobile AND a page containing that text was viewed.

      This is likely what you are looking for- the only reason to include all mobile visits and then filter on the page is if you were also including other pages and wanted pageviews from them to be counted even if the visit in question was not a visit that had come to the specific thank you page.

  3. Hi James — great overview of segment vs filter. I was wondering if you had any idea how to recreate these in bigquery? I’d like to replicate GA data in the bigquery GA export.

    1. Post
      Author

      Hi Amy,

      You certainly can reproduce these in BigQuery. It would be a matter of defining the segment as a condition on the visitID- this way segments would first select entire sessions.

      The filter would be a condition in the form of a where clause afterwards.

      If you are using Analytics Canvas and its BigQuery connector, it can generate much of the SQL for you, particularly the filters.

      Using Canvas the steps might be to first do a query to get the specific visitIDs that you want, then write that data into a table, then apply a query with a where clause that has the filter conditions on that to have the final results.

  4. Pingback: Come Filtrare i Dati nelle Tabelle di Google Analytics

Leave a Reply

Your email address will not be published. Required fields are marked *