Expand my Community achievements bar.

SOLVED

value from the url

Avatar

Level 4

Hi,

I am capturing the value of the pageName from URLs and separating it by ":"

 

.Current url is https://www.xyz.jp/abc/def/

 

Expected pageName = xyz:jp:abc:def

 

How can I go about getting the above format value?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

This sounds a lot like your previous question: https://experienceleaguecommunities.adobe.com/t5/adobe-analytics-questions/replace-forward-slash-wit...

 

Are you still having issues with this?

 

 

Edit: The only difference is really that in this case you aren't removing the ".com" or ".jp" from the URL...  in the original code there is a specific "replace" that replaces the ".com" with "" to remove it from the string... in this case you want to keep ".jp".... the original code will do this.. it only replaces the .com unless you explicitly add additional replaces for each domain variant... 

Your original:

Current url is https://www.xyz.com/abc/def/

Expected pageName = xyz:abc:def

 

AND the new:

Current url is https://www.xyz.jp/abc/def/

Expected pageName = xyz:jp:abc:def

 

will work with the code that was provided for both scenarios

 

var url = window.location.href;

// removes the https://www. and .com in the url 
url = url.replace("https://www.", "").replace(".com","");

// replaces all / with :, and removes the last instance of : at the end of the string
var pageName = url.replace(/\//g, ":").replace(/:$/g, ""); 

return pageName;

 

 

IF you wanted to also remove .jp from the equation:

var url = window.location.href;

// removes the https://www. and .com in the url 
url = url.replace("https://www.", "").replace(".com","").replace(".jp","");

// replaces all / with :, and removes the last instance of : at the end of the string
var pageName = url.replace(/\//g, ":").replace(/:$/g, ""); 

return pageName;

 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

This sounds a lot like your previous question: https://experienceleaguecommunities.adobe.com/t5/adobe-analytics-questions/replace-forward-slash-wit...

 

Are you still having issues with this?

 

 

Edit: The only difference is really that in this case you aren't removing the ".com" or ".jp" from the URL...  in the original code there is a specific "replace" that replaces the ".com" with "" to remove it from the string... in this case you want to keep ".jp".... the original code will do this.. it only replaces the .com unless you explicitly add additional replaces for each domain variant... 

Your original:

Current url is https://www.xyz.com/abc/def/

Expected pageName = xyz:abc:def

 

AND the new:

Current url is https://www.xyz.jp/abc/def/

Expected pageName = xyz:jp:abc:def

 

will work with the code that was provided for both scenarios

 

var url = window.location.href;

// removes the https://www. and .com in the url 
url = url.replace("https://www.", "").replace(".com","");

// replaces all / with :, and removes the last instance of : at the end of the string
var pageName = url.replace(/\//g, ":").replace(/:$/g, ""); 

return pageName;

 

 

IF you wanted to also remove .jp from the equation:

var url = window.location.href;

// removes the https://www. and .com in the url 
url = url.replace("https://www.", "").replace(".com","").replace(".jp","");

// replaces all / with :, and removes the last instance of : at the end of the string
var pageName = url.replace(/\//g, ":").replace(/:$/g, ""); 

return pageName;