Skip to content

Instantly share code, notes, and snippets.

@joechristopher
Last active July 29, 2021 16:24
Show Gist options
  • Save joechristopher/d07c377885b1d55987ae3b89f11861b0 to your computer and use it in GitHub Desktop.
Save joechristopher/d07c377885b1d55987ae3b89f11861b0 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'
]);
}
}
@joechristopher
Copy link
Author

Remember that you must enable GTM v2 API via Resources > Advanced Google Services and turn on in the Cloud Platform project too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment