A small, focused CLI for Google Tag Manager, built on the Tag Manager API v2
and your own Google login (OAuth). Every command prints JSON to stdout and exits
non-zero on error, so they compose cleanly with jq and in CI.
Read-only
accounts: list the GTM accounts you can access.containers: list the containers under an account.dump: full audit dump of an account. For every container it pulls the live published version (tags, triggers, variables, built-in variables), falling back to the Default Workspace when a container has never been published.export: reconstruct a container as GTM export JSON (the API has no export endpoint), for backups or to round-trip intopush.diff: compare a container between two refs (live, workspace, a version id, or a file). Tag trigger references are compared by name, so reassigned ids don't show up as spurious changes.audit: run best-practice checks across an account (consent gating, consent-setting consistency, GA4↔Meta conversion parity, Meta standard-event semantics, naming-convention drift). Exits non-zero on findings, so it drops into a pre-publish gate.
Mutating
push: create a new container under an existing account from a GTM export JSON, populate it, and cut a named, unpublished version.publish: publish an existing version live. Refuses to run without--confirm.
Auth
login/logout/whoami: run a fresh consent flow, clear the cached token, or show which token is in use and what it can reach.
It never creates accounts (the API can't, see
Constraints) and never publishes without an
explicit --confirm.
# one-off
npx @mihilista/gtm-cli accounts
# or globally
pnpm add -g @mihilista/gtm-cli
gtm-cli --helpRequires Node.js >= 18.
- In the Google Cloud Console, enable the "Tag Manager API" for a project.
- Create an OAuth client ID of type Desktop app. Download the JSON.
- Save it to
~/.config/gtm-cli/oauth-client.json. - If your OAuth consent screen is in "Testing" mode, add your Google account as a test user.
The first authenticated command prints a consent URL (it does not auto-open
a browser, so you can paste it into whichever browser you like) and waits on a
local loopback server for the redirect. The refresh token is then cached at
~/.config/gtm-cli/token.json, so later runs are non-interactive.
The authorizing browser must be on the same machine (the OAuth redirect
points at http://localhost:<port>). Your Google account needs Edit access
to the chosen GTM account for push, and Publish access for publish; read
access is enough for accounts, containers, dump, export, diff, and
audit.
Config location. Files live under
$XDG_CONFIG_HOME/gtm-cli/(i.e.~/.config/gtm-cli/). For backwards compatibility the CLI also reads the legacy~/.config/gtm-skill/directory if the new one is empty, so a token cached by the original skill keeps working. New tokens are always written to the new location. To force a fresh consent (e.g. to switch Google accounts), delete~/.config/gtm-cli/token.json.
--account and --container accept an id, an exact name, or a name substring
(--container also matches the GTM-XXXX public id).
# List accounts / containers.
gtm-cli accounts
gtm-cli containers --account "example.com"
# Audit an account's live config (great as a pre-publish gate).
gtm-cli audit --account 1234567 # exit 1 if any errors
gtm-cli audit --account 1234567 --strict # exit 1 on warnings too
# Dump everything, pipe into jq.
gtm-cli dump --account "example.com" | jq '.containers[].tags | length'
# Back up a container as GTM export JSON.
gtm-cli export --account 1234567 --container GTM-XXXX > backup.json
# What would publishing the Default Workspace change?
gtm-cli diff --account 1234567 --container GTM-XXXX
# Compare a generated file against what's live:
gtm-cli diff --account 1234567 --container GTM-XXXX --from live --to file:new.json
# Create + populate a container, then cut a version (does NOT publish).
gtm-cli push \
--file gtm-container-www-example-com.json \
--account 1234567 \
--container-name "www.example.com" \
--version-name "Initial setup: www.example.com"
# Publish a reviewed version live (production-affecting, needs --confirm).
gtm-cli publish --account 1234567 --container GTM-XXXX --version-id 7 --confirm
# Auth management.
gtm-cli whoami
gtm-cli login # force a fresh consent (e.g. switch Google accounts)
gtm-cli logout # clear cached token(s)For push, --container-name defaults to container.name in the JSON and
--version-name defaults to Initial setup: <name>.
A ref selects which version of a container to read:
live: the live published version (default).workspace(orws): the Default Workspace (your unpublished edits).- a version id, e.g.
7orv7. file:<path>: a local GTM export JSON (also any path ending in.json).
accounts.containers.createunder the chosen account, getting the realGTM-XXXXXXpublic id.- Finds the auto-created Default Workspace.
built_in_variables.createfor each built-in (ignores409for ones enabled by default).variables.createfor each custom variable.triggers.createfor each trigger, building aplaceholder id -> real idmap.tags.createfor each tag, with firing/blocking trigger ids remapped.create_versionwith a name, producing a reviewable, unpublished container version.
It prints a JSON summary: account, container public id, tagManagerUrl, the new
version id, and counts of entities created.
The container is populated and versioned but not live. Open the
tagManagerUrl, run Preview to confirm tags fire, then Publish the
version manually. If consent mode was enabled, your CMP must push the consent
update (e.g. consent_marketing: true) on grant.
These shape why the tool works the way it does:
- No account creation. Tag Manager API v2 has no
accounts.create(accounts areget/list/updateonly). New accounts are made in the GTM UI. This CLI creates containers, never accounts. - No JSON import endpoint. Container export/import is a UI-only feature.
To populate a container via the API you must create each entity individually,
which is exactly what
pushdoes. - Trigger ids are server-assigned. The
triggerIds in an export JSON are placeholders. Triggers are created first, aplaceholder -> realmap is built, then each tag'sfiringTriggerId/blockingTriggerIdis remapped. Built-in trigger ids (2147479553Initialization,2147479573All Pages) are not intrigger[]and pass through unchanged. Variables are referenced by name, so they need no remap.
403on create usually means the OAuth identity lacks Edit access on that account, or the wrong account id was passed.- A
compilerErroron version creation means an entity references something missing; review the workspace in the GTM UI.
pnpm install
pnpm typecheck # tsc --noEmit (strict)
pnpm test # vitest, covers the pure translate/diff/audit logic
pnpm build # tsup -> dist/index.js (ESM, with shebang)The CLI is plain ESM TypeScript with no runtime config file. Dependencies
(googleapis, google-auth-library, commander) are kept external and
resolved from node_modules rather than bundled.
MIT