Expand my Community achievements bar.

SOLVED

Match method with Regex to fire tag on certain site pages

Avatar

Level 1
Level 1

I'm trying to fire a Google GTAG script only on certain language variations of our site, I can't create a condition to fire this in our Tag Manager as it will impact other tags and pixels firing on the page rule.

I wan't the tag to fire only on certain language variations: '^/(at|cn|cz|de|dk|en|eu|fr|it|nl|no|pl|pt|pt-br|se)/)'

I'm trying to achieve this by using match method with Regex but I am getting a syntax error that I can't figure out on the below script: "Uncaught SyntaxError: Unexpected token ^" which I believe to be coming from the '^' at the start of regex string, however I need to only pull back pages that include the ISO market name (e.g. /en/) at the start of a URl string (e.g. /en/mypage/mysubpage)

if (location.pathname.match(^\/(at|cn|cz|de|dk|en|eu|fr|it|nl|no|pl|pt|pt-br|se)\/); {

{

  
var script = document.createElement('script');

  script
.setAttribute('src','https://www.googletagmanager.com/gtag/js?id=DC-XXXXXX');

  document
.head.appendChild(script);
  window
.dataLayer = window.dataLayer || [];
  
function gtag(){dataLayer.push(arguments);}
  gtag
('js', new Date());
  gtag
('config', 'DC-XXXXXXX');
}
 
}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

should be

when using

the expectation is a regExp starting and ending with / .   For example :

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

should be

when using

the expectation is a regExp starting and ending with / .   For example :