- Hi there, and welcome to another video of
measureschool.com where we teach you the data driven way
of digital marketing.
My name is Julian,
and today I want to talk with you
about the use of regular expressions
within Google Tag Manager and Google Analytics
to gain flexibility when searching
and matching URLs in both tools.
We learned the very basics of RegEx
and I'll give you some more resources
to deepen your learning.
All and more coming up so let's dive in.
(upbeat music)
Now regular expressions can be used in Google Analytics
in several places when building for example
filters, goals, or custom segment.
And in Google Tag Manager they're quite handy
when defining triggers and filtering down on certain cases.
But let's start at the beginning.
What are regular expressions?
To put it simple, regular expressions
also called RegEx
are a way of finding the needle in the haystack.
It's like the command F shortcut in your browser
when you're searching through a bunch of text.
Just more powerful.
So let me demonstrate.
Here in our Google Analytics account
we'll go down to the behavior site
and all pages report.
And we get a bunch of different pages
that were visited on our site.
Now if you wanted to search through them
we can use this search bar
but let's go with the advanced option
and we can put in here anything we want.
Let's say we want to match up our login here.
And apply this filter and we'll only be shown
the pages that contain our search phrase in this case.
Now we have the different options here
of searching for this search phrase,
and we click on it and we see contains it
but also the matching RegEx option.
So let's try that out.
Let's click apply.
And we see we basically get the same results.
Well that's because RegEx in it's basic form
is like the contains option.
Unless you use special characters
that are defined in the rule set of regular expressions.
So for example if I only wanted to match up this login
and nothing that is attached to the back of it
like here with the query strings,
we can actually use the dollar sign to indicate that the
matching should stop here.
Nothing else should come afterwards.
Let's apply this and we see that now our search result
only contains the login with the slash.
Let's go back here for a moment,
and I'll show you another example of regular expressions.
Let's say we want to match two components here,
one is the product and one is the login.
How would we do that?
We can go into the advanced options
and choose the matching RegEx
and we can input our login
and then the O operator within
regular expressions is the pipe.
Put in a pipe, and put in our second expression,
which in our case would be the products.
Press enter.
And now we get all the different products and logins
and all the other variations that contain this expression.
Now again if we only wanted to match up
these first two variations
you would need to put in the dollar sign.
So the dollar sign marks the end of our search term
and therefore we get the results back as expected.
So let's go with another example here.
Let's go on all pages.
And search for all the different websites that have
the forward slash Google in them.
So we get these results back and I'm actually
only interested in these results that
have the Google in front of them
so we want to make sure that it actually begins with the
slash Google in the URL.
How would we do that?
Well, the same as there is a marking
of the end of the string,
there's also marking of the beginning of the string,
with the caret.
So let's try this out.
And we only get pages where slash Google is actually
at the beginning of the page path.
So this is how you can easily mark the
beginning and the end
and also put a or condition inside of your search term.
Now there are many other different keywords
which if you are interested in learning more
you can find out at
measureschool.com/regex
where we'll give you more resources on these keywords
but also how to dive deeper into regular expressions.
I want to quickly show you how you can actually use
them in Google Analytics.
We already saw one search functionality here
but another use case would be in the goals section.
If you go into our admin section and then under goals,
let's say we want to match up everybody
who visited our login page
and count this as a goal.
We'll choose the custom option down here
because we don't want to use any of these templates,
and this will be our login page goal.
And then we'll go with destination because this is the
page path that we are actually matching this to.
And we can choose as our equals too
but also regular expressions
and you see that there's no ends with option here
so it might be quite handy to
use regular expressions in this case.
So we can go ahead and put in our login
and if you wanted to mark the end of the string.
Again we can use the dollar sign.
As we have used the regular expression option down here
if you go into the funnel
all these different funnel steps that we might be
configuring would also match up with regular expressions
to the page path field.
So keep that in mind.
It can be very powerful if you want to
match up different pages as the path step
to display in your funnel visualization.
For now we won't do this.
And just verify this goal.
And we can see we have a conversion
rate on this page of 9.3%
which is obviously high
because the login page is a pretty popular page on our site.
So we have a little bit of a confirmation here
that this actually works.
If you want to see which pages are matched up,
I would urge you to go back into the reporting
and go to your all pages report
and put in the same regular expression
into the search field.
Then you can go ahead and save this
and it will forthcoming record this as a goal
in your Google Analytics.
Now what about Google Tag Manager?
If you go into Google Tag Manager,
I have an example prepared here.
We have a Google Analytics page view tracking
but also an event that is firing on two different triggers,
the flying ninja trigger,
and the happy ninja trigger,
and this is actually on our demo page,
two product pages that we have right here.
Now it will fire an event called important products
and the triggers when we click into them we see,
the triggers just aren't true when the page URL
contains flying ninja
and the same when we go into the happy ninja
page URL contains happy ninja.
You can demonstrate that by clicking on this product
and we see that our important product event
fires as expected.
Now this is all great
but it could get very messy when you attach
more and more different firing triggers to this event.
So how could we do this with regular expressions?
Well let's go into this flying ninja trigger
and rewrite that to match up to flying ninja or happy ninja.
And under the firing on options,
a lot of people think you can just
click on the plus sign here
and put in the page URL
contains happy ninja.
But this would actually not work
because the whole trigger will turn false
because in this scenario the page URL needs to contain
flying ninja and happy ninja
which is not possible on our side
and therefore these, this trigger will fail
and our tag will not fire.
Please remember if you have multiple filter options
these fill a value add between each other with
the and function so this and this needs to be true.
Now with RegEx we can diffuse the situation
by choosing in the options here the matches RegEx
and add our happy ninja with the pipe
to the end of this search term.
Let's get rid of this and save this trigger.
And with this new trigger attached to our tag
we don't need the happy ninja trigger anymore
so let's go into our tag and get rid of that.
Save our tag.
Of course as always do a little bit of a quality check.
Go to our product flying ninja page
and we see our important products event fired.
Now our other case the happy ninja t-shirt
we also see hopefully, yep that the Google Analytics event
important products has fired.
When we click into the tag
and choose the right event first
click into the tag.
You can see the firing trigger turn true
because the page URL matched the option
flying ninja and happy ninja.
So this is how you can also control
whether your RegEx is working within Google Tag Manager.
So the RegEx option within Google Tag Manager
can be quite powerful when trying to
match up different options in the filter section
of the trigger.
Now obviously there is much more
to the use of regular expressions
and it can become quite complicated
if you're matching up different patterns,
and especially combining
different patterns of regular expressions
in one regular expression.
It's really a path that I want to send you down
because it can be quite powerful
and take your Google Analytic skills
and Tag Manager skills to the next level
so we've compiled more resources under
measureschool.com/regex
for you to check out and dive deeper
into regular expressions.
(upbeat music)
And that's already it with this week's video of
measureschool.com
If you have any questions
please leave them in the comments below
and if you liked this video
give us a thumbs up,
and subscribe to this channel
so we can bring you the latest developments
in digital measurement every week.
My name is Julian.
Until next time.