Using Code to Edit Many GTM Triggers Simultaneously

Posted by: Ed Hungerford Date Posted: September 25, 2025
Back to Case Studies

Issue

ecoATM recently launched a Canadian site. Unfortunately, Shopify’s Markets feature (through which the Canadian site was launched) loads the Google Tag Manager container of the US site automatically, which would cause US data to mingle with CA data. There are multiple ways around this, but just in case, I decided to prepare a version of each GTM container that would prevent triggers from firing on the wrong domain.

Having said that, the two US containers are very big and contain a lot of triggers. Manually editing each one would eat up a lot of my time, and there was also always the chance that I’d miss one or two by accident. While GTM has a bulk edit trigger feature, it does not allow existing triggers to be edited in bulk — it can only add new triggers or remove existing ones.

From my experience working on the GTM -> CSV project, I knew it was possible to export a GTM container and edit the contents using PHP and JavaScript. If I could retool the script on https://mcfxsolutions.webpagefxdev.com/gtm-to-csv-converter/, I could save myself a lot of time and effort.

Solution

I copied the code from https://mcfxsolutions.webpagefxdev.com/gtm-to-csv-converter/ and removed everything that had to do with tags and variables. Then I tweaked the trigger-related code to add the following object to the filter property of each trigger:

{
"type": "CONTAINS",
"parameter": [
{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{Page Hostname}}"
},
{
"type": "TEMPLATE",
"key": "arg1",
"value": "ecoatm.ca"
},
{
"type": "BOOLEAN",
"key": "negate",
"value": "true"
}
]
}

After that, I generated a new .csv, imported it into a new workspace, and sent it to the client.

Achievement

Bulk edited about 50 triggers in GTM using JS/PHP.

Snippet/Screenshot/Link

...

<body>
<main id="page-body">
    <div class="gtm-converter-container container">
        <div class="gtm-converter-input">
            <fieldset>
                <legend>Upload your GTM container here</legend>
                <p>
                    <input type="file" id="gtm-file" name="gtm-file" accept=".json">
                    <label for="gtm-file">Choose a file</label>
                </p>
                <p><input type="submit" id="gtm-submit" name="gtm-submit" value="Convert"></p>
                <p>Note: The file must be a valid GTM container JSON file.</p>
            </fieldset>
        </div>
        <div class="gtm-converter-output">
            <div class="gtm-converter-output-pres">
                <div class="gtm-converter-output-pre-container">
                    <pre>

                    </pre>
                </div>
            </div>
        </div>
    </div>
</main>

<script>
    document.querySelector('#gtm-submit').addEventListener('click', function(event) {
        const fileInput = document.getElementById('gtm-file');
        const file = fileInput.files[0];

        // Error handling for if there's no file.
        if (!file) {
            alert('Please select a file to upload.');
            return;
        }

        // Error handling for if there's an issue with the file type.
        const reader = new FileReader();
        reader.onload = function(event) {
            const contents = event.target.result;
            if(!contents) {
                alert('Error reading file. Please try again.');
                return;
            }
            // Process the GTM container JSON here
            try {
                const gtmContainer = JSON.parse(contents);
                const triggers = gtmContainer.containerVersion.trigger;

                let myTriggers = []
                triggers.forEach(trigger => {
                    trigger.filter ?
                    trigger.filter.push({
                        "type": "CONTAINS",
                        "parameter": [
                            {
                                "type": "TEMPLATE",
                                "key": "arg0",
                                "value": "{{Page Hostname}}"
                            },
                            {
                                "type": "TEMPLATE",
                                "key": "arg1",
                                "value": "ecoatm.ca"
                            },
                            {
                                "type": "BOOLEAN",
                                "key": "negate",
                                "value": "true"
                            }
                        ]
                    })
                    : trigger.filter = [{
                        "type": "CONTAINS",
                        "parameter": [
                            {
                                "type": "TEMPLATE",
                                "key": "arg0",
                                "value": "{{Page Hostname}}"
                            },
                            {
                                "type": "TEMPLATE",
                                "key": "arg1",
                                "value": "ecoatm.ca"
                            },
                            {
                                "type": "BOOLEAN",
                                "key": "negate",
                                "value": "true"
                            }
                        ]
                    }]
                    myTriggers.push(trigger);
                });

                gtmContainer.containerVersion.trigger = myTriggers;
                console.log(gtmContainer);

                // Append to document
                myGTMContainer = JSON.stringify(gtmContainer, null, 2)
                const outputPre = document.querySelector('.gtm-converter-output-pres pre');
                outputPre.textContent = myGTMContainer;
                const copyIcon = document.querySelector('.gtm-converter-copy-icon');


                //And put the CSV into a file that is downloaded automatically
                const blob = new Blob([myGTMContainer], { type: 'text/json' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = 'gtm-container.json';
                document.body.appendChild(a);
                a.click();
            }
            catch (e) {
                alert('Error processing file. Please try again.');
                console.log(e);
                return;
            }
            // You can convert the JSON to CSV and display it in the output area
        };
        reader.readAsText(file);
    })
</script>
...