Expand my Community achievements bar.

Set Page Google Analytics

Avatar

Level 1
Level 1

I am trying to force the Page Tracking on my site (see: Link).

What I am hoping to achieve is prepend the market name to the URI which I can pull out of my site's dataLayer (variable = dataLayer.language and thus the new Page Path: dataLayer.language.substring(0,2)+document.location.pathname e.g. /en/pagepath1/pagepath2.

I've created a script below to define this.

There is a small complication here in that some of the pages already have the country/language variation passed in the URL so I have a regular expression to search for the market/language ISO code (lower case) to help perform an if statement that returns document.location.pathname where this is the case.

My script below however, seems to be fine but in testing it is returning "undefined" does anyone why this must be the case?

function PagePath() {

 
var StandardPagePath = document.location.pathname;
 
var NewPagePath = dataLayer.language.substring(0, 2) + document.location.pathname;
 
var LocaleRegExPatt = /^(at|cn|cz|de|dk|en|eu|fr|it|nl|no|pl|pt|pt-br|se)\//

 
try {
  
if (StandardPagePath.includes(LocaleRegExPatt)) {
  
return (StandardPagePath);
  
} else {
  
return (NewPagePath)
  
}

 
} catch (err) {}

}

My second question is how can I reference this function to pass through to the page tracking i.e. set the page here:

ga('set', 'page', {{PagePath function here}} );

Thanks in advance,

0 Replies