Target click tracking using elements data- attribute

458 Views Asked by At

I would like to add analytics.js click tracking to some elements on a page which all have a unique data- attribute.

On the anaytics page :

https://developers.google.com/analytics/devguides/collection/analyticsjs/events

It says to add the code in the following format :

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);

Although when for instance it says eventCategory the example it gives is just "Video" rather than a class or id.

My question is how would i right the above so it tracks each element based on its data attribute for instance : data-id="2250ee774b0a42688745b6143b662328"

Any help would be greatly appreciated.

Thanks

David

2

There are 2 best solutions below

0
On

you should use https://sizzlejs.com/ to find the selector based on data-id and write custom code.

But why not use this tool to record your actions without going through this hassle.

www.customerlabs.co checkout their action recorder.

0
On

It depends on how you want to see the data in the google analytics and what are those "some elements", I would suggest using the following approach using jQuery by adding a click handler

Suppose you want to group all the data-id under a category DataIDClicks

$('[data-id]').on("click",function(){
  ga('send', 'event', 'DataIDClicks', $(this).data('id'));
})