REGEXP_REPLACE Examples in Google Data Studio

Figuring out the correct regular expression is by far the most difficult part of using formulas in Google Data Studio. Sometime in the future, I’ll write a longer post on just that. Today I wanted to get you started with some examples and case studies to start trimming your data for your dashboards.

The function REGEXP_REPLACE creates a dimension by extracting a subset of an already available source dimension then replacing it with something else. Google uses Google RE2 regular expressions, so it’s worth a read to get a handle on how to use regular expressions in Google Data Studio.

If you still don’t understand REGEX after reading that link, keep trying. I’ve read it 5 times and I still don’t have a full grasp like I should.

Example #1 – Remove URL Parameters with REGEXP_REPLACE

Best practice is to have multiple views in your Google Analytics. One with all data and at lease one more that has URL parameters removed. If you’ve slipped on this, no worries. REGEXP_REPLACE to the rescue.

To remove the URL parameters, click “Add Dimension” then “Create Field”. Copy the below code into the formula box and hit “Apply”.

REGEXP_REPLACE(Landing Page,”[?].*”,””)

Example #2 – Remove and Replace Height of Display Resolution with REGEXP_REPLACE

If for whatever reason, you only care about the width of the display your visitors are seeing your website on, then this is your formula. (There’s a responsive web design need in here somewhere)

The below formula will find “x” and everything after it and replace it with a space and “Pixels”

REGEXP_REPLACE(Screen Resolution,'(x.*)’,’ Pixels’)

Example #3 – Annoying your Canadian Boss with REGEXP_REPLACE

This is ill advised during a recession. This is also not career advice. If your boss is from someplace the US like Idaho, feel free to replace that with “Not a real place”.

The below uses much less convoluted REGEX so have a ball with this one.

REGEXP_REPLACE(Country,’Canada’,’Merica #2′)

Other Text Functions in Google Data Studio

When you finally decide to get smarter about all things digital and get your first second income stream built to make that WiFi Money. You need to sign up for my Substack below. Start with the article on Your Path to WiFi Money.

If you're looking at how to start your own website, I've created an easy guide for that too.