Use custom templates for improved security & performance

Business Benefits

Ensure consistent deployment, the best user experience, and easiest management for custom tags.


Navigate to Templates in the Google Tag Manager user interface and click New by the template type you want to create to build a new template.

  • In Web containers, you can create tag and variable templates
  • In Server containers, you can also create Client templates

If you want a head start, you can use ready-made template.tpl files, such as Simo Ahava’s Google Tag Manager Templates, and import them into the Template Editor by clicking Import from the action menu in the top-right corner of the UI.

Add template metadata, such as Name, Icon, and Description in the Info tab of the template editor.

Fill in the template fields in the Fields tab of the template editor

  • Give each field a name that you can then reference in the Code Editor.
  • Don’t forget to explore the Field Configurations for each field by clicking the cogwheel icon in the field. Through field configurations, you can add things like validation rules or nested fields.

Available Field Configurations for the Simple Table field type

Add the template code in the Code tab of the template editor

Use the syntax data. to reference the fields you just created, replacing field name with the name of the field. The code editor uses a sandboxed JavaScript version which is explained on Google’s reference page.

Add all permissions in the Permissions tab of the editor, fill in required fields, and click Run to test the template in the editor and make sure there are no missing permissions.

Running the template will throw an error if there are problems with permissions.

You can also use the logToConsole() template API to add logging to your template. This helps with the build setup. Alternatively, you can add unit tests to the template using the Tests tab in the editor.

The template has permissions to set the _ga cookie and only the _ga cookie. Any other cookie name would raise a permission error.

Click Save to save the template in the respective Custom category - client, tag, or variable - once the test has run successfully.

If you want to publish the template to the Community Template Gallery, tick the Agree to Community Template Gallery Terms of Service checkbox in the Info tab and export the template following the submission guidelines.

Check this box if you want to prepare the template for gallery submission

Last edited by @hesh_fekry 2023-11-14T11:10:25Z