Building Custom GTM Tag Templates With Sandboxed JavaScript APIs
Why Custom GTM Templates beat Custom HTML tags for security, performance, and maintainability—with practical Sandboxed JavaScript API examples for Web and Server GTM.
Syed Yasir
Conversion Tracking & Server-Side Attribution Expert
Table of Contents
Most legacy GTM containers are littered with fragile Custom HTML tags. Custom HTML executes arbitrary JavaScript with unrestricted access to the DOM, cookies, and global window object—making security audits painful and creating accidental script collisions.
GTM Custom Templates (both Web and Server-Side) replace Custom HTML with Sandboxed JavaScript: a strict subset of ES5.1 where every network request, cookie write, and global read must be explicitly declared via granular permissions.
Why Custom Templates Outperform Custom HTML
- Least-Privilege Security: A template can only send requests to URLs or read cookies explicitly approved in its Permissions tab.
- Reusable UI Fields: Marketing teams configure parameters via clean form fields instead of editing raw JavaScript.
- Unit Testing Built In: GTM’s Template Editor includes a native Tests tab so you can mock APIs and assert tag behavior before publishing.
- Zero
eval()or Unsafe DOM Injection: Keeps your Content Security Policy (CSP) clean and protects Core Web Vitals.
Example: Server-Side GTM Custom Tag Using Sandboxed JS
Below is a clean sGTM Custom Tag template pattern that reads Event Data, checks Consent Mode state, and dispatches an authenticated JSON webhook using sendHttpRequest:
const getAllEventData = require('getAllEventData');
const sendHttpRequest = require('sendHttpRequest');
const JSON = require('JSON');
const isConsentGranted = require('isConsentGranted');
// Enforce Consent Mode V2 before sending advertising signals
if (!isConsentGranted('ad_storage')) {
data.gtmOnSuccess();
return;
}
const eventData = getAllEventData();
const payload = JSON.stringify({
event_name: eventData.event_name,
event_id: eventData.event_id,
value: eventData.value,
currency: eventData.currency,
});
sendHttpRequest(
data.endpointUrl,
(statusCode) => {
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
},
payload
);
Key Sandboxed JavaScript Gotchas
- Always call
data.gtmOnSuccess()ordata.gtmOnFailure(): Failing to invoke the callback leaves the tag hanging in GTM Preview. - Use
require(...)for all APIs: Standard globals likefetch,window, ordocumentare intentionally unavailable. UsesendPixel,injectScript,copyFromDataLayer, orsendHttpRequest. - Lock Down Permissions: Avoid wildcard URL permissions (
https://*/*); restrictsendHttpRequeststrictly to your target API host.
Need to clean up legacy Custom HTML tags or build custom templates for your team? Explore our Google Tag Manager Setup service or book a free tracking audit.
Free Tracking Audit
Want Me to Audit Your Conversion Tracking Stack?
Book a free tracking audit. I'll review your GTM, GA4, Server-Side Tracking, Meta CAPI, Google Ads Enhanced Conversions, and Consent Mode V2 setup and highlight exactly what to fix.