Setting pagename to path in DTM

Avatar

Avatar

fixepah

Avatar

fixepah

fixepah

15-10-2015

I've got the following code to assign the path value to a data element:

if(location.pathname){
  return location.pathname;
}
else
  return '';

Is this correct if I want to have the URL path as the pagename value in the Pages report?   The reason I ask is I am seeing full URLs as Page values in the reporting.

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Jason_Egan

Avatar

Jason_Egan

Jason_Egan

15-10-2015

I just do this and it works fine:

[img]Screen Shot 2014-09-16 at 4.21.41 PM.png[/img]

Also, if you want to see the output in the console, I'd suggest using "_satellite.notify()" and then looking at DTM in debug mode so that you don't have to worry about old browsers and "console.log()."

- Jason

Answers (4)

Answers (4)

Avatar

Avatar

shawncreed1

Avatar

shawncreed1

shawncreed1

15-10-2015

Data elements are locally scoped, which means that you would need to prefix this with "window." to access the right object.

If you update your code to the following, it should work:

if(window.location.pathname){
  return window.location.pathname;
}
else
  return '';

-Shawn

Avatar

Avatar

Jeff__Chasin

Employee

Avatar

Jeff__Chasin

Employee

Jeff__Chasin
Employee

15-10-2015

Sorry, for the console you need `console.log` instead of return. The "return" statements are for the Data Element or Rule Condition.

So, your Data Element or Rule Condition would look like:

if(document.location.pathname) { return document.location.pathname; } else { return ''; }

The code above should return the value of the path, like "/products/shoes/air-jordans/" or whatever.

To do a quick check in the console with any Web page in your browser, you can either type `document.location.pathname`, or open the console and paste this:

if(document.location.pathname) { console.log(document.location.pathname); } else { console.log('NONE'); }

Avatar

Avatar

fixepah

Avatar

fixepah

fixepah

15-10-2015

I'm not sure I'm doing this right, but when I put that code into the console, I get a syntax error:

[img]Console error for DTM.png[/img]

Avatar

Avatar

Jeff__Chasin

Employee

Avatar

Jeff__Chasin

Employee

Jeff__Chasin
Employee

15-10-2015

Hey Jason,

If your Data Element contains the code you wrote:

if(document.location.pathname) { return document.location.pathname; } else { return ''; // empty string }

You should see just the path, without the protocol, subdomain, etc.  If you copy & paste this code into the browser console, what do you see?  Is that different than the Data Element return value?

If you want to PM me the Web Property, DE and Rule, I can look in a bit.