我创建了一个JavaScript(用jQuery)代码,将识别数据属性:
HTML:(此属性的内容是一个JSON字符串[事件名称,属性]注意:它只能识别双引号)
<a href="mailto:[email protected]" data-mp-track='["Contact Click", {"Type" : "Email"}]'>Contact</a>
的Javascript:
$('a[data-mp-track]').on('click', function(event) {
var callback, mp_details, new_tab, properties, that;
that = this;
new_tab = event.which === 2 || event.metaKey || that.target === '_blank';
callback = function() {
if (new_tab) {
return;
}
window.location = that.href;
};
mp_details = JSON.parse(that.getAttribute('data-mp-track'));
if (!new_tab) {
event.preventDefault();
setTimeout(callback, 300);
}
properties = $.extend(mp_details[1], {
'Page Title': document.title,
'URL': window.location.pathname
});
mixpanel.track(mp_details[0], properties, callback);
});
得到了一些灵感来源于this post。