Question

I am trying to create a regexp to fire a tag within Google Tag Manager on certain pages.

The issue I am having is that I do not want to fire this tag on URLs matching a querystring in them, since it is only a session identifier and I do not need the tag to fire on the pages that have a query string. These are basically duplicates and they do not need tracking in a 3rd party tracking program. I know how I could exclude them in GA, but I can't figure out how to do it for the third party tracking.

I'll detail the scenario below and what I have tried.

Example pages that come up in my URL report if I look in GA:

/page

/page/subpage?my-handsome-query-string&some-other-data

/page/subpage

/page/subpage/subsubpage

/page/?query-string-again

So what I want to do is to fire the tracking on pages that does NOT have the query string, and it is proving quite the issue.

If I put in ^/page.*[^\?] it just doesn't work. I guess I am completely using the negated character class all wrong? I can't get it working and would require some assistance on how to devise a better regexp.

Some other I tried were:

^/page/.* but this one only matched everything after /page/ but not /page.

I am not very good with regular expressions, so what I basically want to do is match /page, /page/subpage, /page/subpage/subpage etc, but not any URL that has a query string in it.

In GTM I can't create two rules that says "Include {{url path}} matching this" and "Exclude {{url path}} matching \?", so it all needs to be done within one regexp... And that totally got me at a loss.

Edit: Mike gave a good answer to solve my GTM part, but I am still interested in learning if it is possible to do above but with a single regex?

Was it helpful?

Solution

You can actually create two rules as you described.

In GTM, tags can have both Firing rules and Blocking rules. Blocking takes precedence. eg.

Firing rule:

{{url}} matches ^page/.*

Blocking rule:

{{url}} does not contain ?

Another option is to use a custom javascript macro. It is in the form of a function(){ } which can detect a query string value in window.location.search and return boolean. Then have a firing rule {{your custom fn}} equals 1.

You can also create a macro which uses the URL macro type and Query component type. The value is set to the query string without the leading ?. If the url was example.com?foo=bar this macro would contain foo=bar. Then simply add a firing rule {{query}} matches Regex ^$ or {{query}} does not contain something-that-will-never-be-in-the-url-to-avoid-regex

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top