How can I track clicks on all internal links without using onclick? (menu links, text links, button clicks)

Hello,

I know I can insert onclick e.g.:

<a href="mysite.com/page2" onclick="javascript:_paq.push(['trackEvent', 'Click', 'PageID']);">Click here</a>

but isn’t there an easier way to apply event tracking to all links? I also found this advice “you’re better off not using onClick at all and attaching the event handler to the DOM node through your JS code. This is known as unobtrusive javascript.” I’m not sure how to do that - I just want to be able to track clicks on all links so I can see what people are doing on my site. It must work on all devices. It would be nice if I didn’t had to manually change every single link on all my pages, menus etc.

How do I do it and is it possible to use the link text (e.g. “click here to sign up”, “read more here” etc.) to identify each link? (so there will be an event for each link).

I have looked for a solution for several days, but I guess I’m not smart enough… any help is appreciated, thanks.

You mean to follow up with events?
Thank U

Maybe you can do this with jQuery. Just add the tracking-event to the click-element on the required elements.

$( "#menu a" ).click(function() { _paq.push(['trackEvent', 'Click', 'PageID']); }); $( ".postbody a" ).click(function() { _paq.push(['trackEvent', 'Click', 'PageID']); }); $( "button" ).click(function() { _paq.push(['trackEvent', 'Click', 'PageID']); });

You need to change the selectors for the menu-links, text-links and maybe the buttons. More about jQuery-Selectors in the Documentation.

1 Like

Thanks and sorry for my very late response (bad things happened in my life…)

@selenaly: Maybe I don’t understand you, but I’m using Piwik so that I don’t have to use Google analystics…

@pfleck: Thanks. I’m not sure how to make it work or where to put the code, but I will look into that.

I’m kind of surprised no one else is asking about how to do this - how can you make a website better if you don’t know what people are clicking on/not clicking on? I thought that was Piwik’s primary job…

Why do you want to track events for links? Isn’t an action tracked when the users enters the linked page?

I thought the same… But I found an possible explanation for this: If you have the same Links several times (TopLevel-Navigation, Footer-Navigation, etc.), you may want to know, how the users navigate within your page to optimize it.

1 Like

Exactly! How can you create a great website if you don’t know what people click on? I have used “onclick” on a few links, buttons, banners on my site and I found out that they only got a few clicks… so I removed them or changed them. I can’t/shouldn’t use “onclick” on everything so I need something that is easy to implement. I guess it is easy if you use Google Analytics… not so much with Piwik…
Thanks

Hello, sorry for my delayed answer.
The matter is not easy if you do not want to use an onclick event. there is still the function trackContentInteraction, but unfortunately every visit of every html element is included.
Unfortunately, the trackContentInteractionNode does not work as it should. Thus, the clicks on the selected element are only displayed in the Visitor Log, not under Contents or Events. Maybe a bug?
You can probably somehow trick with piwikContentIgnoreInteraction. (?, untested)
During a test it happened to me that only clicks and no visits in content were displayed. There was the problem that the percentages went up over 100%. But unfortunately I noticed that too late and had already overwritten the code.

The only possibility that still exists is that you use subdomains (outlinks) and rewrite these with htaccess. But I have not tested that yet.