Regular Expressions (or RegEx for short) can help speed up your use of Google Analytics as well as add extra flexibility to the way you include (or exclude) particular data and information from reports. You can think of Regular Expressions as a way to search for and match particular criteria. If you’re just getting started, then Regular Expressions can be daunting, but remember that you don’t need to memorise them and there are plenty of resources!
More advanced? Skip ahead to the cheat sheet
Lets start with a simple scenario. If you wanted to search for two particular folders, the /products/ folder and the /services/ folder within your reports, then you could use a Regular Expression like:
You will notice immediately that there are some different characters in there. The characters highlighted in red are the ones that form the Regular Expression. Lets break this down.
The brackets create a list which means that we can use it to match particular items contained within the brackets.
The pipe, which you should be able to find above the backslash on your keyword (if you are using an English keyboard) looks like a vertical line. This creates an ‘OR’ statement. So now we can see it will match products or services.
We can see that outside the brackets we have a forward slash / on both sides. This means that products or services must fall within the bracket before and after. So at this stage we are matching /products/ or /services/.
Finally we can see that there is a caret ^ on the left. This means that everything after the caret must come first in order to match. So if we had something like /special/products/ it would not match because /products/ is not first.
To go a step further, you might have pages within the folders, for example /products/phone.html. Using the the Regular Expression we have been looking at, this page would be matched by our expression, meaning it would be included within our reports. If you didn’t want this page included and you just wanted the folders, you could add a dollar sign to the end. Doing this means that ^/(products|services)/$ would only ever match /products/ or /services/ and nothing before or after.
What's your favourite RegEx?