gtm-type-generator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +116 -0
  2. package/dist/index.js +544750 -0
  3. package/package.json +51 -0
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Google Tag Manager DataLayer Type Generator (`gtm-type-generator`)
2
+
3
+ [![npm version](https://img.shields.io/npm/v/gtm-type-generator.svg)](https://www.npmjs.com/package/gtm-type-generator)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A CLI tool that automatically generates strict TypeScript definitions for your `window.dataLayer.push` method by directly fetching your configuration from the Google Tag Manager (GTM) API.
7
+
8
+ ## Why?
9
+ Without strict types, `dataLayer.push` is a black box. A typo in an event name or a missing variable can silently break your tracking.
10
+ This tool solves that by looking at your actual GTM workspace and creating types that enforce correctness at compile time.
11
+
12
+ ```typescript
13
+ // With gtm-type-generator, this gives a TS error because 'method' is required for the 'login' event!
14
+ window.dataLayer.push({ event: 'login' });
15
+
16
+ // ✅ Correct
17
+ window.dataLayer.push({ event: 'login', method: 'Google' });
18
+ ```
19
+
20
+ ## Features
21
+ - **Type Safety**: Enforces correct `event` names and their associated variables based on your actual GTM Triggers and Tags.
22
+ - **Auto-Sync**: Connects directly to the Tag Manager API v2 to fetch your latest container configuration.
23
+ - **Seamless Integration**: Designed to be run as an npm script to keep your codebase in sync with your GTM workspace.
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ npm install -D gtm-type-generator
29
+ ```
30
+
31
+ ## Authentication Setup
32
+
33
+ To access the GTM API, you need a Google Service Account:
34
+ 1. Go to the [Google Cloud Console](https://console.cloud.google.com/) and enable the **Tag Manager API**.
35
+ 2. Create a **Service Account** and download its JSON key file.
36
+ 3. **CRITICAL**: Go to your [Google Tag Manager Admin Panel](https://tagmanager.google.com/), select "User Management", and invite the Service Account's email address with at least **Read** permissions for the target container.
37
+
38
+ ## Usage
39
+
40
+ ### 1. Add an npm script
41
+ Add a script to your `package.json` to easily update your types:
42
+
43
+ ```json
44
+ {
45
+ "scripts": {
46
+ "update:gtm-type": "gtm-type-generator generate --account-id 123456 --container-id 987654 --workspace-id 2 --key-file ./gtm-service-account.json --output src/types/gtm.d.ts"
47
+ }
48
+ }
49
+ ```
50
+
51
+ *Tip: You can also use environment variables (`GTM_ACCOUNT_ID`, `GTM_CONTAINER_ID`, `GTM_WORKSPACE_ID`, `GOOGLE_APPLICATION_CREDENTIALS`) instead of passing them as CLI flags.*
52
+
53
+ Run the script:
54
+ ```bash
55
+ npm run update:gtm-type
56
+ ```
57
+
58
+ ### 2. Configure TypeScript (`tsconfig.json`)
59
+ Ensure your TypeScript compiler includes the generated `.d.ts` file so it automatically extends the global `Window` interface across your project.
60
+
61
+ ```json
62
+ {
63
+ "compilerOptions": {
64
+ // ... other options
65
+ },
66
+ "include": [
67
+ "src/**/*",
68
+ "src/types/gtm.d.ts" // Ensure the output path is included here
69
+ ]
70
+ }
71
+ ```
72
+
73
+ ## Development
74
+
75
+ ### Prerequisites
76
+ - Node.js 20+
77
+ - npm
78
+
79
+ ### Setup
80
+ ```bash
81
+ npm install
82
+ ```
83
+
84
+ ### Scripts
85
+ - `npm run build`: Build the project using esbuild.
86
+ - `npm test`: Run unit tests using Vitest.
87
+ - `npm run lint`: Run ESLint to check for code style and potential errors.
88
+ - `npm run lint -- --fix`: Automatically fix linting issues.
89
+
90
+ ### CI/CD
91
+ This project uses GitHub Actions for continuous integration and delivery:
92
+ - **CI**: Runs on every push to `main` and all Pull Requests. It executes linting, tests, and a build to ensure code quality.
93
+ - **Release**: Automatically publishes to npm when a new tag starting with `v` (e.g., `v1.0.0`) is pushed to the repository. Note: Requires `NPM_TOKEN` to be set in GitHub Secrets.
94
+
95
+ ## Architecture & Extraction Process
96
+
97
+ This tool generates types by analyzing the relationships between Triggers, Tags, and Variables in your GTM workspace.
98
+
99
+ 1. **API Client**: Authenticates and fetches Triggers, Tags, and Variables from the specified GTM workspace.
100
+ 2. **Config Extraction**:
101
+ - **Trigger Analysis**: Scans for "Custom Event" triggers and extracts the `event` name (e.g., `login`, `purchase`).
102
+ - **Variable Identification**: Finds all Tags that fire on those triggers and recursively extracts any `{{Data Layer Variable}}` references within their parameters or HTML.
103
+ - **Mapping**: Maps the identified variables to the corresponding `event` name.
104
+ 3. **Generator**: Outputs a union type `GtmDataLayerEvent` where each member represents a specific GTM custom event and its required/optional variables.
105
+
106
+ ```mermaid
107
+ graph TD
108
+ API[GTM API v2] --> Triggers[Extract Custom Event Triggers]
109
+ Triggers --> Tags[Identify Linked Tags]
110
+ Tags --> Vars[Extract Data Layer Variables from Tags]
111
+ Vars --> Union[Map to GtmDataLayerEvent Union Member]
112
+ Union --> TS[Generate gtm.d.ts]
113
+ ```
114
+
115
+ ## License
116
+ MIT