Expand my Community achievements bar.

Join us January 15th for an AMA with Champion Achaia Walton, who will be talking about her article on Event-Based Reporting and Measuring Content Groups!
SOLVED

Setting pagename to path in DTM

Avatar

Level 3

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!

1 Accepted Solution

Avatar

Correct answer by
Adobe Champion

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

View solution in original post

5 Replies

Avatar

Employee

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.

Avatar

Level 3

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

Correct answer by
Adobe Champion

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

Avatar

Level 3

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

Employee

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'); }