I have been trying to set up a jQuery handler that will fire an event every time that a user clicks to buttons that lead to external pages and populate the event label dimension with the href of each link clicked.
How do I do it?
Thank you very much in advance.
Solved! Go to Solution.
01-12-2016 08:20 AM - last edited on 01-12-2016 09:37 AM by kathleen_jo
You'll need some jQuery that will parse through all of the hyperlinks on your page. Hopefully the following code will get you on your way.
You can see how this works by copying the code below and pasting into the console while viewing a page on your site.
console.log("External link clicked, url: " + jQuery(this).attr('href')); }); } }} });
I've left the actual call to utag.link for you to complete. Also, may notice hyperlinks that are picked up that you don't want a listener set up. When running the above in the console, I noticed on a couple sites a telephone number as a hyperlink; hence the code block that ignores anything starting with 'tel'. You may find the need to add additional code to block others on your site.
Please let me know how this turns out.
Another user asked the same question yesterday. Are the two of you working together? :)
The soltuion is similar:
I just tried the code for tracking external links and it works great!
I had a small issue that I thought is worth sharing. At the beginning when I set the extension to fire on 'mousedown', tracking would only happen for the second link I clicked on on every page. I then changed it to trigger on 'mouseover' and the problem was solved.
This might be elementary but was new to me. Also, I was using AT internet rather than GA but my intuition is that the extension would work the same way with both platforms.
Excellent to know @Gil123. Thank you for sharing! Let me see about changing the 'mousedown' to 'mouseover' in the code.