XF 1.5 Tracking outbound links with Analytics events


Active member
I am interested to know how can I add outbound link tracking via Analytics.

The code for event tracking is below, so since this probably falls outside templates, I suppose one of core files would need to be modified.

<a onclick="gtag('event', 'click', { 'event_category': 'Outgoing Link', 'event_label': 'https://example.com/' });" href="https://example.com/"></a>

Ideally the event_label tag, should have the outgoing link auto-inserted.

Question is which core file handles URLs inside messages, profile posts, sidebar link etc... ?

You can do that with templates without any problems. XF adds an "externalLink" class to external links. You'd just create a JS listener on page load, filter all links with that class and add your onclick listener then.
Anyways, the link formatter is in /library/XenForo/Helper/String public static function getLinkClassTarget($url).
Top Bottom