To track clicks on a div using google analytics

后端 未结 2 2215
感情败类
感情败类 2021-02-20 13:38

I have multiple DIVs with unique IDs. Each DIV onclick slides down and shows some content below it.

I would like to track the clicks on each DIV to know which DIV is be

2条回答
  •  你的背包
    2021-02-20 13:46

    It sounds like you're looking for Google Analytics "Event Tracking." Assuming you already have your regular GA code snippet set up (to track your site), you can then set up a handler (for clicks of the div, let's say) do to something like this with jQuery:

    $('.trackerClass').click(function() {
    
      var selId = $(this).attr('id');
    
      _gaq.push(['_trackEvent', 'Interactions', 'Click', selId]);
    
    });
    

    Every div that you wanted to track could have both "trackerClass" as the class, and a unique ID to help you identify it.

    If you wanted to track opens/closes individually, you could add a state variable and pass that as well.

    More information is available here:

    https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking

    Edit: for straight JavaScript, kennis' answer will do it.

提交回复
热议问题