value from the url | Community
Skip to main content
Level 4
July 1, 2022
Solved

value from the url

  • July 1, 2022
  • 1 reply
  • 618 views

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?

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 Jennifer_Dungan

This sounds a lot like your previous question: https://experienceleaguecommunities.adobe.com/t5/adobe-analytics-questions/replace-forward-slash-with-colon/m-p/459193

 

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;

 

1 reply

Jennifer_Dungan
Community Advisor and Adobe Champion
Jennifer_DunganCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
July 2, 2022

This sounds a lot like your previous question: https://experienceleaguecommunities.adobe.com/t5/adobe-analytics-questions/replace-forward-slash-with-colon/m-p/459193

 

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;