Page name issue | Community
Skip to main content
cathyw49544988
Level 5
December 19, 2017
Solved

Page name issue

  • December 19, 2017
  • 14 replies
  • 8718 views

Hi, I am trying to create page name based on URL. I modified the URL and form the page name. I did some changes like:

1. remove www.

2. remove.html

3. change / to |

Then I run into a very strange issue. For example a page, 5034 times the page name was correct. But 136 times, the page name showed wrong. somehow all the '-' disappeared and replaced with space. I checked the URL for those pages were correct. I am wondering anyone has any idea maybe what caused this?

Thanks,

-Cathy

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by jdonley

Okay well I don't see anything in this code that directly replaces anything with a space. My next guess is the dataLayer[0]['pageURL'] variable you reference sometimes has spaces in it.  Maybe whatever outputs this data layer variable has it when it is output sometimes. Or maybe there is some other code elsewhere that alters it sometimes. Or, maybe you have other code elsewhere that sets your page name using different code, that executes/overrides this code, sometimes.

I'm not sure what more I can really do to try and help without at least a link to the site to poke at. If you can provide a link to the site (ideally, a page where you see this happening in the reports), I can try and dig deeper into it, but you're definitely getting into "need to be a real coder to QA this" territory. Things like outputting console logs at specific points in DTM, adding breakpoints/watches on variables in the js console.. not really feasible to try and walk you through that stuff on the forums..

Sidenote: At face value it looks like you are piggybacking off of Google Tag Manager's (GTM) dataLayer array.  This is generally a bad idea.. mainly because unlike most data layer conventions, GTM pushes data layer states to an array, and there's no guarantee which element in the stack is the one you actually want for a given event. For example I assume you reference dataLayer[0] with the assumption it will contain relevant page view data from initial page load. Well there's no guarantee this will always be the case with GTM.  This logic also kinda gets tossed out the window if you have SPA pages, because you are effectively only looking at original full page load data (assuming its even first in the queue).

14 replies

cathyw49544988
Level 5
December 19, 2017

Another I want to add is, I used data element to translate the pageName. in the pageload rule, I assign the pageName dataelement value to Page Name variable and also to eVar6 and prop6. Right now, all of those 3 all have the wrong value. To me, the problem happened at data element level. So I think we can rule out the possibility of processing rules. Actually I don't have a lot of processing rules. I already checked. And in DTM, I don't have  a page load rule for mobile device ....... Is there anything else I can check?

Level 8
December 19, 2017

Please help us help you.

Can you post what the data element configuration looks like?

cathyw49544988
Level 5
December 20, 2017

I used Custom Script. The code is as following. Thanks for your help.

var page;

var locale;

if(typeof dataLayer != "undefined" && dataLayer != null ){

  if(typeof dataLayer[0]['locale'] != "undefined" && dataLayer[0]['locale'] != null ){

    locale = dataLayer[0]['locale'].toLowerCase();

    locale = locale.replace(/_/, '-');

  }

  else{

    locale = 'locale can not find';

  }

  if(typeof dataLayer[0]['pageURL'] != "undefined" && dataLayer[0]['pageURL'] != null ){

    page = dataLayer[0]['pageURL'];

 

    if( page.indexOf('www.mycompany.com') != -1 ){   

        page = page.replace(/www.mycompany.com\//, '');

       page = page.replace(/www.mycompany.com/, 'Home');

        page = page.replace(/index.html/, 'Home');

    }

    else{

      page = page.replace(/www./, '');

    }

    page = page.replace(/.html/, ''); 

    page = page.replace(/\//g, '|');

  }

  else{   // can not find URL in dataLayer

    page = getNonAEMName();

    }

}  

else{

  page = getNonAEMName();

}

return page;

function getNonAEMName(){

  var value = [location.host, location.pathname].join('');

  value = value.replace(/www./, '' );

  value = value.replace(/.html/, ''); 

  value = value.replace(/\//g, '|');

  return value;

}

jdonleyAccepted solution
Level 8
December 20, 2017

Okay well I don't see anything in this code that directly replaces anything with a space. My next guess is the dataLayer[0]['pageURL'] variable you reference sometimes has spaces in it.  Maybe whatever outputs this data layer variable has it when it is output sometimes. Or maybe there is some other code elsewhere that alters it sometimes. Or, maybe you have other code elsewhere that sets your page name using different code, that executes/overrides this code, sometimes.

I'm not sure what more I can really do to try and help without at least a link to the site to poke at. If you can provide a link to the site (ideally, a page where you see this happening in the reports), I can try and dig deeper into it, but you're definitely getting into "need to be a real coder to QA this" territory. Things like outputting console logs at specific points in DTM, adding breakpoints/watches on variables in the js console.. not really feasible to try and walk you through that stuff on the forums..

Sidenote: At face value it looks like you are piggybacking off of Google Tag Manager's (GTM) dataLayer array.  This is generally a bad idea.. mainly because unlike most data layer conventions, GTM pushes data layer states to an array, and there's no guarantee which element in the stack is the one you actually want for a given event. For example I assume you reference dataLayer[0] with the assumption it will contain relevant page view data from initial page load. Well there's no guarantee this will always be the case with GTM.  This logic also kinda gets tossed out the window if you have SPA pages, because you are effectively only looking at original full page load data (assuming its even first in the queue).