【问题标题】:JQuery location.href - events trackingJQuery location.href - 事件跟踪
【发布时间】:2015-01-18 12:39:53
【问题描述】:

我有一个包含许多优惠的优惠页面。客户希望将跟踪脚本添加到一个特定的报价并跟踪几个按钮的点击,即“更多信息”、“分享”和“购买”。

我想使用以下内容:

if (window.location.href == "www.ddd.xx/theoffer") {
    if $( "#target" ).click(function() { // run tracking script }
    else if $( "#second_target" ).click(function() { // run another tracking script}
    else if $( "#third_target" ).click(function() { // run another tracking script } };

看起来还好吗?

非常感谢您的帮助! BR, 新手

【问题讨论】:

  • 你试过了吗?什么不工作?如果没有更多的上下文,很难说这是否正确。
  • 删除 ifelse if 语句,它应该可以正常工作。
  • 谢谢罗里,我应该删除所有 if 和 else if 还是只删除第一个 if 中的那些?

标签: javascript jquery event-tracking


【解决方案1】:

我想知道你是在跟踪你的目标页面还是当前页面。window.location.href表示你当前页面的url,或者你可以设置它跳转到另一个页面。

但是无论你在跟踪哪个页面,你都应该先将click函数与按钮绑定。

对于当前页面,可以这样绑定:

$( "#target" ).click(function() { // run tracking script });
$( "#second_target" ).click(function() { // run another tracking script});
$( "#third_target" ).click(function() { // run another tracking script});

此函数为回调函数,不会直接运行跟踪,只有点击右键才会触发。

如果你有很多按钮来做一些类似的工作,似乎你可以将class="track_button"添加到所有<button>s,并像这样绑定:

$(".track_button").click(function(){
  if (window.location.href == "www.dd/xx/the_offer") {
    var curId = $(this).attr("id");
    if(curId == "target"){
      //do tracking 1
    }
    else if(....){
      //do tracking n....
    }
  }
});

【讨论】:

  • 其实我不知道你想用href做什么。对于函数绑定,检查这个:jsfiddle.net/o0d12vpr
  • 我需要 href 以确保用户在单击这些按钮时位于所选的优惠页面上。否则,它会跟踪所有其他优惠页面的点击次数。
  • 好吧。所以我认为我的代码 snnipet 2 适合你。试试吧,更新你的问题,如果我能提供帮助,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多