Skip to main content
Grant_Booth
Level 9
Feb 15, 2019 12:30 AM UTC
Question

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

SanfordWhiteman
User Group Leader
User Group Leader
Feb 15, 2019 5:38 AM UTC

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

});

});

});

});

Translate
Diego_Lineros2
Level 5
Dec 10, 2020 10:52 AM UTC

This is very good, It is a shame Marketo is not giving the option out of the box. By the way, I was using this script in one LP, and it was working well across the board, but lately I noticed stop working in Chrome.

Translate
Grant_Booth
Level 9
Feb 15, 2019 5:33 PM UTC

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.

Translate
SanfordWhiteman
User Group Leader
User Group Leader
Feb 15, 2019 5:46 PM UTC

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

Translate