Setting pagename to path in DTM | Community
Skip to main content
fixepah
Level 2
October 16, 2015
Solved

Setting pagename to path in DTM

  • October 16, 2015
  • 5 replies
  • 5407 views

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!

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 Jason_Egan

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

5 replies

Chasin
Adobe Employee
Adobe Employee
October 16, 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.

shawncreed1
Level 3
October 16, 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

Jason_Egan
Adobe Champion
Jason_EganAdobe ChampionAccepted solution
Adobe Champion
October 16, 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

fixepah
fixepahAuthor
Level 2
October 16, 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]

Chasin
Adobe Employee
Adobe Employee
October 16, 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'); }