PSECU wanted 18 tags and 18 triggers created for their GTM container. The specifications for each tag and trigger were pretty similar, and I had set up 6 other tags in the same way earlier. However, they were different enough that setting them up manually would be a little more tedious than I prefer. Having some prior experience with generating GTM items through code (see Using Code to Edit Many GTM Triggers Simultaneously), I thought I could save myself a little time and energy.
The first thing I did was get my sources together. Remember when I said I’d already set up 6 tags and triggers for this client? I exported just those tags and triggers from the GTM container so I had something to go off.
The first solution I attempted was to query TeamAI. I plugged in the specs for the new tags/triggers and fed it the JSON export of the 6 tags I had already set up. Unfortunately, TeamAI (at least the GPT-5 model) couldn’t handle generating this much at once and crashed my tab.
After that, I realized I didn’t need TeamAI to generate the final JSON for me — I only needed it to generate the code I could use to make it happen. …Unfortunately, this also crashed my tab.
So, I set about writing the code myself. Luckily, it’s not too difficult.
I started with the JavaScript that could make all the triggers for me. These triggers should all have 4 conditions according to the specs:
Because I wanted to write a function that would return a JSON object, I started by defining a function buildTriggerJSON() and making the return value equal to one of the triggers I’d exported from the GTM container. Then, anywhere I knew I’d need a value from the specs, I replaced the relevant part in the JSON with a string template literal, and added it as a parameter in the function definition. (You’ll see what I mean in a bit.)
I ended up with six parameters for my function:
From there, I made an array for each set of 3 triggers I’d have to make and fed them into my function, concatenating them all at the end. (For instance, for the Auto Loans application, I created an array [“Auto Loans”,”AutoLoan”,”&ScreenID=2012237″,”&ScreenID=2012236″,”&ScreenID=2012355″,1], then used the spread operator to feed it into my function buildTriggerJSON(…myArray)(), which returned 3 triggers.
After I had my trigger JSON, I fed it the tags. Tags that have triggers need a trigger ID to reference, which is why I did the triggers first. buildTagJSON() looks very similar to buildTriggerJSON, but it takes fewer parameters and only returns 1 tag per execute. The parameters it takes are:
I fed this function 18 destructured arrays in the same order I created the triggers (so that the IDs would match). I concatenated the results together, and then copied and pasted them into the tags and triggers properties of the JSON I exported. I saved over that JSON and imported it into GTM, and it worked first try.
Added 18 tags and 18 triggers to a GTM container that I created with JavaScript.
function buildTriggerJSON(appName, dlvLabel, screenId1, screenId2, screenId3, triggerId){
return([
{
“accountId”: “586717600”,”containerId”: “2776102”,”triggerId”: String(triggerId),
“name”: `Event – fx-application – ${appName} Pending`,
“type”: “CUSTOM_EVENT”,
“customEventFilter”: [
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{_event}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: “fx-application”
}
]
}
],
“filter”: [
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{DLV – dlvLabel}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: `Application | ${dlvLabel}`
}
]
},
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{DLV – dlvStatus}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: “Pending”
}
]
},
{
“type”: “CONTAINS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{Page URL}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: `${screenId1}`
}
]
}
],
“fingerprint”: “1759866912906”,
“parentFolderId”: “690”
},
{
“accountId”: “586717600”,”containerId”: “2776102”,”triggerId”: String(triggerId+1),
“name”: `Event – fx-application – ${appName} Denied`,
“type”: “CUSTOM_EVENT”,
“customEventFilter”: [
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{_event}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: “fx-application”
}
]
}
],
“filter”: [
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{DLV – dlvLabel}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: `Application | ${dlvLabel}`
}
]
},
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{DLV – dlvStatus}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: “Denied”
}
]
},
{
“type”: “CONTAINS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{Page URL}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: `${screenId2}`
}
]
}
],
“fingerprint”: “1759866912906”,
“parentFolderId”: “690”
},
{
“accountId”: “586717600”,”containerId”: “2776102”,”triggerId”: String(triggerId+2),
“name”: `Event – fx-application – ${appName} Approved`,
“type”: “CUSTOM_EVENT”,
“customEventFilter”: [
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{_event}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: “fx-application”
}
]
}
],
“filter”: [
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{DLV – dlvLabel}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: `Application | ${dlvLabel}`
}
]
},
{
“type”: “EQUALS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{DLV – dlvStatus}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: “Approved”
}
]
},
{
“type”: “CONTAINS”,
“parameter”: [
{
“type”: “TEMPLATE”,
“key”: “arg0”,
“value”: “{{Page URL}}”
},
{
“type”: “TEMPLATE”,
“key”: “arg1”,
“value”: `${screenId3}`
}
]
}
],
“fingerprint”: “1759866912906”,
“parentFolderId”: “690”
},
])
}
function buildTagJSON(appNameUnderscored, triggerId){
return([
{
“accountId”: “586717600”,
“containerId”: “2776102”,
“tagId”: String(triggerId),
“name”: `GA4 – ${appNameUnderscored}`,
“type”: “gaawe”,
“parameter”: [
{
“type”: “BOOLEAN”,
“key”: “sendEcommerceData”,
“value”: “false”
},
{
“type”: “LIST”,
“key”: “eventSettingsTable”,
“list”: [
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_submitted”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvSubmitted}}”
}
]
},
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_status”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvStatus}}”
}
]
},
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_label”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvLabel}}”
}
]
},
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_value”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvValue}}”
}
]
},
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_term”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvmetric4}}”
}
]
},
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_interest”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvmetric5}}”
}
]
},
{
“type”: “MAP”,
“map”: [
{
“type”: “TEMPLATE”,
“key”: “parameter”,
“value”: “application_limit”
},
{
“type”: “TEMPLATE”,
“key”: “parameterValue”,
“value”: “{{DLV – dlvmetric6}}”
}
]
}
]
},
{
“type”: “TEMPLATE”,
“key”: “eventName”,
“value”: `${appNameUnderscored}`
},
{
“type”: “TEMPLATE”,
“key”: “measurementIdOverride”,
“value”: “G-NXBRFDQH5H”
},
{
“type”: “TEMPLATE”,
“key”: “eventSettingsVariable”,
“value”: “{{Event Settings}}”
}
],
“fingerprint”: “1759867537404”,
“firingTriggerId”: [
String(triggerId)
],
“parentFolderId”: “690”,
“tagFiringOption”: “ONCE_PER_EVENT”,
“monitoringMetadata”: {
“type”: “MAP”
},
“consentSettings”: {
“consentStatus”: “NOT_SET”
}
}])
}