如何在用户单击链接时记录MixPanel事件? [英] How do I log a MixPanel event when a user clicks a link?
问题描述
当用户点击某种类型的链接时,我正在尝试在MixPanel中记录一个事件。我正在使用JQuery进行不显眼的操作,据我所知,我需要添加一个回调函数,以便在记录事件后将用户带到URL。
I'm trying to log an event in MixPanel when users click a certain type of link. I'm using JQuery to do it unobtrusively and as far as I understand I need to add a callback function to take the user to URL after the event has been logged.
这是我正在使用的代码:
This is the code I'm using:
<script type="text/javascript">
$("#more-posts").click(function() {
event.preventDefault();
mpq.track("More Posts", function(){
window.location = $(this).attr("href");
});
});
</script>
不幸的是,既不会将用户带到页面也不记录事件,但我看到没有错误Chrome中的Javascript控制台。
Unfortunately this neither takes the user to the page nor logs the event, but I see no errors in the Javascript console in Chrome.
任何想法可能是什么问题?
Any ideas what the problem may be?
更新:还根据评论中的建议尝试了此代码:
Update: Also tried this code based on suggestions in the comments:
<script type="text/javascript">
function go_to_link(link) {
window.location = link;
}
$("#more-posts").on("click", function(event) {
event.preventDefault();
mpq.track("More Posts");
setTimeout("go_to_link($("#more-posts").attr("href"))", 2000);
});
</script>
现在重定向到正确的链接,但仍然没有记录事件。
It now redirects to the correct link, but still doesn't log an event.
推荐答案
Mixpanel最近添加了一种方法 mixpanel.track_links 。
Mixpanel has recently added a method mixpanel.track_links that does the job.
这篇关于如何在用户单击链接时记录MixPanel事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!