Onclick attribute for tracking analysis

Hey guys,

I’ve seen a lot of people struggling with this onclick attribute thing, including me.

I run an e-commerce business and 90% of my traffic comes from Ads (mainly Pinterest Ads). And without this onclick attribute I can’t track my AddToCart events (which is CRUCIAL).

So I tried every solutions, the “whenClicked trick”, hack #12 from Finsweet but I didn’t manage to make it work with my Pinterest AddToCart tag.

But yesterday I found a solution that finally works perfectly for me so I wanted to share it with you guys just in case it can help someone. Here’s the script, just put it “before tag” :

<script>
document.getElementById("*BUTTONID*").onclick = function() {myFunction()};
 
function myFunction() {
  *EVENT TAG*
}
</script>

In my case, with the Pinterest’s addtocart tag it looks like this and works like magic :

<script>
document.getElementById("atcbutton").onclick = function() {myFunction()};

function myFunction() {
  pintrk('track', 'addtocart');
}
</script>

Just don’t forget to set an ID for your button and you’re good to go😉

2 Likes

Super useful, thanks for sharing this!

1 Like

You’re welcome ! Glad I can help