Custom Templates in Google Tag Manager: Taking Your GTM Setup to the Next Level
Google Tag Manager (GTM) is a free tool from Google that lets you add, manage, and update tags on your website without involving developers. It makes it easy to set up analytics, ad pixels, scripts, or even a cookie consent banner, all in one place.
GTM comes with a robust set of built-in tags, including Google Analytics, Google Ads, Floodlight, Hotjar, LinkedIn, and more. But sometimes that’s not enough, for example, when you need to:
- Add a Meta or TikTok pixel with extra parameters that are not supported by default.
- Integrate a non-standard API.
- Implement your own measurement protocol.
In these cases, people typically fall back on Custom HTML tags. They are powerful but come with downsides: low security, a lack of structure, and no parameter validation. That’s exactly why Google introduced Custom Templates.
What Are Custom Templates?
Templates are custom tags or variable templates in GTM, built on sandboxed JavaScript, an isolated environment where the code can run safely. In simple terms, it’s a secure way to add custom logic to GTM and share it with others without the risks that come with Custom HTML. Each template has a JSON structure that describes what the code is allowed to do (e.g., fetch or logToConsole), which input parameters it accepts, and which UI fields should appear for users (variable names, checkboxes, selectors, etc.).

Essentially, a Custom Template is a mini-app within GTM.
Why You Should Use Custom Templates
Custom Templates let you:
- Extend GTM functionality without worrying that a piece of code will “break” the site.
- Support custom integrations like Meta Conversion API (via server-side GTM), TikTok Advanced Matching, CRM → GA4 Measurement Protocol, or your own backend API calls.
- Streamline teamwork to build the template once and let your teammates simply fill in the fields.
Examples of Custom Templates
- Facebook Conversion API Tag lets you send events directly to Meta without manual code.
- GA4 Event Tag (Server) is perfect for setting up server-side analytics.
- TikTok Pixel Tag is a ready-to-use template for events like AddToCart, Purchase, CompletePayment, and more.
- Custom HTTP Request Tag is a universal way to send data to any endpoint.
- Cookie Consent Templates are good for managing user consent (GDPR/Consent Mode).
Where to Find Ready-to-Use Templates
Google provides an official repository, Community Template Gallery. Here you can find hundreds of verified templates from Meta and TikTok to Sendinblue or Klaviyo. Add it to your container and use it just like any other tag or variable.
How to Create Your Custom Template
If the built-in templates or the ones from the community gallery don’t cover your needs, you can create your own. GTM includes a dedicated Template Editor for that:
1. In Templates → New → Tag Template, click Create.

2. Define which fields the user can fill in (for example, a pixel ID or an API key).

3. Add your code in the Code tab, as it runs inside a sandboxed environment.

4. In Permissions, allow only the actions your template actually needs (for example, Send HTTP Request).

5. Save and test the template, and now you can use it just like any standard tag.
Example
Let’s say you want to send every Form Submit event from your website to your CRM via webhook. Instead of relying on a Custom HTML tag, you can build a secure Custom Template in the Template Editor.
Template fields (Fields tab):
| Display name | Type | Variable name | Default value |
| CRM Endpoint URL | Text | endpointUrl | https://example-crm.com/api/event |
| Event Name | Text | eventName | form_submit |
| User Email | Text | userEmail | (empty) |

Template code (Code tab):
// Sandbox APIs
const queryPermission = require('queryPermission');
const getUrl = require('getUrl');
const sendHttp = require('sendHttp');
const logToConsole = require('logToConsole');
const JSON = require('JSON');
// 1) Safely read gclid from URL (only if allowed in Permissions)
var gclid = null;
if (queryPermission('get_url', 'query', 'gclid')) {
gclid = getUrl('query', false, null, 'gclid'); // returns string|null
}
// 2) Build the payload
const payload = {
event: data.eventName,
email: data.userEmail
};
// Add gclid only if available
if (gclid) {
payload.gclid = gclid;
}
// 3) Send POST request
sendHttp(data.endpointUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(function(response) {
logToConsole('✅ Event sent to CRM', {
status: response && response.statusCode,
payload: payload
});
data.gtmOnSuccess();
})
.catch(function(err) {
logToConsole('❌ CRM request failed', err);
data.gtmOnFailure(err && err.message ? err.message : 'Request failed');
});
Setting up the tag and testing (Testing + Preview tabs):
Save the template → Tags → New → Tag Type → Custom → Send Event to CRM
Fill in the fields:
- Endpoint: Your URL (for testing, it is convenient to use a temporary webhook such as webhook.site).
- Event name: form_submit.
- Email: A test email.
Now add a trigger (for example, Form Submission or Click). Then enable Preview, repeat the event, and you will see the log and response status in the console.
That’s it! Nothing too complicated!
Wrapping Up
Custom Templates are the next step in GTM evolution. They:
- Make tag management safer (sandboxed environment).
- Simplify code reuse.
- Reduce dependency on developers.
- Allow you to build your own tools for any analytics needs.
If you often work with custom events or APIs, give Custom Templates a try. They will save you time, reduce headaches, and make your GTM container much more professional.

