@owlmeans/web-log 0.1.18-rc.1 → 0.1.18-rc.3
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 +131 -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
|
@@ -1,3 +1,133 @@
|
|
|
1
|
+
# @owlmeans/web-log
|
|
2
|
+
|
|
3
|
+
Consent-gated analytics plugins for `@owlmeans/log` in the browser. Application code logs a business
|
|
4
|
+
event once with `{ analytics: 'event.name' }`; the plugins registered here decide where it goes —
|
|
5
|
+
`gtmAnalyticsPlugin` pushes it onto `window.dataLayer` for a tag manager, `consentedAnalyticsPlugin`
|
|
6
|
+
hands it to any sender you supply — and both send only while the visitor has granted the analytics
|
|
7
|
+
consent category. A web app uses it whenever it counts events in GTM or another analytics system.
|
|
8
|
+
It does not load the tag manager script (that is `@owlmeans/web-gtm`), does not record consent (that
|
|
9
|
+
is `@owlmeans/consent` and the `@owlmeans/web-consent` UI) and is not a logger: the calls themselves
|
|
10
|
+
are `@owlmeans/log`. The `web-log` skill covers this package; the `logging` skill sets the policy for
|
|
11
|
+
log lines and analytics events.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bun add @owlmeans/web-log@^0.1.18-rc.3
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Concepts
|
|
20
|
+
|
|
21
|
+
- **Analytics as a call option** — `log.info(message, data, { analytics })` from `@owlmeans/log`
|
|
22
|
+
delivers an `AnalyticsEvent` to every registered plugin's `track`. A string names the event; with
|
|
23
|
+
`analytics: true` the name is `options.event`, else the message.
|
|
24
|
+
- **Consent gate** — a plugin built here checks `consentStore.granted(category)` (default
|
|
25
|
+
`CONSENT_ANALYTICS`, `'analytics'`) on every event.
|
|
26
|
+
- **Drop, never queue** — an event that arrives without consent is discarded. A tag manager replays
|
|
27
|
+
its queue when it loads, so a held event would be sent for a moment the visitor never agreed to.
|
|
28
|
+
- **Payload** — by default `{ event, ...data }`; a non-object `data` becomes `{ value }`. `map`
|
|
29
|
+
replaces the shape per plugin.
|
|
30
|
+
- **One plugin per sink** — each analytics system is its own named plugin with its own allow-list.
|
|
31
|
+
Registering a plugin with an existing name replaces the earlier one.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### 1. Register the tag-manager sink at startup
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { addLogPlugin } from '@owlmeans/log'
|
|
39
|
+
import { gtmAnalyticsPlugin } from '@owlmeans/web-log'
|
|
40
|
+
|
|
41
|
+
addLogPlugin(gtmAnalyticsPlugin({ allow: ['project.created', 'sign_in'] }))
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Emit an event from application code
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { logger } from '@owlmeans/log'
|
|
48
|
+
|
|
49
|
+
const log = logger('projects')
|
|
50
|
+
|
|
51
|
+
log.info('Created', { kind: 'web' }, { analytics: 'project.created' })
|
|
52
|
+
// with analytics consent: window.dataLayer.push({ event: 'project.created', kind: 'web' })
|
|
53
|
+
// without it: nothing is pushed, now or after a later grant
|
|
54
|
+
|
|
55
|
+
// count it without writing a console line
|
|
56
|
+
log.info('Signed in', undefined, { analytics: 'sign_in', console: false })
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
A call without `analytics` never reaches the plugins' `track`, whatever its level.
|
|
60
|
+
|
|
61
|
+
### 3. Add a second analytics system
|
|
62
|
+
|
|
63
|
+
`consentedAnalyticsPlugin` is the same gate over any sender. Give it a distinct `name` so it does not
|
|
64
|
+
replace the GTM plugin.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { addLogPlugin } from '@owlmeans/log'
|
|
68
|
+
import { consentedAnalyticsPlugin } from '@owlmeans/web-log'
|
|
69
|
+
|
|
70
|
+
addLogPlugin(consentedAnalyticsPlugin(
|
|
71
|
+
payload => navigator.sendBeacon('/metrics', JSON.stringify(payload)),
|
|
72
|
+
{
|
|
73
|
+
name: 'beacon',
|
|
74
|
+
allow: ['subscription.started'],
|
|
75
|
+
map: event => ({ name: event.event, scope: event.scope, at: event.time }),
|
|
76
|
+
}
|
|
77
|
+
))
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 4. Gate on a different consent category
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { CONSENT_MARKETING } from '@owlmeans/consent'
|
|
84
|
+
import { gtmAnalyticsPlugin } from '@owlmeans/web-log'
|
|
85
|
+
|
|
86
|
+
addLogPlugin(gtmAnalyticsPlugin({ name: 'gtm-ads', category: CONSENT_MARKETING, allow: ['purchase'] }))
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API
|
|
90
|
+
|
|
91
|
+
| Symbol | Kind | Purpose |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `gtmAnalyticsPlugin(options?)` | function → `LogPlugin` | Consent-gated sink pushing onto `window.dataLayer` (created when missing); name `'gtm'` unless overridden; no-op outside a browser |
|
|
94
|
+
| `consentedAnalyticsPlugin(send, options?)` | function → `LogPlugin` | Consent-gated sink calling `send(payload)`; name `'web-analytics'` unless overridden |
|
|
95
|
+
| `ConsentedAnalyticsOptions` | type | Options of both factories (below) |
|
|
96
|
+
| `DataLayerWindow` | type | `{ dataLayer?: unknown[] }` — the window shape the GTM sink writes to |
|
|
97
|
+
|
|
98
|
+
### `ConsentedAnalyticsOptions`
|
|
99
|
+
|
|
100
|
+
| Field | Default | Meaning |
|
|
101
|
+
|---|---|---|
|
|
102
|
+
| `name` | `'gtm'` / `'web-analytics'` | Plugin name; a second plugin of the same name replaces the first |
|
|
103
|
+
| `category` | `CONSENT_ANALYTICS` | Consent category that must be granted at send time |
|
|
104
|
+
| `allow` | every event | Event names this plugin sends |
|
|
105
|
+
| `map(event)` | `{ event, ...data }` | Reshape an `AnalyticsEvent`; return `undefined` to drop it |
|
|
106
|
+
|
|
107
|
+
The order of checks per event is: allow-list, consent, `map`, send.
|
|
108
|
+
|
|
109
|
+
## Common pitfalls
|
|
110
|
+
|
|
111
|
+
- Do not call `window.dataLayer.push` from components — log the event and let the plugin apply the
|
|
112
|
+
consent gate.
|
|
113
|
+
- Plugins only implement `track`: a log line without the `analytics` option is never sent. The log
|
|
114
|
+
level does not filter analytics, and `console: false` only silences the console sink.
|
|
115
|
+
- Events emitted before consent is granted are lost by design; do not add your own buffer in `send`.
|
|
116
|
+
- Two `gtmAnalyticsPlugin` calls with the default name leave only the last one registered — pass
|
|
117
|
+
`name` for each extra plugin.
|
|
118
|
+
- Never put personal content into analytics `data` — `@owlmeans/log` redacts known secret keys, but
|
|
119
|
+
user text and e-mail addresses are the caller's responsibility.
|
|
120
|
+
- Loading the GTM script is separate: this package only feeds `dataLayer`; `@owlmeans/web-gtm` loads
|
|
121
|
+
the tag and signals consent mode.
|
|
122
|
+
|
|
123
|
+
## Related packages
|
|
124
|
+
|
|
125
|
+
- [`@owlmeans/log`](../log) — `logger`, `addLogPlugin`, `LogPlugin`, `AnalyticsEvent`
|
|
126
|
+
- [`@owlmeans/consent`](../consent) — `consentStore` and the consent category constants
|
|
127
|
+
- [`@owlmeans/web-gtm`](../web-gtm) — loads the tag manager with consent mode
|
|
128
|
+
- [`@owlmeans/web-consent`](../web-consent) — the consent banner and preferences UI
|
|
129
|
+
- [`@owlmeans/viable-log`](../viable-log) — the analytics plugin for apps generated by viable
|
|
130
|
+
|
|
1
131
|
<!-- owlmeans:agent-guidance:start -->
|
|
2
132
|
## Agent guidance
|
|
3
133
|
|
|
@@ -6,7 +136,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
|
|
|
6
136
|
your project's skill store (`.agents/skills/`):
|
|
7
137
|
|
|
8
138
|
```sh
|
|
9
|
-
npx @owlmeans/agent-skills@^0.1.18-rc.
|
|
139
|
+
npx @owlmeans/agent-skills@^0.1.18-rc.52
|
|
10
140
|
```
|
|
11
141
|
|
|
12
142
|
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.3",
|
|
5
|
+
"generatedAt": "2026-10-08T21:27:54.303Z",
|
|
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.3"` 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.3",
|
|
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.12",
|
|
26
|
+
"@owlmeans/log": "^0.1.18-rc.3"
|
|
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'
|