0

这就是我正在考虑做的事情。在包含我想发送到 Mixpanel 的数据的链接和按钮中使用 data-track 属性...类似于:

点击它

然后以某种方式 jQuery 绑定到包含数据轨道的任何单击。或者可能绑定到所有点击,如果项目包含数据跟踪属性,则获取该值并提交给 mixpanel,如下所示:

mpq.track("Open slideshow", {'show': 'Xmas'}, function(){ myfunction() });

可能的?好主意?馊主意?关于如何在不需要大量代码的情况下实现的任何想法?

谢谢

4

3 回答 3

1

Sorry to resurrect, but here is an (untested) solution that should work. You can specify the event name using the data-track property, and any additional event attributes using a JSON string in the data-track-attrs property. The javascript will bind to the click handler for any element with a data-track property and dispatch the event to Mixpanel. In the case where a native JSON decoder is not available, it will fail silently.

<div class="header">
  <a href="#" data-track="Login" data-track-attrs='{ "where": "header" }'>login</a>
</div>

<a href="#" data-track="Open slideshow" data-track-attrs="{ show: 'Xmas' }">open xmas</a>

<div class="footer">
  <a href="#" data-track="Login" data-track-attrs='{ "where": "footer" }'>login</a>
</div>

<script type="text/javascript">
  $(function() {
    $("body").on('click', '[data-track]', function(evt) {
      var event_name = $(this).data('track');
      var event_attrs;
      try {
        event_attrs = JSON.parse($(this).data('track-attrs'));
      } catch(e) {}

      mixpanel.track(event_name, event_attrs);

    });
  });
于 2012-03-27T20:20:00.103 回答
1

试试这个选择器$('.YourClass[data-track]')

于 2011-09-01T11:21:15.173 回答
-1

对一个老问题的超级迟到的答案,但这里有一个 jQuery 插件可以做到这一点(还有更多): http: //os.lucidfusion.com/datatrack/

于 2015-08-02T07:00:42.817 回答