Want to capture only url after # in page url | Community
Skip to main content
Level 3
July 6, 2023
Solved

Want to capture only url after # in page url

  • July 6, 2023
  • 1 reply
  • 749 views

I want to capture url after #tag in page url, how should i set it up in data elements in tag manager.

 

www.link.com#article1

www.link.com#article2

 

So i have created variable Article and i want to store variable article1, article2.. so on.

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

I don't think there's a default standard data element for this, but you can get it using a Custom Code data element using window.location.hash

 

return window.location.hash

 

OR if you need to sanitize the data, or check for values before returning the final value (since often links from FB or other external source could add hash values to your URLs that may not be your "article" values, or if there are any named anchors used in your site like accessible "skip links", or "back to top", etc links that all use #):

 

var hash = window.location.hash; // Do sanitation and checks here return hash;

 

1 reply

Jennifer_Dungan
Community Advisor and Adobe Champion
Jennifer_DunganCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
July 6, 2023

I don't think there's a default standard data element for this, but you can get it using a Custom Code data element using window.location.hash

 

return window.location.hash

 

OR if you need to sanitize the data, or check for values before returning the final value (since often links from FB or other external source could add hash values to your URLs that may not be your "article" values, or if there are any named anchors used in your site like accessible "skip links", or "back to top", etc links that all use #):

 

var hash = window.location.hash; // Do sanitation and checks here return hash;