@owlmeans/web-log 0.1.18-rc.1 → 0.1.18-rc.2
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 +1 -1
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/web-log/SKILL.md +2 -2
- package/build/consented.d.ts +11 -0
- package/build/consented.d.ts.map +1 -0
- package/build/consented.js +32 -0
- package/build/consented.js.map +1 -0
- package/build/gtm.d.ts +12 -0
- package/build/gtm.d.ts.map +1 -0
- package/build/gtm.js +18 -0
- package/build/gtm.js.map +1 -0
- package/build/index.d.ts +3 -20
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -47
- package/build/index.js.map +1 -1
- package/package.json +3 -3
- package/src/consented.ts +37 -0
- package/src/gtm.ts +21 -0
- package/src/index.ts +3 -56
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
|
|
|
6
6
|
your project's skill store (`.agents/skills/`):
|
|
7
7
|
|
|
8
8
|
```sh
|
|
9
|
-
npx @owlmeans/agent-skills@^0.1.18-rc.
|
|
9
|
+
npx @owlmeans/agent-skills@^0.1.18-rc.49
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
The embedded files are version-matched to this package release. Do not edit them
|
package/agent-meta/manifest.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 2,
|
|
3
3
|
"package": "@owlmeans/web-log",
|
|
4
|
-
"version": "0.1.18-rc.
|
|
5
|
-
"generatedAt": "2026-10-
|
|
4
|
+
"version": "0.1.18-rc.2",
|
|
5
|
+
"generatedAt": "2026-10-05T21:03:06.397Z",
|
|
6
6
|
"canonicalRepo": "https://github.com/owlmeans/common",
|
|
7
7
|
"entries": [
|
|
8
8
|
{
|
|
@@ -8,7 +8,7 @@ user-invocable: false
|
|
|
8
8
|
# @owlmeans/web-log
|
|
9
9
|
|
|
10
10
|
**Layer:** Web (depends on `@owlmeans/log` and `@owlmeans/consent`)
|
|
11
|
-
**Install:** `"@owlmeans/web-log": "^0.1.18-rc.
|
|
11
|
+
**Install:** `"@owlmeans/web-log": "^0.1.18-rc.2"` in `dependencies`
|
|
12
12
|
|
|
13
13
|
The browser half of "analytics are plugins" — see `/log` for the call parameters. A page logs a
|
|
14
14
|
business event once; which systems hear it is the set of plugins registered.
|
|
@@ -25,7 +25,7 @@ logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.create
|
|
|
25
25
|
|
|
26
26
|
## Consent
|
|
27
27
|
|
|
28
|
-
`consentedAnalyticsPlugin(send, options)` sends only while `
|
|
28
|
+
`consentedAnalyticsPlugin(send, options)` sends only while `consentStore.granted(options.category ?? 'analytics')`
|
|
29
29
|
(`@owlmeans/consent`). **An event without consent is dropped, never queued**: a tag manager replays
|
|
30
30
|
everything already waiting in its queue when it loads, so a held event would be sent after a later
|
|
31
31
|
grant — in a window the visitor never agreed to be measured in. The load of the tag itself is gated
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LogPlugin } from '@owlmeans/log';
|
|
2
|
+
import type { ConsentedAnalyticsOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* An analytics plugin that sends only while a consent category is granted.
|
|
5
|
+
*
|
|
6
|
+
* An event arriving without consent is DROPPED, never queued: a tag manager replays everything
|
|
7
|
+
* already waiting in its queue when it loads, so a held event would be sent after a later grant —
|
|
8
|
+
* for a moment the visitor never agreed to be measured in.
|
|
9
|
+
*/
|
|
10
|
+
export declare const consentedAnalyticsPlugin: (send: (payload: Record<string, unknown>) => void, options?: ConsentedAnalyticsOptions) => LogPlugin;
|
|
11
|
+
//# sourceMappingURL=consented.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consented.d.ts","sourceRoot":"","sources":["../src/consented.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkB,SAAS,EAAE,MAAM,eAAe,CAAA;AAC9D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;AAW3D;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,SAC7B,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,YAAW,yBAAyB,KACnF,SAcD,CAAA"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CONSENT_ANALYTICS, consentStore } from '@owlmeans/consent';
|
|
2
|
+
/** What the plugin sends by default: the event name and its flat data. */
|
|
3
|
+
const defaultPayload = (event) => {
|
|
4
|
+
const data = event.data;
|
|
5
|
+
const flat = data != null && typeof data === 'object' && !Array.isArray(data)
|
|
6
|
+
? data
|
|
7
|
+
: data !== undefined ? { value: data } : {};
|
|
8
|
+
return { event: event.event, ...flat };
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* An analytics plugin that sends only while a consent category is granted.
|
|
12
|
+
*
|
|
13
|
+
* An event arriving without consent is DROPPED, never queued: a tag manager replays everything
|
|
14
|
+
* already waiting in its queue when it loads, so a held event would be sent after a later grant —
|
|
15
|
+
* for a moment the visitor never agreed to be measured in.
|
|
16
|
+
*/
|
|
17
|
+
export const consentedAnalyticsPlugin = (send, options = {}) => ({
|
|
18
|
+
name: options.name ?? 'web-analytics',
|
|
19
|
+
track: event => {
|
|
20
|
+
if (options.allow != null && !options.allow.includes(event.event)) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!consentStore.granted(options.category ?? CONSENT_ANALYTICS)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const payload = (options.map ?? defaultPayload)(event);
|
|
27
|
+
if (payload != null) {
|
|
28
|
+
send(payload);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=consented.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consented.js","sourceRoot":"","sources":["../src/consented.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAInE,0EAA0E;AAC1E,MAAM,cAAc,GAAG,CAAC,KAAqB,EAA2B,EAAE;IACxE,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAA;IACvB,MAAM,IAAI,GAAG,IAAI,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QAC3E,CAAC,CAAC,IAA+B;QACjC,CAAC,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;IAC7C,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,GAAG,IAAI,EAAE,CAAA;AACxC,CAAC,CAAA;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,IAAgD,EAAE,OAAO,GAA8B,EAAE,EAC9E,EAAE,CAAC,CAAC;IACf,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,eAAe;IACrC,KAAK,EAAE,KAAK,CAAC,EAAE;QACb,IAAI,OAAO,CAAC,KAAK,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YAClE,OAAM;QACR,CAAC;QACD,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,IAAI,iBAAiB,CAAC,EAAE,CAAC;YACjE,OAAM;QACR,CAAC;QACD,MAAM,OAAO,GAAG,CAAC,OAAO,CAAC,GAAG,IAAI,cAAc,CAAC,CAAC,KAAK,CAAC,CAAA;QACtD,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;YACpB,IAAI,CAAC,OAAO,CAAC,CAAA;QACf,CAAC;IACH,CAAC;CACF,CAAC,CAAA"}
|
package/build/gtm.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { LogPlugin } from '@owlmeans/log';
|
|
2
|
+
import type { ConsentedAnalyticsOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The tag-manager sink: pushes `{ event, ...data }` onto `window.dataLayer`, consent permitting.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created'] }))
|
|
8
|
+
* logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const gtmAnalyticsPlugin: (options?: ConsentedAnalyticsOptions) => LogPlugin;
|
|
12
|
+
//# sourceMappingURL=gtm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtm.d.ts","sourceRoot":"","sources":["../src/gtm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAE9C,OAAO,KAAK,EAAE,yBAAyB,EAAmB,MAAM,YAAY,CAAA;AAE5E;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,aAAa,yBAAyB,KAAQ,SAQ5C,CAAA"}
|
package/build/gtm.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { consentedAnalyticsPlugin } from './consented.js';
|
|
2
|
+
/**
|
|
3
|
+
* The tag-manager sink: pushes `{ event, ...data }` onto `window.dataLayer`, consent permitting.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created'] }))
|
|
7
|
+
* logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export const gtmAnalyticsPlugin = (options = {}) => consentedAnalyticsPlugin(payload => {
|
|
11
|
+
if (typeof window === 'undefined') {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const win = window;
|
|
15
|
+
win.dataLayer = win.dataLayer ?? [];
|
|
16
|
+
win.dataLayer.push(payload);
|
|
17
|
+
}, { name: 'gtm', ...options });
|
|
18
|
+
//# sourceMappingURL=gtm.js.map
|
package/build/gtm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gtm.js","sourceRoot":"","sources":["../src/gtm.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAGzD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,OAAO,GAA8B,EAAE,EAAa,EAAE,CACvF,wBAAwB,CAAC,OAAO,CAAC,EAAE;IACjC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,OAAM;IACR,CAAC;IACD,MAAM,GAAG,GAAG,MAAoC,CAAA;IAChD,GAAG,CAAC,SAAS,GAAG,GAAG,CAAC,SAAS,IAAI,EAAE,CAAA;IACnC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AAC7B,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* An analytics plugin that sends only while a consent category is granted.
|
|
5
|
-
*
|
|
6
|
-
* An event arriving without consent is DROPPED, never queued: a tag manager replays everything
|
|
7
|
-
* already waiting in its queue when it loads, so a held event would be sent after a later grant —
|
|
8
|
-
* for a moment the visitor never agreed to be measured in.
|
|
9
|
-
*/
|
|
10
|
-
export declare const consentedAnalyticsPlugin: (send: (payload: Record<string, unknown>) => void, options?: ConsentedAnalyticsOptions) => LogPlugin;
|
|
11
|
-
/**
|
|
12
|
-
* The tag-manager sink: pushes `{ event, ...data }` onto `window.dataLayer`, consent permitting.
|
|
13
|
-
*
|
|
14
|
-
* ```ts
|
|
15
|
-
* addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created'] }))
|
|
16
|
-
* logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export declare const gtmAnalyticsPlugin: (options?: ConsentedAnalyticsOptions) => LogPlugin;
|
|
20
|
-
export type { ConsentedAnalyticsOptions, DataLayerWindow } from './types.js';
|
|
1
|
+
export type * from './types.js';
|
|
2
|
+
export * from './consented.js';
|
|
3
|
+
export * from './gtm.js';
|
|
21
4
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA"}
|
package/build/index.js
CHANGED
|
@@ -1,48 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const defaultPayload = (event) => {
|
|
4
|
-
const data = event.data;
|
|
5
|
-
const flat = data != null && typeof data === 'object' && !Array.isArray(data)
|
|
6
|
-
? data
|
|
7
|
-
: data !== undefined ? { value: data } : {};
|
|
8
|
-
return { event: event.event, ...flat };
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* An analytics plugin that sends only while a consent category is granted.
|
|
12
|
-
*
|
|
13
|
-
* An event arriving without consent is DROPPED, never queued: a tag manager replays everything
|
|
14
|
-
* already waiting in its queue when it loads, so a held event would be sent after a later grant —
|
|
15
|
-
* for a moment the visitor never agreed to be measured in.
|
|
16
|
-
*/
|
|
17
|
-
export const consentedAnalyticsPlugin = (send, options = {}) => ({
|
|
18
|
-
name: options.name ?? 'web-analytics',
|
|
19
|
-
track: event => {
|
|
20
|
-
if (options.allow != null && !options.allow.includes(event.event)) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (!isConsented(options.category ?? CONSENT_ANALYTICS)) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const payload = (options.map ?? defaultPayload)(event);
|
|
27
|
-
if (payload != null) {
|
|
28
|
-
send(payload);
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
/**
|
|
33
|
-
* The tag-manager sink: pushes `{ event, ...data }` onto `window.dataLayer`, consent permitting.
|
|
34
|
-
*
|
|
35
|
-
* ```ts
|
|
36
|
-
* addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created'] }))
|
|
37
|
-
* logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export const gtmAnalyticsPlugin = (options = {}) => consentedAnalyticsPlugin(payload => {
|
|
41
|
-
if (typeof window === 'undefined') {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const win = window;
|
|
45
|
-
win.dataLayer = win.dataLayer ?? [];
|
|
46
|
-
win.dataLayer.push(payload);
|
|
47
|
-
}, { name: 'gtm', ...options });
|
|
1
|
+
export * from './consented.js';
|
|
2
|
+
export * from './gtm.js';
|
|
48
3
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/web-log",
|
|
3
|
-
"version": "0.1.18-rc.
|
|
3
|
+
"version": "0.1.18-rc.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@owlmeans/consent": "^0.1.18-rc.
|
|
26
|
-
"@owlmeans/log": "^0.1.18-rc.
|
|
25
|
+
"@owlmeans/consent": "^0.1.18-rc.11",
|
|
26
|
+
"@owlmeans/log": "^0.1.18-rc.2"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@owlmeans/dep-config": "workspace:*",
|
package/src/consented.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CONSENT_ANALYTICS, consentStore } from '@owlmeans/consent'
|
|
2
|
+
import type { AnalyticsEvent, LogPlugin } from '@owlmeans/log'
|
|
3
|
+
import type { ConsentedAnalyticsOptions } from './types.js'
|
|
4
|
+
|
|
5
|
+
/** What the plugin sends by default: the event name and its flat data. */
|
|
6
|
+
const defaultPayload = (event: AnalyticsEvent): Record<string, unknown> => {
|
|
7
|
+
const data = event.data
|
|
8
|
+
const flat = data != null && typeof data === 'object' && !Array.isArray(data)
|
|
9
|
+
? data as Record<string, unknown>
|
|
10
|
+
: data !== undefined ? { value: data } : {}
|
|
11
|
+
return { event: event.event, ...flat }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An analytics plugin that sends only while a consent category is granted.
|
|
16
|
+
*
|
|
17
|
+
* An event arriving without consent is DROPPED, never queued: a tag manager replays everything
|
|
18
|
+
* already waiting in its queue when it loads, so a held event would be sent after a later grant —
|
|
19
|
+
* for a moment the visitor never agreed to be measured in.
|
|
20
|
+
*/
|
|
21
|
+
export const consentedAnalyticsPlugin = (
|
|
22
|
+
send: (payload: Record<string, unknown>) => void, options: ConsentedAnalyticsOptions = {}
|
|
23
|
+
): LogPlugin => ({
|
|
24
|
+
name: options.name ?? 'web-analytics',
|
|
25
|
+
track: event => {
|
|
26
|
+
if (options.allow != null && !options.allow.includes(event.event)) {
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
if (!consentStore.granted(options.category ?? CONSENT_ANALYTICS)) {
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
const payload = (options.map ?? defaultPayload)(event)
|
|
33
|
+
if (payload != null) {
|
|
34
|
+
send(payload)
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
})
|
package/src/gtm.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LogPlugin } from '@owlmeans/log'
|
|
2
|
+
import { consentedAnalyticsPlugin } from './consented.js'
|
|
3
|
+
import type { ConsentedAnalyticsOptions, DataLayerWindow } from './types.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The tag-manager sink: pushes `{ event, ...data }` onto `window.dataLayer`, consent permitting.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created'] }))
|
|
10
|
+
* logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export const gtmAnalyticsPlugin = (options: ConsentedAnalyticsOptions = {}): LogPlugin =>
|
|
14
|
+
consentedAnalyticsPlugin(payload => {
|
|
15
|
+
if (typeof window === 'undefined') {
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
const win = window as unknown as DataLayerWindow
|
|
19
|
+
win.dataLayer = win.dataLayer ?? []
|
|
20
|
+
win.dataLayer.push(payload)
|
|
21
|
+
}, { name: 'gtm', ...options })
|
package/src/index.ts
CHANGED
|
@@ -1,57 +1,4 @@
|
|
|
1
|
-
import { CONSENT_ANALYTICS, isConsented } from '@owlmeans/consent'
|
|
2
|
-
import type { AnalyticsEvent, LogPlugin } from '@owlmeans/log'
|
|
3
|
-
import type { ConsentedAnalyticsOptions, DataLayerWindow } from './types.js'
|
|
4
1
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const flat = data != null && typeof data === 'object' && !Array.isArray(data)
|
|
9
|
-
? data as Record<string, unknown>
|
|
10
|
-
: data !== undefined ? { value: data } : {}
|
|
11
|
-
return { event: event.event, ...flat }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* An analytics plugin that sends only while a consent category is granted.
|
|
16
|
-
*
|
|
17
|
-
* An event arriving without consent is DROPPED, never queued: a tag manager replays everything
|
|
18
|
-
* already waiting in its queue when it loads, so a held event would be sent after a later grant —
|
|
19
|
-
* for a moment the visitor never agreed to be measured in.
|
|
20
|
-
*/
|
|
21
|
-
export const consentedAnalyticsPlugin = (
|
|
22
|
-
send: (payload: Record<string, unknown>) => void, options: ConsentedAnalyticsOptions = {}
|
|
23
|
-
): LogPlugin => ({
|
|
24
|
-
name: options.name ?? 'web-analytics',
|
|
25
|
-
track: event => {
|
|
26
|
-
if (options.allow != null && !options.allow.includes(event.event)) {
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
if (!isConsented(options.category ?? CONSENT_ANALYTICS)) {
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
const payload = (options.map ?? defaultPayload)(event)
|
|
33
|
-
if (payload != null) {
|
|
34
|
-
send(payload)
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The tag-manager sink: pushes `{ event, ...data }` onto `window.dataLayer`, consent permitting.
|
|
41
|
-
*
|
|
42
|
-
* ```ts
|
|
43
|
-
* addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created'] }))
|
|
44
|
-
* logger('projects').info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export const gtmAnalyticsPlugin = (options: ConsentedAnalyticsOptions = {}): LogPlugin =>
|
|
48
|
-
consentedAnalyticsPlugin(payload => {
|
|
49
|
-
if (typeof window === 'undefined') {
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
const win = window as unknown as DataLayerWindow
|
|
53
|
-
win.dataLayer = win.dataLayer ?? []
|
|
54
|
-
win.dataLayer.push(payload)
|
|
55
|
-
}, { name: 'gtm', ...options })
|
|
56
|
-
|
|
57
|
-
export type { ConsentedAnalyticsOptions, DataLayerWindow } from './types.js'
|
|
2
|
+
export type * from './types.js'
|
|
3
|
+
export * from './consented.js'
|
|
4
|
+
export * from './gtm.js'
|