How to Filter Google Analytics Visitors by County or Metro Area

GA v5 still doesn’t offer a stock report that filters traffic by metropolitan region akin to the AdWords DMA geo-targeting. Here’s how to segment traffic for a specific County or a metro area.

This post on the Google Analytics blog reminded me that GA v5 still doesn’t offer a stock report that filters traffic by metropolitan region akin to the AdWords DMA geo-targeting.

For Stateside users, the current relevant location hierarchy is Continent > Sub-Continent > Country > State > City

In the supporting video, ShufflePoint presents a nifty data “mashup” in a 3-D landscape layered in Google Earth, but what if you only want to analyze a segment of traffic from a specific County or a metro area?

Solution: Custom Advanced Segments

You could consult the Location report and drill down to the city level to retrieve all target cities from the list, but 1) it could be tedious work for metro areas that are spread out like Kansas City and 2) the report could be incomplete since it depends on clicks. For context, there are over 80 cities in the KC metro on both sides of the Kansas and Missouri border that could be referenced in Google Analytics.

What we’ll do instead is use ready-made lists of cities and do a bulk find & replace to package the cities into regular expressions (you wouldn’t want to filter 80 entries!).

N.B.: using Kansas City as an example, an Advanced Segment for “kansas city” would only track traffic from users with an IP address geo-located specifically in the “kansas city” city (not the metro area).

What you’ll need:

1. A list of cities by county or metropolitan area. I like the National Association of Counties since it sources its listings from the U.S. Census Bureau. Once you’ve found the County you wish to track, click on the “Places in […] County” text link.

2. A spreadsheet editor if you don’t have a ready-made list of cities and a text editor that can do multi-line find & replace (I like EditPad).

Estimated time: 5-10 minutes.

Step-by-step guide:

1. Scrape

Scrape the list of cities and clean-up in a spreadsheet if necessary (e.g. to remove any tab delimited data). You can skip this step if you already have a handy list of cities.

find a county

Import text into editor:

city info

Import text into spreadsheet and isolate city (be sure to remove all extraneous information):

excel city isolation

2. Find & Replace

Paste into your text editor and do a “find & replace all” to replace line breaks with the pipe symbol | to signify “or” in regular expression (RegEx) terms.

find and replace

The RegEx string output from the find & replace:

city regex

3. Create a Google Analytics Advanced Segment

In the Google Analytics reporting view, click on the Advanced Segments sub-tab located in the top horizontal navigation bar, and then click on “+ New Custom Segment” on the right.

4. Filter by State

In the add New Custom Segment view, the filter action is defaulted to “Include.” Type/select “Region” in the dimensions/metrics dropdown and select “Exactly matching” as the matching method, and type the State name in the blank field (the State filter prevents a widespread city name like Springfield showing up from multiple states).

5. Filter by City with the RegEx

Click on “Add ‘AND’ statement” and then click on “+Add a dimension or metric.” Type/select City in the dimensions/metrics dropdown, then select “Matching RegExp” as the matching method. Lastly, paste the RegEx string that you created into the blank field. If you wish to track multiple counties or a DMA, simply add the cities to the RegEx or create an “OR” statement if you prefer to cleanly categorize each county by statement. Click on Save Segment once you’re done.

google analytics county seg

6. Using it

To activate your new custom segment, go back to the Avanced Segments tab, and select your custom segment in the right column.