如何从第三方JavaScript跟踪和分析中删除或阻止事件监听器?

时间:2022-11-27 15:17:08

I run an e-commerce website and we have various third-party JavaScript that add click handlers to links and forms and then insert a delay to make sure the tracking goes through. The delay is inserted by burning CPU and running a for or while loop until a certain date is passed either 350 ms later (Marketo / Munchkin) or 500ms for Visual Website Optimizer. Combined this is almost 1 second!

我运行一个电子商务网站,我们有各种第三方JavaScript,可以为链接和表单添加点击处理程序,然后插入延迟以确保跟踪完成。通过刻录CPU并运行for或while循环插入延迟,直到350 ms后(Marketo / Munchkin)或视觉网站优化程序500ms通过某个日期。结合这几乎是1秒!

Sometimes, maybe the delay is nice so tracking can be more reliable. However, we don't want this to happen when you click most links on our site because it adds up to 1 second of delay for the user. With that big a delay, there goes all the other performance optimizations we've done!

有时,也许延迟很好,因此跟踪可以更可靠。但是,当您点击我们网站上的大多数链接时,我们不希望这种情况发生,因为它为用户增加了1秒的延迟。有了这么大的延迟,我们已经完成了所有其他性能优化!

Unfortunately, we need a lot of the functionality of these scripts (like Visual Website Optimizer and Marketo) so we can't remove them.

不幸的是,我们需要这些脚本的许多功能(如Visual Website Optimizer和Marketo),因此我们无法删除它们。

Is it possible to remove the handlers they've added or prevent them from firing, given that I don't have references to them?

鉴于我没有引用它们,是否可以删除它们添加的处理程序或防止它们被触发?

4 个解决方案

#1


1  

You can use unbind to remove events.

您可以使用unbind删除事件。

If you need know the event name or type, you can see with Chrome Dev Tools

如果您需要了解活动名称或类型,可以使用Chrome开发者工具查看

Another method is capture the event and stop the propagation.

另一种方法是捕获事件并停止传播。

EDIT: If the event if not launched with jQuery, you can use removeEventListener or set null the property event.

编辑:如果事件如果没有使用jQuery启动,您可以使用removeEventListener或设置null属性事件。

#2


0  

You cannot remove the event listener without the reference to the handler function, but what you can do do is to clone the element.

如果没有对处理函数的引用,则无法删除事件侦听器,但您可以做的是克隆该元素。

var elements = document.getElementsByTagName('a');
for(var i =0 ; i < elements.length; i++){
    var cloned = elements[i].cloneNode(true);
    elements[i].parentNode.replaceChild(cloned, elements[i])
}

#3


0  

You can use pure javascript to remove the event listeners,

您可以使用纯JavaScript来删除事件侦听器,

At the start of your program just call a function to remove the event listener, something like this

在程序开始时,只需调用一个函数来删除事件监听器,就像这样

var domElement = document.getElementById('elementWithEventListener');
domElement.removeEventListener('click',listener)

And yes, you will have to find out what listeners are getting fired.

是的,你必须找出哪些听众被解雇了。

Check out Event listeners for more info.

查看事件监听器以获取更多信息。

#4


-1  

Well an alternative workaround might be to change the setTimeout functionality to work without or with smaller delay

另外一种解决方法可能是将setTimeout功能更改为不使用或具有较小延迟的工作

window.oldSetTimeout = window.setTimeout;
window.setTimeout = function(func, delay) {
   //change the delay value here
   return window.oldSetTimeout(function() {
       func();
   }, delay);
};

#1


1  

You can use unbind to remove events.

您可以使用unbind删除事件。

If you need know the event name or type, you can see with Chrome Dev Tools

如果您需要了解活动名称或类型,可以使用Chrome开发者工具查看

Another method is capture the event and stop the propagation.

另一种方法是捕获事件并停止传播。

EDIT: If the event if not launched with jQuery, you can use removeEventListener or set null the property event.

编辑:如果事件如果没有使用jQuery启动,您可以使用removeEventListener或设置null属性事件。

#2


0  

You cannot remove the event listener without the reference to the handler function, but what you can do do is to clone the element.

如果没有对处理函数的引用,则无法删除事件侦听器,但您可以做的是克隆该元素。

var elements = document.getElementsByTagName('a');
for(var i =0 ; i < elements.length; i++){
    var cloned = elements[i].cloneNode(true);
    elements[i].parentNode.replaceChild(cloned, elements[i])
}

#3


0  

You can use pure javascript to remove the event listeners,

您可以使用纯JavaScript来删除事件侦听器,

At the start of your program just call a function to remove the event listener, something like this

在程序开始时,只需调用一个函数来删除事件监听器,就像这样

var domElement = document.getElementById('elementWithEventListener');
domElement.removeEventListener('click',listener)

And yes, you will have to find out what listeners are getting fired.

是的,你必须找出哪些听众被解雇了。

Check out Event listeners for more info.

查看事件监听器以获取更多信息。

#4


-1  

Well an alternative workaround might be to change the setTimeout functionality to work without or with smaller delay

另外一种解决方法可能是将setTimeout功能更改为不使用或具有较小延迟的工作

window.oldSetTimeout = window.setTimeout;
window.setTimeout = function(func, delay) {
   //change the delay value here
   return window.oldSetTimeout(function() {
       func();
   }, delay);
};