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 file definitions:
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 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
This is the recommended option to keep your GTM API types automatically updated.
-
Install the type definitions directly using a package manager like
npmorpnpm:pnpm install -D stape-gtm-api-types # or npm install --save-dev stape-gtm-api-typesIf you don't have one of them installed, you can follow the installation instructions:
-
After installing the package, you can reference the type definitions in your project as follows:
This method is useful if you prefer not to create a
jsconfig.jsonfile.You must add a special comment to the very top of each JavaScript file containing GTM Sandboxed API code where you want IntelliSense.
Add a special comment to the very top of each JavaScript file where you want IntelliSense. Make sure to add it exactly as is (with triple slashes).
- For Web GTM Sandboxed APIs:
/// <reference types="stape-gtm-api-types/web-gtm-sandboxed-apis" /> // Your GTM template code starts here... const copyFromDataLayer = require('copyFromDataLayer');
- For Server GTM Sandboxed APIs:
/// <reference types="stape-gtm-api-types/server-gtm-sandboxed-apis" /> // Your GTM template code starts here... const getAllEventData = require('getAllEventData');
Create a
jsconfig.jsoncontaining the following content in your project's root directory where the code of your template is located.- For Web GTM Sandboxed APIs:
{ "compilerOptions": { "paths": { "*": ["./node_modules/stape-gtm-api-types/web-gtm-sandboxed-apis"] } }, "include": ["**/*"] }- For Server GTM Sandboxed APIs:
{ "compilerOptions": { "paths": { "*": ["./node_modules/stape-gtm-api-types/server-gtm-sandboxed-apis"] } }, "include": ["**/*"] } -
Reload your editor if IntelliSense does not appear immediately.
⚠️ If you chose this option, you will not receive automatic updates, and you'll have to manually update the.d.tsfiles by copying and pasting them from this GitHub repository.
Choose the file definition (.d.ts file) that corresponds to the environment you are developing for (Web or Server). 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.tsfile in your project directory (e.g., in the same folder as your.jsfile). -
Add the following comment to the first line of your JavaScript file containing GTM Sandboxed API code where you want IntelliSense. Make sure to add it exactly as is (with triple slashes):
- For Web GTM Sandboxed APIs:
/// <reference path="./web-gtm-sandboxed-apis.d.ts" /> // Your GTM template code starts here... const copyFromDataLayer = require('copyFromDataLayer');
- For Server GTM Sandboxed APIs:
/// <reference path="./server-gtm-sandboxed-apis.d.ts" /> // Your GTM template code starts here... const getAllEventData = require('getAllEventData');
-
Adjust the path: make sure the
pathin the comment correctly points to the location of your.d.tsfile relative to your JavaScript 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.tsfile in your project's root directory. - Create a
jsconfig.jsonfile 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.
Both methods work perfectly. The Method 2 is recommended for easier updates and maintenance.
JetBrains IDEs will automatically index type definitions from installed npm packages. No manual copying required.
Other editors that support TypeScript/JavaScript language servers will automatically detect type definitions from npm packages.
For tag configuration examples, please refer to the /examples folder.
Created and developed by Giovani Ortolani Barbosa (LinkedIn, GitHub).
The GTM Sandboxed API IntelliSense is maintained by the Stape Team under the Apache 2.0 license.