Export All of Your Tags from GTM to a Spreadsheet Effortlessly [Improved]

Whilst browsing the web I found this article sharing painless ways to copy all your Google Tag Manager tags over to a Spreadsheet. Sadly when I tried this, my Currently_Paused field didn’t seem to populate correctly.

The author, Anton Entin did a great job with the original code so I shared my update on the page as a comment. On the off chance that doesn’t get updated and in the interest of everyone being able to quickly copy their tags into a spreadsheet here is my take on it.

Click here to jump to the video →

If you’re looking for a quick and painless way to copy all your Google Tag Manager tags, including their paused/active status, below is a snippet of code you can paste into your browser console that will copy all your tags onto your clipboard for you to paste into a CSV, Google sheet or Excel spreadsheet. This should take no longer than 5 minutes

How to use

  1. Navigate to your GTM container and have all your tags shown on your screen
  2. [Important] Scroll to the bottom of the page and ensure “Show rows” is set to “ALL” within GTM to expose all your GTM tags on the page
  3. Paste the snippet shown below into your Chrome Dev Tools Console.
    • You can right click on the page and locate “Inspect” to access your Dev Tools. From there click on “Console”
    • Your output will be copied over to your clipboard
  4. The contents are now stored in your clipboard, access Excel or Google Sheets and paste from your clipboard onto the spreadsheet
  5. You may need to use a function to split the data into columns in Excel or Google Sheets. Please refer to the video at the bottom of this article for more instructions, below are the commands to do this:
    – Google Sheets: Data -> Split text to columns
    – Excel: Data -> Text to columns. Choose “delimited”, then choose “comma” and your delimiter

Code Snippet to paste


// Paste this script in the Console section of your browser Dev Tools.
gtmData = [];

var button = document.querySelector(".suite.suite-up-button.md-button.md-standard-theme.md-ink-ripple.layout-align-start-center.layout-row");

// Extract the account name
var accountNameElements = button.querySelectorAll(".suite-up-button-text-secondary");
var accountName;
if (accountNameElements.length > 1) {
    accountName = accountNameElements[1].textContent.trim();
} else {
    console.log("Expected more than one .suite-up-button-text-secondary element, found less");
}

// Extract the GTM container name
var gtmContainerNameElement = button.querySelector(".suite-up-text-name");
var gtmContainerName = gtmContainerNameElement.textContent.trim();

// GTM Container ID
var gtmNumber = document.querySelector('.gtm-container-public-id.md-gtm-theme').textContent.trim();

document.querySelectorAll('tr[gtm-table-row]').forEach(n => {
    const td2 = n.querySelector('td:nth-child(2)');
    const td3 = n.querySelector('td:nth-child(3)');
    const td4 = n.querySelector('td:nth-child(4)');
    const td5 = n.querySelector('td:nth-child(5)');

    const triggerName = td2 ? td2.textContent.trim() : '';
    const eventType = td3 ? td3.textContent.trim() : '';
    const firingTriggers = Array.from(n.querySelectorAll('td:nth-child(4) .small-trigger-chip')).map(conditionElement => conditionElement.textContent.trim());
    const lastEdited = td5 ? td5.textContent.trim() : '';


    // To find if Tag is currently paused
    const paused = n.classList.contains('gtm-table-row--paused');

    const tag = {
        Account: accountName,
        Property: gtmContainerName,
        GTM_Container: gtmNumber,
        Name: triggerName,
        Type: eventType,
        Firing_Triggers: firingTriggers,
        Last_Edited: lastEdited,
        Currently_Paused: paused
    }

    gtmData.push(tag);
})

//Output this to your clipboard for Excel or any CSV style program
function jsonToCSV(json) {
    const fields = Object.keys(json[0]);
    const csv = json.map(row => {
        return fields.map(fieldName => {
            // Check if the field value is an array and handle it accordingly
            if (Array.isArray(row[fieldName])) {
                return '"' + row[fieldName].join(';') + '"';  // Join array elements with semicolon and enclose in quotes
            } else {
                return JSON.stringify(row[fieldName], replacer); // Use replacer to handle other data types and escaping
            }
        }).join(",");
    });
    csv.unshift(fields.join(",")); // add header column
    return csv.join("\r\n");

    function replacer(key, value) {
        // Handle escaping of quotes in strings
        if (typeof value === 'string') {
            return value.replace(/"/g, '""');
        }
        return value;
    }
}

// Assuming 'gtmData' is your JSON array
const csvData = jsonToCSV(gtmData);

// Use the console's copy function
copy(csvData);

For those that prefer to watch than read, here’s a video:



Let me know in the comments if this worked for you!

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Your Script User - Marketer

Hi Nitish,

‘Last Edited’ does not seem to be working.

Can you please update script or check if it’s working for you?

Also, it requires to do some sort of modification on the Google sheet to get it ready to use.

Can you try to make it work out as well?

Thanks.