Skip to content

Instantly share code, notes, and snippets.

@davecurrierseo
Created September 21, 2023 20:34
Show Gist options
  • Save davecurrierseo/fbac2ec82ef65325f454bd6508605383 to your computer and use it in GitHub Desktop.
Save davecurrierseo/fbac2ec82ef65325f454bd6508605383 to your computer and use it in GitHub Desktop.
Google Tag Manager CSV
// Just paste this into the console when viewing gtm tags
// Find the table element
var table = document.querySelector('table.gtm-multiselect-table');
if (!table) {
console.error('Table not found on this page.');
} else {
// Extract data from the table
var data = [];
var rows = table.querySelectorAll('tr.wd-tag-row');
rows.forEach(function (row) {
var rowData = [];
var cells = row.querySelectorAll('td');
cells.forEach(function (cell, index) {
var cellData = cell.textContent.trim();
// Check if it's the 3rd column and contains elements with the specified class
if (index === 2) {
var smallTriggerChips = cell.querySelectorAll('.small-trigger-chip.md-gtm-theme');
if (smallTriggerChips.length > 0) {
// Extract text from each small trigger chip and join with line breaks
cellData = Array.from(smallTriggerChips, function (chip) {
return chip.textContent.trim();
}).join('\n');
}
}
rowData.push(cellData);
});
data.push(rowData.join(',')); // Separate columns with commas for CSV
});
// Create a CSV string
var csvContent = 'data:text/csv;charset=utf-8,' + data.join('\n');
// Create a download link and trigger the download
var encodedUri = encodeURI(csvContent);
var link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download', 'table_data.csv');
document.body.appendChild(link);
link.click();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment