capture URL from a click tag

Bronze Contributor
Bronze Contributor

Is there a way to capture URL from a click?

I'm trying to populate the URL into event_label using extensions. I have jQuery Selector and want to have js code as below for populating event label. Can this work? 

$(this).find('div.PlayerThumbnail-module__link--3s667').find('a').attr('href');

4 REPLIES 4

capture URL from a click tag

Silver Contributor
Silver Contributor

Hi @ak87qa 

How about using window.location.origin - that will give your url as well

capture URL from a click tag

Tealium Expert
Tealium Expert

Hi @ak87qa 

If the click is being captured on an anchor tag, then $(this) should refer to that anchor tag, so all you should need is:

$(this).attr("href")

Your code is looking for the div within the clicked element, and then looking inside that for the anchor.

If this is right (ie the click is being captured on a div, or a list element, or something similar) then you shouldn't need to use .find twice.  You can just nest the selectors:

$(this).find('div.PlayerThumbnail-module__link--3s667 a').attr('href');

It also looks like that div class includes an ID.  Is that coming from your CMS, and is it likely to change?  If so, it could break the link tracking.

 

Another option that we have used many times is to add data attributes to links, and then using them for tracking.  So you'd have something like:

<a href="someurl.html" class="someclass" data-tracking="useful-tracking-string">link text</a>

then you can use

$(this).data("tracking")

to pull back the reporting friendly data from the link.

capture URL from a click tag

Bronze Contributor
Bronze Contributor
Thanks BenStephenson,
Is there documentation with a few JS Code examples as above? I haven't come across any documentation on this topic.

capture URL from a click tag

Bronze Contributor
Bronze Contributor

Is there a Javascript variable I can create based on a click/destination URL, and only populate if a "link" tealium event is fired?

I'm not conversant in JavaScript, and was working out whether it could be the following:

 

b["$(this).attr("href");"]

 

or

 

b["document.location.href"]

I haven't been able to find any documentation...

 

Public