How do I build a GTM custom template/variable to log events into a third-party SDK like Amplitude?
Build a custom tag template using GTM's sandboxed JavaScript APIs (injectScript, callInWindow, gtmOnSuccess/gtmOnFailure) rather than a raw Custom HTML tag - it gives you a reusable, permission-scoped tag with a proper field UI for API keys and event names, instead of a one-off script block every editor has to remember and re-paste correctly.
Why this happens
A raw Custom HTML tag calling a third-party SDK works, but it has no field validation, no permission declaration so GTM can warn about what domains or APIs it touches, and has to be copy-pasted correctly into every environment that needs it. A real custom template solves all three, and is what a governed container should use instead of ad hoc HTML tags for recurring third-party integrations.
Fix it
- In GTM, go to Templates > New (Tag Templates), which opens the template editor with Info, Fields, and Code tabs.
- On the Fields tab, define the inputs your template needs - for a third-party SDK, typically an API Key text field, an Event Name field, and an optional Event Properties table so users configure the tag without touching code.
- On the Code tab, write sandboxed JS: use
injectScriptto load the SDK if not already present, thencallInWindowto invoke its logging method once the script confirms loaded, reading values fromdata.eventName/data.eventProperties. - Call
data.gtmOnSuccess()once the SDK call completes, anddata.gtmOnFailure()in any error path - GTM's tag firing status in Preview relies on these callbacks to know whether the tag actually succeeded, not just that its code ran without throwing. - Review the auto-detected permissions on the Info/Permissions tab - GTM generates this from your code, and it is what other editors on the container see before approving or using the template, which is the governance benefit over a Custom HTML tag that declares nothing.
How to verify it worked
In GTM Preview, fire the tag and confirm it shows a green "Fired" status specifically, not just "evaluated" - a custom template tag that never calls its success or failure callback shows as stuck or pending in Preview, which is itself a sign the callback wiring is incomplete. Cross-check inside the third-party SDK's own live event view that the event actually landed with the expected name and properties, not just that GTM's own status looked correct.
Still stuck after working through this?
Send us what you are seeing in Preview and the Network tab. We trace GTM containers for a living and can usually tell you what is actually happening in one look.
Ask a GTM Engineer