@owlmeans/web-gtm 0.1.18-rc.2 → 0.1.18-rc.20
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.
- package/README.md +15 -0
- package/agent-meta/manifest.json +16 -0
- package/agent-meta/skills/web-gtm/SKILL.md +96 -0
- package/package.json +2 -2
- package/tests/head.spec.ts +65 -0
- package/build/.gitkeep +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!-- owlmeans:agent-guidance:start -->
|
|
2
|
+
## Agent guidance
|
|
3
|
+
|
|
4
|
+
This package ships embedded agent skills under `agent-meta/`. After installing your
|
|
5
|
+
`@owlmeans/*` packages, run the OwlMeans agent-skills installer to place them into
|
|
6
|
+
your project's skill store (`.agents/skills/`):
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npx @owlmeans/agent-skills@^0.1.18-rc.26
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The embedded files are version-matched to this package release. Do not edit them
|
|
13
|
+
directly — they are regenerated on each publish. To contribute guidance edits,
|
|
14
|
+
open a PR against the source monorepo.
|
|
15
|
+
<!-- owlmeans:agent-guidance:end -->
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 2,
|
|
3
|
+
"package": "@owlmeans/web-gtm",
|
|
4
|
+
"version": "0.1.18-rc.20",
|
|
5
|
+
"generatedAt": "2026-09-15T13:13:28.703Z",
|
|
6
|
+
"canonicalRepo": "https://github.com/owlmeans/common",
|
|
7
|
+
"entries": [
|
|
8
|
+
{
|
|
9
|
+
"kind": "skill",
|
|
10
|
+
"name": "web-gtm",
|
|
11
|
+
"category": "package-specific",
|
|
12
|
+
"file": "skills/web-gtm/SKILL.md",
|
|
13
|
+
"canonicalPath": ".agents/skills/web-gtm/SKILL.md"
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: web-gtm
|
|
3
|
+
description: How to use @owlmeans/web-gtm — the Google Tag Manager head snippet that declares Consent Mode defaults before the container loads, the noscript frame, and the script-side loader for a host that cannot edit its own HTML. Auto-invoked when adding a tag manager to a page, importing gtmHeadScript, gtmNoscriptFrame or loadGtm, or debugging why a tag ignores a stored consent decision.
|
|
4
|
+
user-invocable: false
|
|
5
|
+
---
|
|
6
|
+
<!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
|
|
7
|
+
|
|
8
|
+
# @owlmeans/web-gtm
|
|
9
|
+
|
|
10
|
+
**Layer:** Web
|
|
11
|
+
**Install:** `"@owlmeans/web-gtm": "^0.1.18-rc.20"` in `dependencies`
|
|
12
|
+
|
|
13
|
+
The tag-manager half of the consent set. It emits **strings**, not components, and it holds no
|
|
14
|
+
state — the decision lives in `@owlmeans/consent`, which this package reads through
|
|
15
|
+
`consentBootstrapScript` and `consentStore`.
|
|
16
|
+
|
|
17
|
+
## The one idea: order
|
|
18
|
+
|
|
19
|
+
Google Tag Manager decides what a tag may do from the consent state present **when the container
|
|
20
|
+
loads**. A React bundle cannot get there first: by the time an island mounts, `gtm.js` has been
|
|
21
|
+
running for hundreds of milliseconds and has already decided. A site whose defaults arrive after
|
|
22
|
+
the container is not configured differently — it is *unconfigured for the window that matters*, and
|
|
23
|
+
nothing in the page reports it.
|
|
24
|
+
|
|
25
|
+
So `gtmHeadScript` emits the consent bootstrap and then the container, in that order, as one
|
|
26
|
+
inline script. Everything else in this package exists to serve that.
|
|
27
|
+
|
|
28
|
+
## Key Exports
|
|
29
|
+
|
|
30
|
+
| Export | Description |
|
|
31
|
+
|--------|-------------|
|
|
32
|
+
| `gtmHeadScript(opts)` | The inline `<head>` snippet: consent defaults, stored decision, then the container |
|
|
33
|
+
| `gtmNoscriptFrame(opts)` | The hidden `<noscript>` iframe, for the top of `<body>` |
|
|
34
|
+
| `loadGtm(opts)` | Script-side container load, for a host that cannot emit into its own head |
|
|
35
|
+
| `GtmOptions` | `ConsentOptions` plus `id` (e.g. `GTM-XXXXXXX`) and optional `dataLayerName` |
|
|
36
|
+
|
|
37
|
+
## Stamping it
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { gtmHeadScript, gtmNoscriptFrame } from '@owlmeans/web-gtm'
|
|
41
|
+
|
|
42
|
+
const opts = { id: 'GTM-XXXXXXX' }
|
|
43
|
+
const head = gtmHeadScript(opts) // inline <script> content for <head>, first
|
|
44
|
+
const frame = gtmNoscriptFrame(opts) // <noscript> content for the top of <body>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The snippet is stamped **from HTML**, never from the application bundle. How each surface does it:
|
|
48
|
+
|
|
49
|
+
- a Vite app — a `transformIndexHtml` plugin substituting the two markers, so the snippet cannot
|
|
50
|
+
drift from the package;
|
|
51
|
+
- an Astro site — `owlHeadScripts()` from `@owlmeans/astro`, `set:html` in the base layout.
|
|
52
|
+
|
|
53
|
+
A surface that runs no container of its own stamps only `consentBootstrapScript()` from
|
|
54
|
+
`@owlmeans/consent` — that is the case for the bundler-generated `index.html` of a generated
|
|
55
|
+
application, which carries the consent bootstrap and no tag manager. The bootstrap and this
|
|
56
|
+
snippet are separate stampings: adding a container means adding `gtmHeadScript` /
|
|
57
|
+
`gtmNoscriptFrame` beside it.
|
|
58
|
+
|
|
59
|
+
`gtmHeadScript` also **reads the stored decision** and pushes `consent/update` before the
|
|
60
|
+
container, so a returning visitor's tags are not denied for the first paint of every page. That
|
|
61
|
+
read is why the same `ConsentOptions` (categories, `storageKey`) must be passed here and to the
|
|
62
|
+
dialog: two different category sets in one page mean the snippet and the component disagree about
|
|
63
|
+
what was asked.
|
|
64
|
+
|
|
65
|
+
## Gotchas
|
|
66
|
+
|
|
67
|
+
- **The container id and queue name are injected as data, never interpolated raw.** Both come from
|
|
68
|
+
configuration, and configuration reaches these functions as a string. `gtmHeadScript` JSON-encodes
|
|
69
|
+
them, so a quote inside an id is escaped rather than closing the literal and turning the rest into
|
|
70
|
+
executable script; `gtmNoscriptFrame` URL-encodes the id so it cannot break out of the attribute.
|
|
71
|
+
Never build either string by hand.
|
|
72
|
+
- **`dataLayerName` is for a page running more than one container.** It is passed through to the
|
|
73
|
+
container the way Google's own snippet does (`&l=` when it is not `dataLayer`); leave it unset
|
|
74
|
+
otherwise, or tags looking at the default queue see nothing.
|
|
75
|
+
- **`loadGtm` is the fallback, not the default.** It is for a single-page app whose HTML is not
|
|
76
|
+
yours to edit. It still pushes the defaults first (through `consentStore.init`) and refuses to
|
|
77
|
+
load a second time — it marks its own `<script>` element with an id derived from the container —
|
|
78
|
+
but it runs after the bundle, which is exactly the window the head snippet closes. Prefer the
|
|
79
|
+
head snippet wherever the document can be edited.
|
|
80
|
+
- **A second `consent/default` after a tag has loaded can WIDEN what was already narrowed**, so the
|
|
81
|
+
bootstrap is idempotent through a window flag. A page may therefore carry the call twice — once
|
|
82
|
+
inline, once from the bundle that mounts the dialog — without harm. Do not defeat the flag.
|
|
83
|
+
- `loadGtm` returns immediately when there is no `document`, so it is safe to call from code that
|
|
84
|
+
also runs during server rendering.
|
|
85
|
+
|
|
86
|
+
## Depends On
|
|
87
|
+
|
|
88
|
+
- `@owlmeans/consent` — `consentBootstrapScript`, `consentStore`, `ConsentOptions`, the category
|
|
89
|
+
model and the Consent Mode signal mapping
|
|
90
|
+
|
|
91
|
+
## Related
|
|
92
|
+
|
|
93
|
+
- `consent` — categories, the storage record and its migration, Consent Mode v2 signalling, and the
|
|
94
|
+
ordering rule this package implements
|
|
95
|
+
- `web-consent` — the dialog and the cookie-policy page that produce the decision
|
|
96
|
+
- `astro` — `owlHeadScripts()`, which composes both strings for an Astro layout
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/web-gtm",
|
|
3
|
-
"version": "0.1.18-rc.
|
|
3
|
+
"version": "0.1.18-rc.20",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@owlmeans/consent": "^0.1.18-rc.
|
|
25
|
+
"@owlmeans/consent": "^0.1.18-rc.4"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@owlmeans/dep-config": "workspace:*",
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { CONSENT_KEY } from '@owlmeans/consent'
|
|
3
|
+
import { gtmHeadScript, gtmNoscriptFrame } from '../src/index.js'
|
|
4
|
+
|
|
5
|
+
const ID = 'GTM-TESTID1'
|
|
6
|
+
|
|
7
|
+
describe('@owlmeans/web-gtm — the head snippet', () => {
|
|
8
|
+
test('the consent defaults are declared BEFORE the container is requested', async () => {
|
|
9
|
+
// The entire reason this package exists. Consent Mode decides what a tag may do from the state
|
|
10
|
+
// present when the container loads, so a `consent/default` that arrives after `gtm.js` leaves
|
|
11
|
+
// the page unconfigured for the window that matters — and nothing reports it. Asserted on
|
|
12
|
+
// offsets rather than on presence, because both strings are always present and only the order
|
|
13
|
+
// has ever been wrong.
|
|
14
|
+
const script = gtmHeadScript({ id: ID })
|
|
15
|
+
|
|
16
|
+
const defaults = script.indexOf("'consent','default'")
|
|
17
|
+
const container = script.indexOf('gtm.js')
|
|
18
|
+
|
|
19
|
+
expect(defaults).toBeGreaterThanOrEqual(0)
|
|
20
|
+
expect(container).toBeGreaterThanOrEqual(0)
|
|
21
|
+
expect(defaults).toBeLessThan(container)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('it reads a stored decision, so a returning visitor is not treated as denied', async () => {
|
|
25
|
+
const script = gtmHeadScript({ id: ID })
|
|
26
|
+
|
|
27
|
+
expect(script).toContain(JSON.stringify(CONSENT_KEY))
|
|
28
|
+
expect(script).toContain("'consent','update'")
|
|
29
|
+
// And the read happens before the container too — a decision applied afterwards is a decision
|
|
30
|
+
// the first tag never saw.
|
|
31
|
+
expect(script.indexOf("'consent','update'")).toBeLessThan(script.indexOf('gtm.js'))
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('the container id and queue name are injected as data, never interpolated raw', async () => {
|
|
35
|
+
// These come from configuration, and configuration reaches this function as a string. JSON
|
|
36
|
+
// encoding is what keeps a stray quote from closing the literal and turning the rest of an id
|
|
37
|
+
// into executable script — the value still appears verbatim, and that is fine; what matters is
|
|
38
|
+
// that a quote inside it is ESCAPED rather than terminating the string.
|
|
39
|
+
const script = gtmHeadScript({ id: `${ID}"+alert(1)+"`, dataLayerName: 'owlLayer' })
|
|
40
|
+
|
|
41
|
+
expect(script).toContain(JSON.stringify(`${ID}"+alert(1)+"`))
|
|
42
|
+
expect(script).toContain(JSON.stringify('owlLayer'))
|
|
43
|
+
// The escaped form is what makes it inert; the bare form would have closed the literal.
|
|
44
|
+
expect(script).toContain('\\"+alert(1)+\\"')
|
|
45
|
+
expect(script).not.toContain(`"${ID}"+alert(1)+""`)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('a custom queue name is passed to the container', async () => {
|
|
49
|
+
expect(gtmHeadScript({ id: ID, dataLayerName: 'owlLayer' })).toContain("l!='dataLayer'")
|
|
50
|
+
expect(gtmHeadScript({ id: ID })).toContain(JSON.stringify('dataLayer'))
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('@owlmeans/web-gtm — the noscript frame', () => {
|
|
55
|
+
test('it names the container and stays invisible', async () => {
|
|
56
|
+
const frame = gtmNoscriptFrame({ id: ID })
|
|
57
|
+
|
|
58
|
+
expect(frame).toContain(`id=${ID}`)
|
|
59
|
+
expect(frame).toContain('display:none')
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('an id is URL-encoded, so it cannot break out of the attribute', async () => {
|
|
63
|
+
expect(gtmNoscriptFrame({ id: 'a"b' })).not.toContain('a"b')
|
|
64
|
+
})
|
|
65
|
+
})
|
package/build/.gitkeep
DELETED
|
File without changes
|