Using Regex to Get the Most Out of Tag Inspector

Amanda GrossmannGeneral, How-ToLeave a Comment

Tag Inspector is an amazingly powerful tool that is highly configurable, allowing you to validate a variety of tags across many platforms. One way you can take advantage of this flexible platform is by using our regex-matching feature within our validation rules.

Regex? What’s a Regex?

Regex is an abbreviation for regular expression, which is a sequence of characters that may be used in a search process for a certain pattern. Using a regex can allow greater ease and flexibility when searching for something. This post will review how to use regex within Tag Inspector.

Using Regex in Tag Inspector

Let’s say I have some Floodlights set up on my site and I want to verify that the correct group tag string was used for my marketing campaign across certain categories of pages.

I would start by creating a validation rule and indicating that I want to use “HTTP Request Matches Regex” for my rule:

RegEx

Let’s say that I know that the activity tag string used in my tag implementation was bea19a. I would want to check that this is sent within the HTTP request by putting the following:

cat=bea19a

Let’s take it step by step, to see how I came up with this. If you’re interested in checking that the activity string was sent property in the HTTP request, you’ll want to check the “cat” parameter, as identified in Google documentation.

RegEx

So, off the bat, I know that I want to include cat= something in my regex. We also know that the activity string I’m interested in tracking is bea19a, so what I’m looking for is actually cat=bea19a. Having this will search for this text match exactly within the HTTP request. Easy, right?

RegEx

Let’s go on to the second part of our example, in which we specify the criteria for when our rule should be applied.

Using Regex to Specify Validation Rule Execution Criteria

In order to finish setting up my validation rule, I’ll need to specify the criteria for when my rule should be applied. As I mentioned before, my Floodlight campaign has been deployed on certain category pages that I’m interested in validating with Tag Inspector. My campaign has launched for the following category pages:

  • Makeup → Eyeshadow (abeautystoreonline.com/makeup/eyeshadow)
  • Makeup → Lipstick (abeautystoreonline.com/makeup/lipstick)
  • Skincare → Moisturize (abeautystoreonline.com/skincare/moisturize)
  • Skincare → Acnecare (abeautystoreonline.com/skincare/acnecare)

I can craft a regex to match and validate on these exact category pages. Let’s do it.

RegEx within Tag Inspector

Let’s start by specifying the top-level categories for our regex - makeup and skincare.

/makeup|/skincare

The “|” symbol is used to indicate OR. So far, our regex will match for URLs that have makeup OR skincare. This is good, because it matches our original criteria, but it’s also not that great because it isn’t specific enough. The regex we have right now will partially match all of these pages:

  • Makeup → Eyeshadow (abeautystoreonline.com/makeup/eyeshadow)
  • Makeup → Lipstick (abeautystoreonline.com/makeup/lipstick)
  • Makeup → Lip Liner (abeautystoreonline.com/makeup/lipliner)
  • Skincare → Moisturize → Product 1 (abeautystoreonline.com/skincare/moisturize/product01)
  • Skincare → Anti-aging (abeautystoreonline.com/skincare/angi-aging)

As you can see above, we’re matching more pages than we’d like right now, because we’re matching product pages and we’re also matching other subcategories like Lip Liner and Anti-aging. We need to make our regex more specific so that it only targets the categories in our marketing campaign.

In order to get more specific, I’ve added the following subcategories - eyeshadow (under Makeup) and moisturize (under Skincare). I’ve also put parentheses around my two examples so that my OR operator works properly. It will look for /makeup/eyeshadow OR /skincare/moisturize.

  (/makeup/eyeshadow)|(/skincare/moisturize)

You’ll notice this still isn’t checking for all our sub-categories yet. Let’s go ahead and add the other sub-categories:

 (/makeup/eyeshadow)|(/makeup/lipstick)|(/skincare/moisturize)|(/skincare/acnecare)

This is great progress! Now we have specificity - but it’s still not that great for a couple of reasons. First, this regex will also match product pages for these categories and subcategories - this is not what we want. Secondly, it’s a little repetitive. One of the benefits of using regex is that you can use regex patterns to match without repetition. For example, instead of having that regex above, we can instead use this simplified version:

(/makeup/(eyeshadow|lipstick))|(/skincare/(moisturize|acnecare))

This is much shorter than the version before and it will now match more closely to what we want:

  • Makeup → Eyeshadow (abeautystoreonline.com/makeup/eyeshadow)
  • Makeup → Lipstick (abeautystoreonline.com/makeup/lipstick)
  • Makeup → Lip Liner (abeautystoreonline.com/makeup/lipliner)
  • Skincare → Moisturize → Product 1 (abeautystoreonline.com/skincare/moisturize/product01)
  • Skincare → Anti-aging (abeautystoreonline.com/skincare/angi-aging)

How do we specify our regex so that we only match category pages and not product pages? We will use a character, $, that says to only look for this exact end of the pattern.

(/makeup/(eyeshadow|lipstick)$)|(/skincare/(moisturize|acnecare)$)

Now that we’ve added that, we can see that this improves our regex matching:

  • Makeup → Eyeshadow (abeautystoreonline.com/makeup/eyeshadow)
  • Makeup → Lipstick (abeautystoreonline.com/makeup/lipstick)
  • Skincare → Moisturize (abeautystoreonline.com/skincare/moisturize)
  • Skincare → Acnecare (abeautystoreonline.com/skincare/acnecare)
  • Makeup → Lip Liner (abeautystoreonline.com/makeup/lipliner)
  • Skincare → Moisturize → Product 1 (abeautystoreonline.com/skincare/moisturize/product01)
  • Skincare → Anti-aging (abeautystoreonline.com/skincare/angi-aging)

We are almost done! One thing to remember about regex is that you have to escape special characters like \, /, { and some others. If you do not “escape” these characters, they will be interpreted differently by the regex engine. So, we actually need to escape those forward slashes (since they are special characters) and then our regex will be done. We will do this by “escaping” them with a “\” or backslash character:

(\/makeup\/(eyeshadow|lipstick)$)|\/(skincare\/(moisturize|acnecare)$)

So we’ll go ahead and add this regex to our validation rule and it will look for that activity string on the category pages that we’ve set up for our marketing campaign.

Tag Inspector: RegEx Rule

If you want to test your regex a bit more easily before entering it into Tag Inspector, you can test it out at regexr.com.

Facebook Comments

Article Filed Under