Track anchor links with munchkin?

Highlighted
Level 7

Track anchor links with munchkin?

Hi all,

I did some testing on our pages and noticed that munchkin doesn't make a Click Link activity for anchor links: for example someone on examplepage.com won't create an activity if they click the link examplepage.com#someID.

Is there a simple way to get munchkin to do this?

Grant

3 REPLIES 3
Highlighted
Level 10 - Community Moderator

Re: Track anchor links with munchkin?

Yep, see

More links Munchkin won't track (unless you tell it to) - Part 1 of 2

and the mchReDecorate technique:

document.addEventListener('DOMContentLoaded', function(e) {

var arrayify = getSelection.call.bind([].slice);

var canonicalDocLocation = document.createElement("a");

canonicalDocLocation.href = document.location;

canonicalDocLocation.hash = "";

arrayify(document.links)

.map(function(link){

var canonicalLink = document.createElement("a");

canonicalLink.href = link.href;

canonicalLink.hash = "";

return {

original : link,

canonical : canonicalLink

}

})

.filter(function(linkDescriptor){

return linkDescriptor.canonical.href == canonicalDocLocation.href &&

( linkDescriptor.original.hash || /#$/.test(linkDescriptor.original.href) );

})

.forEach(function(linkDescriptor){

console.log("reattaching Munchkin to jump link", linkDescriptor.original);

linkDescriptor.original.addEventListener("click", function(){

Munchkin.munchkinFunction("clickLink",{

href : this.href

});

});

});

});

Highlighted
Level 7

Re: Track anchor links with munchkin?

Thanks Sanford! I was about to post that I'd found something that would work by putting the clickLink munchkin function into the hyperlink's onclick attribute, but I will check out the blog post as I would love to find something universal.

Highlighted
Level 10 - Community Moderator

Re: Track anchor links with munchkin?

Use the code above. No need to hijack the inline onclick attribute.