javascript 当用户单击链接时,如何记录 MixPanel 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8657118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I log a MixPanel event when a user clicks a link?
提问by cutwithflourish
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.
当用户单击某种类型的链接时,我试图在 MixPanel 中记录一个事件。我正在使用 JQuery 不显眼地完成它,据我所知,我需要添加一个回调函数,以便在记录事件后将用户带到 URL。
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>
Unfortunately this neither takes the user to the page nor logs the event, but I see no errors in the Javascript console in Chrome.
不幸的是,这既不会将用户带到页面也不会记录事件,但我在 Chrome 的 Javascript 控制台中没有看到任何错误。
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.
它现在重定向到正确的链接,但仍不记录事件。
回答by alephzarro
Mixpanel has recently added a method mixpanel.track_linksthat does the job.
Mixpanel 最近添加了一个方法mixpanel.track_links来完成这项工作。
回答by Thomas Hunter II
The third argument of the mpq.track function is a callback. This code gets executed once the tracking has completed, and would be a reliable way to send the user to the other page.
mpq.track 函数的第三个参数是回调。一旦跟踪完成,此代码就会执行,并且是将用户发送到另一个页面的可靠方式。
$("#more-posts").on("click", function(event) {
event.preventDefault();
mpq.track("More Posts", null, function() {
go_to_link($("#more-posts").attr("href"))
});
});
回答by Josh Smith
I believe this is a candidate for MixPanel support: [email protected]. The bug does not lie with your jQuery code. Here's a working jsFiddlethat demonstrates the basic functionality.
我相信这是 MixPanel 支持的候选人:[email protected]。错误不在于您的 jQuery 代码。这是一个演示基本功能的有效jsFiddle。
As I mentioned, I've seen similar issues with a _kmq.push
with Kissmetrics. Their JS simply might not have time to register the event. If you try a longer timeout, it might work, but this is bad UX.
正如我所提到的,我在_kmq.push
Kissmetrics 中看到了类似的问题。他们的 JS 可能根本没有时间注册该事件。如果您尝试更长的超时时间,它可能会起作用,但这是糟糕的 UX。
Update here if/when you reach out to MixPanel.
如果/当您联系 MixPanel 时,请在此处更新。
回答by raylu
回答by ulisesrmzroche
The easiest way is to use the function callback in mixpanel.track which fires after the event is successfully logged.
最简单的方法是使用 mixpanel.track 中的函数回调,它会在事件成功记录后触发。
Code would look like this.
代码看起来像这样。
mixpanel.track("Great Success!", {}, function(){
window.location.href = "www.whatever.com";
});