This guide explains how to use the provided TypeScript Declaration Files (.d.ts
) to enable IntelliSense (autocompletion and hover-documentation) for Google Tag Manager's Sandboxed JavaScript APIs in your code editor.
There are two files:
server-gtm-sandboxed-apis.d.ts
: For Server-side GTM templates.web-gtm-sandboxed-apis.d.ts
: For Web GTM templates.
Google Tag Manager's custom templates run in a "sandboxed" JavaScript environment that has a special set of APIs (like copyFromDataLayer
or sendHttpRequest
). Standard code editors don't know about these custom APIs, so they can't provide any help as you code.
These .d.ts
files act as a guide for your editor. They define the signatures, parameters, and documentation for every GTM API, effectively teaching your editor how the GTM environment works.
intellisense-demo.mov
Choose the file that corresponds to the environment you are developing for (Web or Server) and place it in your project folder. Then, choose one of the following methods to activate IntelliSense.
This method is useful if you prefer not to create a jsconfig.json
file. You must add a special comment to the very top of each JavaScript file where you want IntelliSense.
- Place the
.d.ts
file in your project directory (e.g., in the same folder as your.js
file). - Add the following comment to the first line of your
.js
file:/// <reference path="./web-gtm-sandboxed-apis.d.ts" /> // Your GTM template code starts here... const copyFromDataLayer = require('copyFromDataLayer');
- Adjust the path: Make sure the
path
in the comment correctly points to the location of your.d.ts
file relative to your.js
file.
This is a modern and reliable method for any project, even if it's just a single file. It tells your editor to treat the entire folder as a JavaScript project.
- Place the
.d.ts
file in your project's root directory. - Create a
jsconfig.json
file in the same root directory. - Add the following content to your
jsconfig.json
:{ "compilerOptions": { "target": "esnext" }, "include": ["**/*"] }
- Reload Your Editor: If IntelliSense doesn't appear immediately, restart your editor or use the "Reload Window" command.
With this setup, the editor will automatically find and use the type definitions for all .js
files in your project.
Both jsconfig.json
and the triple-slash directive methods work perfectly. The jsconfig.json
approach is generally more robust and requires less maintenance.
JetBrains IDEs offer the simplest setup:
- Place the
.d.ts
file anywhere in your project directory.
That's it. The IDE will automatically index the file and provide IntelliSense across the entire project without any additional configuration. You do not need a jsconfig.json
file or triple-slash directives.
For other modern editors that use a Language Server Protocol (LSP) for JavaScript and TypeScript, the process is generally the same as for JetBrains:
- Ensure your editor has a TypeScript/JavaScript language server installed and configured.
- Place the
.d.ts
file in the root of your project folder.
The language server should automatically detect and use the type definitions to provide IntelliSense.
Created and developed by Giovani Ortolani Barbosa (LinkedIn, GitHub).