I track links clicked on my page with some jquery like so:
// Track when a visitor clicks a link
$( document ).ready(function() {
// Add an event handler to all body anchor links
$( "body" ).find("a").click(function() {
ga('send', 'event', document.title, 'Link: ' + this.innerHTML, '');
});
});
This works very well for static page content. However, some of my pages load in partial page content dynamically from elsewhere on my site using Jquery load() method. e.g. $( "#result" ).load( "ajax/test.html" );
I have noticed that when I click on a link on this dynamic content, the link isn't getting tracked in GA.
I guess this makes sense, the link event handlers are added when the page is initially loaded, any content pulled through after this won't have these event handlers registered...
So my question is, how do i track links clicked for content that is added dynamically after the page has loaded? Should I add my event handler code (above) to a function which i can call on page load then after any dynamic content has been added, telling it to scan the page again? Will that cause a conflict with any original event handlers or will it override them?
thanks