|
2 | 2 |
|
3 | 3 | 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. |
4 | 4 |
|
5 | | -There are two files: |
| 5 | +There are two file definitions: |
6 | 6 | - `server-gtm-sandboxed-apis.d.ts`: For **Server-side** GTM templates. |
7 | 7 | - `web-gtm-sandboxed-apis.d.ts`: For **Web** GTM templates. |
8 | 8 |
|
9 | | ---- |
10 | | - |
11 | | -## What Are These Files? |
| 9 | +## What Are These Definitions? |
12 | 10 |
|
13 | 11 | 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. |
14 | 12 |
|
15 | | -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. |
| 13 | +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. |
16 | 14 |
|
17 | 15 |
|
18 | 16 | https://github.com/user-attachments/assets/868bbe2c-3e28-47b1-b195-cdfc509ec340 |
19 | 17 |
|
20 | | - |
21 | 18 | --- |
22 | 19 |
|
23 | 20 | ## How to Use |
24 | 21 |
|
25 | | -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. |
| 22 | +1. Install the type definitions directly using a package manager like `npm` or `pnpm`: |
| 23 | + ```bash |
| 24 | + pnpm install -D stape-gtm-api-types |
| 25 | + # or |
| 26 | + npm install --save-dev stape-gtm-api-types |
| 27 | + ``` |
| 28 | + |
| 29 | + This is the recommended way to keep your GTM API types up to date. |
| 30 | + |
| 31 | +2. After installing the package, you can reference the type definitions in your project as follows: |
| 32 | + |
| 33 | + ### Method 1: Using a Triple-Slash Directive (File-by-File) |
26 | 34 |
|
27 | | -### Method 1: Using a Triple-Slash Directive (File-by-File) |
| 35 | + 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 containing GTM Sandboxed API code where you want IntelliSense. |
28 | 36 |
|
29 | | -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. |
| 37 | + Add a special comment to the **very top** of each JavaScript file where you want IntelliSense. |
| 38 | + Make sure to add it exactly as is (with triple slashes). |
30 | 39 |
|
31 | | -1. **Place the `.d.ts` file** in your project directory (e.g., in the same folder as your `.js` file). |
32 | | -2. **Add the following comment** to the first line of your `.js` file: |
| 40 | + - For **Web** GTM Sandboxed APIs: |
33 | 41 | ```javascript |
34 | | - /// <reference path="./web-gtm-sandboxed-apis.d.ts" /> |
| 42 | + /// <reference types="stape-gtm-api-types/web-gtm-sandboxed-apis" /> |
35 | 43 |
|
36 | 44 | // Your GTM template code starts here... |
37 | 45 | const copyFromDataLayer = require('copyFromDataLayer'); |
38 | 46 | ``` |
39 | | -3. **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. |
40 | 47 |
|
41 | | -### Method 2: Using `jsconfig.json` |
| 48 | + - For **Server** GTM Sandboxed APIs: |
| 49 | + ```javascript |
| 50 | + /// <reference types="stape-gtm-api-types/server-gtm-sandboxed-apis" /> |
| 51 | +
|
| 52 | + // Your GTM template code starts here... |
| 53 | + const getAllEventData = require('getAllEventData'); |
| 54 | + ``` |
| 55 | + |
| 56 | + ### Method 2: Using `jsconfig.json` |
42 | 57 |
|
43 | | -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. |
| 58 | + Create a `jsconfig.json` containing the following content in your project's root directory where the code of your template is located. |
44 | 59 |
|
45 | | -1. **Place the `.d.ts` file** in your project's root directory. |
46 | | -2. **Create a `jsconfig.json` file** in the same root directory. |
47 | | -3. **Add the following content** to your `jsconfig.json`: |
| 60 | + - For **Web** GTM Sandboxed APIs: |
48 | 61 | ```json |
49 | 62 | { |
50 | 63 | "compilerOptions": { |
51 | | - "target": "esnext" |
| 64 | + "paths": { |
| 65 | + "*": ["./node_modules/stape-gtm-api-types/web-gtm-sandboxed-apis"] |
| 66 | + } |
52 | 67 | }, |
53 | 68 | "include": ["**/*"] |
54 | 69 | } |
55 | 70 | ``` |
56 | | -4. **Reload Your Editor**: If IntelliSense doesn't appear immediately, restart your editor or use the "Reload Window" command. |
57 | 71 |
|
58 | | -With this setup, the editor will automatically find and use the type definitions for all `.js` files in your project. |
| 72 | + - For **Server** GTM Sandboxed APIs: |
| 73 | + ```json |
| 74 | + { |
| 75 | + "compilerOptions": { |
| 76 | + "paths": { |
| 77 | + "*": ["./node_modules/stape-gtm-api-types/server-gtm-sandboxed-apis"] |
| 78 | + } |
| 79 | + }, |
| 80 | + "include": ["**/*"] |
| 81 | + } |
| 82 | + ``` |
| 83 | +
|
| 84 | +3. Reload your editor if IntelliSense does not appear immediately. |
59 | 85 |
|
60 | 86 | --- |
61 | 87 |
|
62 | 88 | ## Editor-Specific Instructions |
63 | 89 |
|
| 90 | +
|
64 | 91 | ### Visual Studio Code |
65 | 92 |
|
66 | | -Both `jsconfig.json` and the triple-slash directive methods work perfectly. The `jsconfig.json` approach is generally more robust and requires less maintenance. |
| 93 | +Both methods work perfectly. The **Method 2** is recommended for easier updates and maintenance. |
67 | 94 |
|
68 | 95 | ### JetBrains IDEs (WebStorm, IntelliJ IDEA, etc.) |
69 | 96 |
|
70 | | -JetBrains IDEs offer the simplest setup: |
71 | | -
|
72 | | -1. **Place the `.d.ts` file** anywhere in your project directory. |
73 | | -
|
74 | | -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. |
| 97 | +JetBrains IDEs will automatically index type definitions from installed `npm` packages. No manual copying required. |
75 | 98 |
|
76 | 99 | ### Other Editors (Sublime Text, Neovim, etc.) |
77 | 100 |
|
78 | | -For other modern editors that use a Language Server Protocol (LSP) for JavaScript and TypeScript, the process is generally the same as for JetBrains: |
79 | | - |
80 | | -1. **Ensure your editor has a TypeScript/JavaScript language server** installed and configured. |
81 | | -2. **Place the `.d.ts` file** in the root of your project folder. |
82 | | - |
83 | | -The language server should automatically detect and use the type definitions to provide IntelliSense. |
| 101 | +Other editors that support TypeScript/JavaScript language servers will automatically detect type definitions from `npm` packages. |
84 | 102 |
|
85 | 103 | --- |
86 | 104 |
|
|
0 commit comments