最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - To track clicks on a div using google analytics - Stack Overflow

programmeradmin2浏览0评论

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 being clicked the most.

How do I do this with google analytics?

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 being clicked the most.

How do I do this with google analytics?

Share Improve this question edited Oct 31, 2017 at 9:22 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Jun 25, 2012 at 23:30 LINGSLINGS 3,6305 gold badges36 silver badges47 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

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.

Check out the GA page on Event Tracking: https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide

The basic idea is:

var someDiv = document.getElementById('someDiv');
someDiv.addEventListener('click', function(){
    _gaq.push(['_trackEvent', 'someDiv', 'click']);
}, false);
发布评论

评论列表(0)

  1. 暂无评论