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

Parsing URL minus domain & QSP to Page Name

Jmaguire
Level 2
Level 2

Hi,

 

Apologies if this is mentioned but I couldn't find anything in Adobe documentation or on the forums.

 

A site I'm working on doesn't use a data layer and currently only passes URL into the Page Name variables. I'm looking for a workaround to pass URL minus domain and query string parameters into the Page Name var, possibly with slashes replaced by colons.

 

Is there a rule for this I can use within Adobe Launch? Assume it may require some custom code.

 

Thanks,

James

1 Accepted Solution
Brian_Johnson_
Correct answer by
Community Advisor
Community Advisor

@Jmaguire -

The simplest approach might be to create a "page name" data element that parses the pathname per your requirements. 


Maybe start with something like this:

var path = document.location.pathname;

if (path === "/") {
  // specify a default value if the path is "/" (assume home page)
  path = "homepage";
} else {
  // remove leading "/", then replace all remaining "/" with ":"
  path = path.replace(/^\//, "").replace(/\//g, ":");
}

return path;

 

Then, in your rules, reference the data element. 

View solution in original post

11 Replies
Brian_Johnson_
Correct answer by
Community Advisor
Community Advisor

@Jmaguire -

The simplest approach might be to create a "page name" data element that parses the pathname per your requirements. 


Maybe start with something like this:

var path = document.location.pathname;

if (path === "/") {
  // specify a default value if the path is "/" (assume home page)
  path = "homepage";
} else {
  // remove leading "/", then replace all remaining "/" with ":"
  path = path.replace(/^\//, "").replace(/\//g, ":");
}

return path;

 

Then, in your rules, reference the data element. 

Brian_Johnson_
Community Advisor
Community Advisor
@Jmaguire - document.location.pathname is ONLY the file path of the URL. It does NOT include protocol, hostname, query parameters, or has values. See the following link for more information: https://developer.mozilla.org/en-US/docs/Web/API/URL
jkm-disco
Level 5
Level 5

Just as a subtle note on this method, you will not get the expected names from this method if a user saves the page onto their computer. It will instead populate with the path on the person's computer where the HTML document lives. If you wanted to avoid this data, you may either want to run s.abort on locally saved pages or consider an option for setting pagename based on internal document context.

PratheepArunRaj
Moderator
Moderator

Dear Jmaguire,

Why do you want to capture the pathname + Query String Parameters in the pagename? I would not recommend it because it will result in duplication of the pages.

URL: https://experienceleaguecommunities.adobe.com/adobe-analytics-questions/message-id/21725?q=test

Pagename: /adobe-analytics-questions/message-id/21725?q=test

You are trying to achieve the above right? If yes, please do not go for it, particularly in the Page Name variable.

Thanks, Arun.

Jmaguire
Level 2
Level 2

--- duplicate comment, Experience League site being very buggy today! ---

Jmaguire
Level 2
Level 2

Hi @PratheepArunRaj, I want to capture path name MINUS domain and QSPs.

 

Brian's answer is very helpful but still includes QSPs and as you mention this would cause many duplicate pages to be captured separately.

Brian_Johnson_
Community Advisor
Community Advisor
The example I provided does NOT include query parameters.
PratheepArunRaj
Moderator
Moderator
Yes... I would ask you to go for Pathname along excluding QSPs as shared by Brian... Better solution...