Skip to content

Asim-Tahir/gtm-template-sandbox-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GTM Template Sandbox Types

Downloads License Version

TypeScript types for Google Tag Manager(GTM) template's sandboxed JS APIs both for template and testing

Installation

npm install

npm i -D gtm-template-sandbox-types
yarn add -D gtm-template-sandbox-types
pnpm add -D gtm-template-sandbox-types
bun add -D gtm-template-sandbox-types

Usage:

// tsconfig.json
{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.web.json"
    },
    {
      "path": "./tsconfig.test.json"
    }
  ]
}
// tsconfig.web.json
{
  "compilerOptions": {
    "allowJs": true,
    "types": [
      "gtm-template-sandbox-types/web",
    ]
  },
  "include": [
    "src/**/*.js",
  ]
}
// tsconfig.test.json
{
  "compilerOptions": {
    "allowJs": true,
    "types": [
      "gtm-template-sandbox-types/test",
    ]
  },
  "include": [
    "tests/setupTests.js",
    "tests/**/*.test.js",
  ]
}
// src/index.js
/// <reference types="gtm-template-sandbox-types/web" />
// or use reference types instead of tsconfig types field

// Full types are available in the web GTM template context, including GTM template APIs, global variables, and utility types.
const logToConsole = require("logToConsole");

logToConsole("Hello, GTM Template Sandbox!");
logToConsole("My GTM Template parameters are:", data);
// tests/sample.test.js
/// <reference types="gtm-template-sandbox-types/test" />
// or use reference types instead of tsconfig types field

const mockData = {
  // Mocked field values
};

// Call runCode to run the template's code.
runCode(mockData);

// Verify that the tag finished successfully.
assertApi("gtmOnSuccess").wasCalled();

License

MIT License