Google Tag Manager. How to prevent custom tags from being loaded programmatically?


Andriy Lozynskiy

I'm working on a website using GTM (Google Tag Manager). GTM contains some scripts (tags) from websites that are not allowed in my country. It's causing an error in the console and I want to stop loading this tag. I don't have access to GTM account, so I should use js. The script is Custom HTML Tagbecause when I try the following code it stops loading:

    dataLayer = [{
        'gtm.blacklist':['html']
    }];

But it also stops loading other custom tabs. How to stop loading certain custom tags programmatically?

Matus

I think it can be done. Looking at the GTM code, we can see that it uses the insertBefore function to add a Script element to the website (for now, they can change this at any time). So in theory you could "add" some code to a native function and prevent the script from being loaded from a specific source. For example, you can run the following code before loading 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;
        }
    };
})();

(code from: adding code to javascript function programmatically )

I haven't tested this code, so I don't recommend doing it without proper testing, or you might decide not to do it at all (before you start, you might want to read: Why extending native objects is a bad practice? ). I agree with Eike's answer, but I would say that it is possible to prevent custom tags from loading programmatically.

Related


Custom HTML tags in Google Tag Manager with variables

Furman I have a custom html tag in my google tag manager <script>const d = 'sdfsf'; console.log(d);</script> Now I want to sdfsfbe a variable from my HTML to pass to GTM, where I have the GTM init fragment. Is this achievable anyway? Ike Pilstorff GTM support

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile inside the specfolder I want to use with RSpec . However, this file is loaded every time

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that lives in the specfolder I want to use with RSpec . However, this file is loaded eve

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that lives in the specfolder I want to use with RSpec . However, this file is loaded eve

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent tags from being deleted?

base map I'm using twitter typeaheadjs to populate labels on a page. I want to prevent the token from being deleted when the token value is equal to the login id. I get the event from the docs : Before the item delete event: $('input').on('beforeItemRemove', f

Prevent theme styles from being loaded into custom page templates

TV show I have a website with a wordpress theme. To implement some changes, I created a child theme. very good Now I want to add a page template that allows me to add styles via wp_enqueue_style. For this, wp_head()if I understand correctly, I need to add to m

Prevent theme styles from being loaded into custom page templates

TV show I have a website with a wordpress theme. To implement some changes, I created a child theme. very good Now I want to add a page template that allows me to add styles via wp_enqueue_style. For this, wp_head()if I understand correctly, I need to add to m

How to make a tag run before other tags in google tag manager?

klang i created a tag in google tag manager and made it custom_html and bun a javascript function code inside, i used that function in another tag but after i posted it it said fbq_custom() was not defined but when i posted it When opening the gtm file I can s

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Google Tag Manager tags explained

Alexander Starbucks Can someone explain the Google Tag Manager (container) code and help me learn the following: <script> ( function(w,d,s,l,i){ w[l] = w[l] || []; w[l].push({'gtm.start': new Date().getTime(), event:'gtm.js'}); var f = d.getElementsByTagName(s

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Google Tag Manager tags explained

Alexander Starbucks Can someone explain the Google Tag Manager (container) code and help me learn the following: <script> ( function(w,d,s,l,i){ w[l] = w[l] || []; w[l].push({'gtm.start': new Date().getTime(), event:'gtm.js'}); var f = d.getElementsByTagName(s

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

Google Tag Manager - No tags in container

Bukovski So I tried to implement google tag manager as follows : https://developers.google.com/tag-manager/enhanced-ecommerce . But nothing happened to me. Nothing is posted and nothing is shown in the tag manager admin. Here is a simple code snippet taken fro

How to prevent multiple copies of React from being loaded?

Sylar: In my previous Meteor app, using browserify and React, everything worked until I switched to meteor webpack . I'm using react-select in a Meteor app and it works great, but using browserify prevents multiple copies of react from being loaded, thus avoid

How to prevent multiple copies of React from being loaded?

Sylar: In my previous Meteor app, using browserify and React, everything worked until I switched to meteor webpack . I'm using react-select in a Meteor app and it works great, but using browserify prevents multiple copies of react from being loaded, thus avoid

How to prevent a specific dynamic library from being loaded

Herman I want to use an ancient software (Unreal Tournament "Classic" from 1999, also known as UT99). The dynamic library libtxc_dxtn.sois loaded implicitly in search of optional S3 Texture Compression (S3TC) support. Unfortunately, when loading the library, t

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist