我創建了一個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。