Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Setting pagename to path in DTM

fixepah
Level 3
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
Jason_Egan
Correct answer by
Level 3
Level 3

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
Jeff__Chasin
Employee
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.

shawncreed1
Level 3
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

Jason_Egan
Correct answer by
Level 3
Level 3

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
Level 3
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]

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