Google 标签管理器。如何防止以编程方式加载自定义标签?
Google tags manager. How to prevent loading custom tag programmatically?
我在使用 GTM(Google 标签管理器)的网站上工作。
GTM 包含来自我所在国家/地区不允许的网站的一些脚本(标签)。它会导致控制台出错,我想停止加载此标签。我无权访问 GTM 帐户,所以我应该使用 js 来完成。这个脚本是 Custom HTML Tag
因为当我尝试下面的代码时它停止加载:
dataLayer = [{
'gtm.blacklist':['html']
}];
但它也停止加载其他自定义标签。
如何停止以编程方式加载某些自定义标签?
您不能以编程方式阻止特定自定义 HTML。一个原因是它毫无意义 - "custom HTML" 意味着 "arbitrary code executed in the context of your site",因此代码可以简单地放入另一个 HTML 标记并从那里成为 运行。
您无法控制您网站中 运行 的 GTM 实例(这实际上意味着您无法控制您的网站)不是 Google 可以以任何有意义的方式迎合(如果您控制 GTM,则只需删除标签)。
如果您的意思是您想要禁止某个来源的脚本,那么您可以查看 Content Security Policies (which will work no matter if the scripts runs from GTM or any other source). However CSPs are notoriously hard to implement (and while it is possible to implement them from within GTM 这仅适用于有限测试,不适用于生产用途)。
我觉得可以。查看 GTM 代码,我们可以看到它使用 insertBefore 函数将脚本元素添加到网站(目前,他们可以随时更改此设置)。所以理论上你可以 "add" 一些代码到本机函数并防止从特定来源加载脚本。例如,您可以在加载 GTM 之前 运行 以下代码:
Node.prototype.insertBefore = (function() {
var cached_function = Node.prototype.insertBefore;
return function(script) {
if(script && script.src.indexOf("www.somesource.com/script.js") !== -1){ //change to src you don't want to load on your page
return false; //don't add the script
}else{
var result = cached_function.apply(this, arguments); // use .apply() to call native function
return result;
}
};
})();
(代码取自:Adding code to a javascript function programmatically)
我没有测试这段代码,所以我不建议你在没有适当测试的情况下就这样做,否则你可能会决定根本不这样做(在你这样做之前,你可能想阅读:Why is extending native objects a bad practice? ).我同意 Eike 的回答,但我要说的是,可以防止以编程方式加载自定义标签。
我在使用 GTM(Google 标签管理器)的网站上工作。
GTM 包含来自我所在国家/地区不允许的网站的一些脚本(标签)。它会导致控制台出错,我想停止加载此标签。我无权访问 GTM 帐户,所以我应该使用 js 来完成。这个脚本是 Custom HTML Tag
因为当我尝试下面的代码时它停止加载:
dataLayer = [{
'gtm.blacklist':['html']
}];
但它也停止加载其他自定义标签。 如何停止以编程方式加载某些自定义标签?
您不能以编程方式阻止特定自定义 HTML。一个原因是它毫无意义 - "custom HTML" 意味着 "arbitrary code executed in the context of your site",因此代码可以简单地放入另一个 HTML 标记并从那里成为 运行。
您无法控制您网站中 运行 的 GTM 实例(这实际上意味着您无法控制您的网站)不是 Google 可以以任何有意义的方式迎合(如果您控制 GTM,则只需删除标签)。
如果您的意思是您想要禁止某个来源的脚本,那么您可以查看 Content Security Policies (which will work no matter if the scripts runs from GTM or any other source). However CSPs are notoriously hard to implement (and while it is possible to implement them from within GTM 这仅适用于有限测试,不适用于生产用途)。
我觉得可以。查看 GTM 代码,我们可以看到它使用 insertBefore 函数将脚本元素添加到网站(目前,他们可以随时更改此设置)。所以理论上你可以 "add" 一些代码到本机函数并防止从特定来源加载脚本。例如,您可以在加载 GTM 之前 运行 以下代码:
Node.prototype.insertBefore = (function() {
var cached_function = Node.prototype.insertBefore;
return function(script) {
if(script && script.src.indexOf("www.somesource.com/script.js") !== -1){ //change to src you don't want to load on your page
return false; //don't add the script
}else{
var result = cached_function.apply(this, arguments); // use .apply() to call native function
return result;
}
};
})();
(代码取自:Adding code to a javascript function programmatically)
我没有测试这段代码,所以我不建议你在没有适当测试的情况下就这样做,否则你可能会决定根本不这样做(在你这样做之前,你可能想阅读:Why is extending native objects a bad practice? ).我同意 Eike 的回答,但我要说的是,可以防止以编程方式加载自定义标签。