Click Element event tracking for single page applications


#1

Hi,

Is there a recommended way to get hubspot click events tracked on single page applications (SPA)?
I’ve created some ‘Clicked Element’ events using the jquery selector option. The generated javascript looks something like:

_hsq.push(function(t){t.urlMatches("my-url") && t.trackClick("my-jquery-selector", "my-event-id")});

However on an SPA, the element may not yet be on the page when the tracking code loads. Is there a recommended way to handle this?

Thanks,
Emile


Contact creation via the Events Javascript API
#2

Hi Emile,

Instead of passing _hsq.push() a function with a click listener, you can attach your own click listeners when the element is created, and in the callback of those listeners, push through the event trigger directly, like:

_hsq.push(["trackEvent", {
    id: "EVENT_ID_HERE"
}]);

#3

Ok thanks, I’ll update to use that instead. It’s a pity we can’t use clicked element events as our marketing team could use the bookmarklet plugin to create and manage all the events they need tracked without any code changes required.