Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hussainm/c8111e916e58b97ceafb5394db9e213c to your computer and use it in GitHub Desktop.
Save hussainm/c8111e916e58b97ceafb5394db9e213c to your computer and use it in GitHub Desktop.
GTM Backup Script for Google Sheets
function getAccounts() {
var accountList = TagManager.Accounts.list();
for (var i = 0;i < accountList.account.length;i++) {
//if (accountList.account[i].accountId > 123456) { //if you reach a timeout, resume by manually updating this and re-running
getContainers(accountList.account[i].accountId,accountList.account[i].name);
Utilities.sleep(5000); //GTM only allows 25 queries per 100 seconds, so we have to slow it down
//}
}
}
function getContainers(accountId,accountName) {
try {
var containerList = TagManager.Accounts.Containers.list('accounts/' + accountId);
for (var i = 0;i < containerList.container.length;i++) {
getBackup(accountId,accountName,containerList.container[i].containerId);
Utilities.sleep(5000); //GTM only allows 25 queries per 100 seconds, so we have to slow it down
}
}catch(e){}
}
function getBackup(accountId,accountName,containerId) {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
try {
var containerBackup = TagManager.Accounts.Containers.Versions.live('accounts/' + accountId + '/containers/' + containerId);
sheet.appendRow([
accountName,containerBackup.container.name,accountId,containerId,JSON.stringify(containerBackup)
]);
}catch(e){
sheet.appendRow([
accountName,'',accountId,containerId,'ERROR'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment