@buildinams/tracking 0.0.0-stage → 0.2.0
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 +267 -2
- package/dist/chunk-KLN5K6PR.js +6 -0
- package/dist/chunk-KLN5K6PR.js.map +1 -0
- package/dist/compatibility-Dc1JmTeG.d.ts +35 -0
- package/dist/consent/cookiebot/index.d.ts +5 -0
- package/dist/consent/cookiebot/index.js +27 -0
- package/dist/consent/cookiebot/index.js.map +1 -0
- package/dist/consent/index.d.ts +18 -0
- package/dist/consent/index.js +3 -0
- package/dist/consent/index.js.map +1 -0
- package/dist/consent/onetrust/index.d.ts +9 -0
- package/dist/consent/onetrust/index.js +35 -0
- package/dist/consent/onetrust/index.js.map +1 -0
- package/dist/consent/shopify/index.d.ts +13 -0
- package/dist/consent/shopify/index.js +93 -0
- package/dist/consent/shopify/index.js.map +1 -0
- package/dist/destinations/ga4/index.d.ts +16 -0
- package/dist/destinations/ga4/index.js +15 -0
- package/dist/destinations/ga4/index.js.map +1 -0
- package/dist/destinations/index.d.ts +9 -0
- package/dist/destinations/index.js +17 -0
- package/dist/destinations/index.js.map +1 -0
- package/dist/destinations/shopify/index.d.ts +12 -0
- package/dist/destinations/shopify/index.js +18 -0
- package/dist/destinations/shopify/index.js.map +1 -0
- package/dist/events/index.d.ts +16 -0
- package/dist/events/index.js +77 -0
- package/dist/events/index.js.map +1 -0
- package/dist/flow/index.d.ts +16 -0
- package/dist/flow/index.js +82 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/page/index.d.ts +27 -0
- package/dist/page/index.js +56 -0
- package/dist/page/index.js.map +1 -0
- package/dist/sources/cart/index.d.ts +27 -0
- package/dist/sources/cart/index.js +38 -0
- package/dist/sources/cart/index.js.map +1 -0
- package/dist/sources/cart/shopify/index.d.ts +28 -0
- package/dist/sources/cart/shopify/index.js +16 -0
- package/dist/sources/cart/shopify/index.js.map +1 -0
- package/dist/sources/dom/index.d.ts +202 -0
- package/dist/sources/dom/index.js +597 -0
- package/dist/sources/dom/index.js.map +1 -0
- package/dist/sources/index.d.ts +6 -0
- package/dist/sources/index.js +3 -0
- package/dist/sources/index.js.map +1 -0
- package/dist/types-BfJsv8AV.d.ts +30 -0
- package/dist/types-DfPz7mBl.d.ts +49 -0
- package/docs/decisions/001-vendor-neutral-event-pipeline.md +33 -0
- package/docs/events.md +187 -0
- package/package.json +141 -5
package/README.md
CHANGED
|
@@ -1,3 +1,268 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @buildinams/tracking
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vendor-neutral storefront events: Sources publish them to a Broker, and each Destination receives them through its own Mapping, behind consent. Behavior rules and glossary: [docs/events.md](docs/events.md).
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import { cookiebot } from "@buildinams/tracking/consent/cookiebot";
|
|
7
|
+
import { shopifyCustomerPrivacy } from "@buildinams/tracking/consent/shopify";
|
|
8
|
+
import {
|
|
9
|
+
createDestinations,
|
|
10
|
+
map,
|
|
11
|
+
withConsent,
|
|
12
|
+
} from "@buildinams/tracking/destinations";
|
|
13
|
+
import { ga4 } from "@buildinams/tracking/destinations/ga4";
|
|
14
|
+
import { shopify } from "@buildinams/tracking/destinations/shopify";
|
|
15
|
+
import { createEventBroker, shape } from "@buildinams/tracking/events";
|
|
16
|
+
import { startTracking } from "@buildinams/tracking/flow";
|
|
17
|
+
import { createCurrentPage } from "@buildinams/tracking/page";
|
|
18
|
+
import { createCartSource } from "@buildinams/tracking/sources/cart";
|
|
19
|
+
import { shopifyCart } from "@buildinams/tracking/sources/cart/shopify";
|
|
20
|
+
import type {
|
|
21
|
+
ButtonClickData,
|
|
22
|
+
FormEventData,
|
|
23
|
+
FormSubmitData,
|
|
24
|
+
ItemSelectData,
|
|
25
|
+
LinkClickData,
|
|
26
|
+
ListViewData,
|
|
27
|
+
VideoEventData,
|
|
28
|
+
} from "@buildinams/tracking/sources/dom";
|
|
29
|
+
import {
|
|
30
|
+
clicks,
|
|
31
|
+
createDomSource,
|
|
32
|
+
detailViews,
|
|
33
|
+
forms,
|
|
34
|
+
impressions,
|
|
35
|
+
pageViews,
|
|
36
|
+
scroll,
|
|
37
|
+
selections,
|
|
38
|
+
videos,
|
|
39
|
+
} from "@buildinams/tracking/sources/dom";
|
|
40
|
+
|
|
41
|
+
type Item = { id: string; name?: string; price?: string };
|
|
42
|
+
type CartLine = { id: string; quantity: number; merchandise: { id: string } };
|
|
43
|
+
type CartFragment = { lines: { nodes: CartLine[] } };
|
|
44
|
+
type Named = { id?: string; name?: string };
|
|
45
|
+
type Surroundings = { list: Named; promotion: Named; location: Named };
|
|
46
|
+
|
|
47
|
+
// A new Page per pathname; pass `key` to count e.g. `?page=2` as one too.
|
|
48
|
+
export const currentPage = createCurrentPage();
|
|
49
|
+
|
|
50
|
+
export const broker = createEventBroker(
|
|
51
|
+
{
|
|
52
|
+
"item:select": {
|
|
53
|
+
data: shape<Item & ItemSelectData>(),
|
|
54
|
+
composed: shape<Surroundings>(),
|
|
55
|
+
},
|
|
56
|
+
"item:view": { data: shape<Item>(), composed: shape<Surroundings>() },
|
|
57
|
+
"promotion:select": {
|
|
58
|
+
data: shape<Named>(),
|
|
59
|
+
composed: shape<Surroundings>(),
|
|
60
|
+
},
|
|
61
|
+
"promotion:view": { data: shape<Named>(), composed: shape<Surroundings>() },
|
|
62
|
+
"list:view": {
|
|
63
|
+
data: shape<ListViewData>(),
|
|
64
|
+
composed: shape<Surroundings>(),
|
|
65
|
+
},
|
|
66
|
+
"link:click": {
|
|
67
|
+
data: shape<LinkClickData>(),
|
|
68
|
+
composed: shape<Surroundings>(),
|
|
69
|
+
},
|
|
70
|
+
"button:click": {
|
|
71
|
+
data: shape<ButtonClickData>(),
|
|
72
|
+
composed: shape<Surroundings>(),
|
|
73
|
+
},
|
|
74
|
+
"form:start": {
|
|
75
|
+
data: shape<FormEventData>(),
|
|
76
|
+
composed: shape<Surroundings>(),
|
|
77
|
+
},
|
|
78
|
+
"form:submit": {
|
|
79
|
+
data: shape<FormSubmitData>(),
|
|
80
|
+
composed: shape<Surroundings>(),
|
|
81
|
+
},
|
|
82
|
+
"video:start": { data: shape<VideoEventData>() },
|
|
83
|
+
"video:progress": { data: shape<VideoEventData>() },
|
|
84
|
+
"video:complete": { data: shape<VideoEventData>() },
|
|
85
|
+
"page:view": { data: shape<{ type?: string }>() },
|
|
86
|
+
"page:scroll": { data: shape<{ depth: string }>() },
|
|
87
|
+
"cart:add": { data: shape<{ line: CartLine; quantity: number }>() },
|
|
88
|
+
"cart:remove": { data: shape<{ line: CartLine; quantity: number }>() },
|
|
89
|
+
},
|
|
90
|
+
// Optional; its return type becomes every Event's `context`.
|
|
91
|
+
{ context: () => ({ ...currentPage.read(), market: "nl" }) },
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const destinations = createDestinations(broker.events, [
|
|
95
|
+
map(withConsent(ga4(), "none"), {
|
|
96
|
+
// The Composition decides: an Item in a Promotion but not a List is a promotion selection.
|
|
97
|
+
"item:select": ({ data, composed: { list, promotion }, meta: { id } }) =>
|
|
98
|
+
promotion && !list
|
|
99
|
+
? {
|
|
100
|
+
name: "select_promotion",
|
|
101
|
+
payload: {
|
|
102
|
+
event_id: id,
|
|
103
|
+
promotion_id: promotion.data.id,
|
|
104
|
+
items: [{ item_id: data.id }],
|
|
105
|
+
},
|
|
106
|
+
}
|
|
107
|
+
: {
|
|
108
|
+
name: "select_item",
|
|
109
|
+
payload: {
|
|
110
|
+
event_id: id,
|
|
111
|
+
items: [
|
|
112
|
+
{
|
|
113
|
+
item_id: data.id,
|
|
114
|
+
item_list_id: list?.data.id,
|
|
115
|
+
index: data.index,
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
121
|
+
map(withConsent(shopify(), ["analytics"]), {
|
|
122
|
+
"item:view": ({ data }) => ({ name: "product_viewed", payload: { data } }),
|
|
123
|
+
}),
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
export const domSource = createDomSource(
|
|
127
|
+
broker,
|
|
128
|
+
[
|
|
129
|
+
pageViews(),
|
|
130
|
+
selections(),
|
|
131
|
+
impressions(),
|
|
132
|
+
detailViews(),
|
|
133
|
+
clicks(),
|
|
134
|
+
forms(),
|
|
135
|
+
videos(),
|
|
136
|
+
scroll(),
|
|
137
|
+
],
|
|
138
|
+
// Flushes Detectors when a Page ends and resets them when the next starts.
|
|
139
|
+
{ currentPage },
|
|
140
|
+
);
|
|
141
|
+
// Or your own adapter: { lines: (cart) => …, key: (line) => …, quantity: (line) => … }
|
|
142
|
+
export const cartSource = createCartSource(broker, shopifyCart<CartFragment>());
|
|
143
|
+
|
|
144
|
+
// In the browser, once, e.g. in instrumentation-client.ts (see Starting the Flow):
|
|
145
|
+
startTracking(broker, cookiebot(), destinations, {
|
|
146
|
+
sources: [domSource, cartSource],
|
|
147
|
+
// Tells Shopify what the shopper answered in Cookiebot
|
|
148
|
+
consentSinks: [shopifyCustomerPrivacy()],
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Use `onetrust()` from `@buildinams/tracking/consent/onetrust` or `shopifyConsent()` from `@buildinams/tracking/consent/shopify` instead of `cookiebot()` for OneTrust or Shopify's privacy banner.
|
|
153
|
+
|
|
154
|
+
Then:
|
|
155
|
+
|
|
156
|
+
- Opt in to clicks and videos per element with a Flag (`data-e="click"`, `data-e="video"`), or per area with `data-e-auto="links buttons forms videos"`, e.g. `<footer data-e-auto="links">`. `data-e-auto="none"` opts an area or a single element out.
|
|
157
|
+
- Describe each Page with `data-e="page"`, e.g. `<main data-e="page" data-e-data="type:product">`: `pageViews()` publishes one `page:view` per Page with the Data of every such element, also when there are none. Put it on an element without a Role, e.g. `<div hidden data-e="page" data-e-data="type:product">` inside an Item, or the Role's Data is mixed in. List `pageViews()` first so `page:view` precedes the Page's other Events, and when mapping it to GA4's `page_view`, turn off gtag's own (`send_page_view: false` and Enhanced Measurement's history page views) or Pages count twice.
|
|
158
|
+
- Mark the product on its detail page `data-e="item detail"` for `item:view`; clicks inside it, like add to cart, aren't `item:select`. `data-e` takes several words in any order.
|
|
159
|
+
- Annotate markup: `data-e="item" data-e-data="id:product1"`, with `data-e-data="name"` on a descendant to use its text. In JSX or templates, spread `annotate()` from `/sources/dom` to write both attributes, e.g. `<div {...annotate(["item", "detail"], { id, title })} />`: it escapes `;` and `\` in values, which hand-written markup writes as `\;` and `\\`.
|
|
160
|
+
- Limit a Detector by pathname with `only()` or `except()` from `/sources/dom`, e.g. `except(scroll(), ["/collections/*", "/search", /^\/account\//])` or `only(videos(), ["/lookbook/*"])`.
|
|
161
|
+
- Call `currentPage.sync()` after every client-side navigation; in React, from a layout effect (see [New Pages in React](#new-pages-in-react)). Calling it when the Page didn't change does nothing.
|
|
162
|
+
- Call `cartSource.setSnapshot(cart)` whenever the cart changes.
|
|
163
|
+
|
|
164
|
+
## Starting the Flow
|
|
165
|
+
|
|
166
|
+
Start the Flow once, in the browser's entry point rather than in a component: it then runs before hydration, so clicks and views before React takes over count too. In Next.js, that's `instrumentation-client.ts` at the project root (or in `src/`):
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
// instrumentation-client.ts
|
|
170
|
+
import { startTracking } from "@buildinams/tracking/flow";
|
|
171
|
+
|
|
172
|
+
import { broker, cartSource, destinations, domSource } from "./src/tracking";
|
|
173
|
+
|
|
174
|
+
startTracking(broker, cookiebot(), destinations, {
|
|
175
|
+
sources: [domSource, cartSource],
|
|
176
|
+
consentSinks: [shopifyCustomerPrivacy()],
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Elsewhere, call it in the module that boots the page. On the server it does nothing, and running it again, e.g. after HMR, replaces the previous Flow.
|
|
181
|
+
|
|
182
|
+
## New Pages in React
|
|
183
|
+
|
|
184
|
+
`currentPage.sync()` stays in React: it has to run after the URL changed, which a router's "transition start" hook (like Next's `onRouterTransitionStart`) is too early for. Call it from a layout effect on every navigation:
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
"use client";
|
|
188
|
+
|
|
189
|
+
import { usePathname } from "next/navigation";
|
|
190
|
+
import * as React from "react";
|
|
191
|
+
|
|
192
|
+
import { currentPage } from "./tracking";
|
|
193
|
+
|
|
194
|
+
export const TrackingPages = () => {
|
|
195
|
+
const pathname = usePathname();
|
|
196
|
+
// With a `key` that reads the query, add `useSearchParams()` here too.
|
|
197
|
+
React.useLayoutEffect(() => currentPage.sync(), [pathname]);
|
|
198
|
+
|
|
199
|
+
return null;
|
|
200
|
+
};
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
It must be a layout effect. React commits the new page's DOM first, then runs layout effects, and only then do Detectors watching the DOM (like `detailViews()`) report what changed. With `useEffect`, a product page's `item:view` is published before `currentPage.sync()` and carries the previous Page's referrer. The first Page is the one `createCurrentPage()` was called on, so the initial render's `sync()` does nothing.
|
|
204
|
+
|
|
205
|
+
## Custom Context
|
|
206
|
+
|
|
207
|
+
`context` is any function; its return type becomes every Event's `context`, typed in Mappings. Without it, `context` is `undefined`. Spread `currentPage.read()` into your own to add project data, such as the user:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
// src/tracking/context.ts
|
|
211
|
+
import { createCurrentPage } from "@buildinams/tracking/page";
|
|
212
|
+
|
|
213
|
+
// The one Current Page: also pass it to `createDomSource()` and call its `sync()`.
|
|
214
|
+
export const currentPage = createCurrentPage();
|
|
215
|
+
|
|
216
|
+
type User =
|
|
217
|
+
| { loggedIn: true; id: string; tier: "member" | "vip" }
|
|
218
|
+
| { loggedIn: false };
|
|
219
|
+
|
|
220
|
+
let user: User = { loggedIn: false };
|
|
221
|
+
|
|
222
|
+
/** Called by the app whenever the session changes. */
|
|
223
|
+
export const setTrackingUser = (next: User) => {
|
|
224
|
+
user = next;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export const readContext = () => ({
|
|
228
|
+
...currentPage.read(),
|
|
229
|
+
user,
|
|
230
|
+
market: document.documentElement.dataset.market ?? "default",
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
const broker = createEventBroker(catalog, { context: readContext });
|
|
236
|
+
|
|
237
|
+
const destinations = createDestinations(broker.events, [
|
|
238
|
+
map(withConsent(ga4(), "none"), {
|
|
239
|
+
"item:select": ({ data, context: { user } }) => ({
|
|
240
|
+
name: "select_item",
|
|
241
|
+
payload: {
|
|
242
|
+
user_id: user.loggedIn ? user.id : undefined,
|
|
243
|
+
items: [{ item_id: data.id }],
|
|
244
|
+
},
|
|
245
|
+
}),
|
|
246
|
+
}),
|
|
247
|
+
]);
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
```tsx
|
|
251
|
+
// Wherever the session lives
|
|
252
|
+
const TrackingUser = () => {
|
|
253
|
+
const session = useSession();
|
|
254
|
+
useEffect(() => {
|
|
255
|
+
setTrackingUser(
|
|
256
|
+
session
|
|
257
|
+
? { loggedIn: true, id: session.id, tier: session.tier }
|
|
258
|
+
: { loggedIn: false },
|
|
259
|
+
);
|
|
260
|
+
}, [session]);
|
|
261
|
+
return null;
|
|
262
|
+
};
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
- **Read on publish.** Each Event keeps the Context of the moment it happened, also while it waits for consent; a login mid-visit shows from the next Event on.
|
|
266
|
+
- **Synchronous.** The reader runs inside `publish()`; keep async data, like a fetched profile, in a variable it returns, as `setTrackingUser` does.
|
|
267
|
+
- **Only what Mappings send leaves the page.** Context isn't forwarded by itself: each Mapping picks the fields its Destination gets, behind that Destination's consent, so personal data is decided per Destination.
|
|
268
|
+
- **Browser only.** The reader never runs on the server, so it can use `document` and `window`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/config/index.ts"],"names":[],"mappings":";AAAO,IAAM,cAAA,GAAiB,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa","file":"chunk-KLN5K6PR.js","sourcesContent":["export const IS_DEVELOPMENT = process.env.NODE_ENV === \"development\";\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { C as Catalog, a as EventName, b as EventData } from './types-DfPz7mBl.js';
|
|
2
|
+
|
|
3
|
+
/** Keys a type names, without index signatures such as `data-e-data` fields. */
|
|
4
|
+
type KnownKeys<T> = keyof {
|
|
5
|
+
[K in keyof T as string extends K ? never : K]: T[K];
|
|
6
|
+
};
|
|
7
|
+
/** Fields the catalog declares that are published with an incompatible type. */
|
|
8
|
+
type Conflicts<Published, Declared> = {
|
|
9
|
+
[K in KnownKeys<Published> & keyof Declared]: Published[K] extends Declared[K] ? never : K;
|
|
10
|
+
}[KnownKeys<Published> & keyof Declared];
|
|
11
|
+
type Mismatch<N, K extends keyof Published, Published, Declared> = {
|
|
12
|
+
"Catalog Data doesn't match what is published": {
|
|
13
|
+
event: N;
|
|
14
|
+
field: K;
|
|
15
|
+
published: Published[K];
|
|
16
|
+
declared: Declared[K & keyof Declared];
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* `Published` unchanged when the catalog declares every Event Name with compatible Data,
|
|
21
|
+
* otherwise an error type naming the Event and field. Extra or missing catalog fields pass.
|
|
22
|
+
*/
|
|
23
|
+
type CheckPublished<C extends Catalog, Published> = {
|
|
24
|
+
[N in keyof Published]: N extends EventName<C> ? [Conflicts<Published[N], EventData<C, N>>] extends [never] ? Published[N] : Mismatch<N, Conflicts<Published[N], EventData<C, N>>, Published[N], EventData<C, N>> : {
|
|
25
|
+
"Event Name missing from the catalog": N;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/** `unknown` when compatible; otherwise a required property carrying the error, for checking values that can't carry `published`. */
|
|
29
|
+
type RequireCompatible<C extends Catalog, Published> = [
|
|
30
|
+
CheckPublished<C, Published>
|
|
31
|
+
] extends [Published] ? unknown : {
|
|
32
|
+
"Catalog doesn't accept what is published": CheckPublished<C, Published>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type { CheckPublished as C, RequireCompatible as R };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/consent/cookiebot/cookiebot.ts
|
|
2
|
+
var EVENTS = [
|
|
3
|
+
"CookiebotOnConsentReady",
|
|
4
|
+
"CookiebotOnAccept",
|
|
5
|
+
"CookiebotOnDecline"
|
|
6
|
+
];
|
|
7
|
+
var read = () => {
|
|
8
|
+
const cookiebot2 = window.Cookiebot;
|
|
9
|
+
if (!cookiebot2?.hasResponse || !cookiebot2.consent) return null;
|
|
10
|
+
const { preferences, statistics, marketing } = cookiebot2.consent;
|
|
11
|
+
return { necessary: true, preferences, analytics: statistics, marketing };
|
|
12
|
+
};
|
|
13
|
+
var cookiebot = () => ({
|
|
14
|
+
name: "cookiebot",
|
|
15
|
+
get: read,
|
|
16
|
+
subscribe(listener) {
|
|
17
|
+
const onChange = () => listener(read());
|
|
18
|
+
for (const name of EVENTS) window.addEventListener(name, onChange);
|
|
19
|
+
return () => {
|
|
20
|
+
for (const name of EVENTS) window.removeEventListener(name, onChange);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export { cookiebot };
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/consent/cookiebot/cookiebot.ts"],"names":["cookiebot"],"mappings":";AASA,IAAM,MAAA,GAAS;AAAA,EACd,yBAAA;AAAA,EACA,mBAAA;AAAA,EACA;AACD,CAAA;AAEA,IAAM,OAAO,MAAsB;AAClC,EAAA,MAAMA,aAAa,MAAA,CAA2B,SAAA;AAC9C,EAAA,IAAI,CAACA,UAAAA,EAAW,WAAA,IAAe,CAACA,UAAAA,CAAU,SAAS,OAAO,IAAA;AAC1D,EAAA,MAAM,EAAE,WAAA,EAAa,UAAA,EAAY,SAAA,KAAcA,UAAAA,CAAU,OAAA;AACzD,EAAA,OAAO,EAAE,SAAA,EAAW,IAAA,EAAM,WAAA,EAAa,SAAA,EAAW,YAAY,SAAA,EAAU;AACzE,CAAA;AAEO,IAAM,YAAY,OAAwB;AAAA,EAChD,IAAA,EAAM,WAAA;AAAA,EACN,GAAA,EAAK,IAAA;AAAA,EACL,UAAU,QAAA,EAAU;AACnB,IAAA,MAAM,QAAA,GAAW,MAAM,QAAA,CAAS,IAAA,EAAM,CAAA;AACtC,IAAA,KAAA,MAAW,IAAA,IAAQ,MAAA,EAAQ,MAAA,CAAO,gBAAA,CAAiB,MAAM,QAAQ,CAAA;AACjE,IAAA,OAAO,MAAM;AACZ,MAAA,KAAA,MAAW,IAAA,IAAQ,MAAA,EAAQ,MAAA,CAAO,mBAAA,CAAoB,MAAM,QAAQ,CAAA;AAAA,IACrE,CAAA;AAAA,EACD;AACD,CAAA","file":"index.js","sourcesContent":["import type { Consent, ConsentProvider } from \"..\";\n\ntype CookiebotGlobal = {\n\tCookiebot?: {\n\t\thasResponse?: boolean;\n\t\tconsent?: { preferences: boolean; statistics: boolean; marketing: boolean };\n\t};\n};\n\nconst EVENTS = [\n\t\"CookiebotOnConsentReady\",\n\t\"CookiebotOnAccept\",\n\t\"CookiebotOnDecline\",\n];\n\nconst read = (): Consent | null => {\n\tconst cookiebot = (window as CookiebotGlobal).Cookiebot;\n\tif (!cookiebot?.hasResponse || !cookiebot.consent) return null;\n\tconst { preferences, statistics, marketing } = cookiebot.consent;\n\treturn { necessary: true, preferences, analytics: statistics, marketing };\n};\n\nexport const cookiebot = (): ConsentProvider => ({\n\tname: \"cookiebot\",\n\tget: read,\n\tsubscribe(listener) {\n\t\tconst onChange = () => listener(read());\n\t\tfor (const name of EVENTS) window.addEventListener(name, onChange);\n\t\treturn () => {\n\t\t\tfor (const name of EVENTS) window.removeEventListener(name, onChange);\n\t\t};\n\t},\n});\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type ConsentCategory = "necessary" | "preferences" | "analytics" | "marketing";
|
|
2
|
+
type Consent = Readonly<Record<ConsentCategory, boolean>>;
|
|
3
|
+
/** `none` is for Destinations that enforce consent themselves. */
|
|
4
|
+
type ConsentRequirement = "none" | readonly ConsentCategory[];
|
|
5
|
+
type ConsentProvider = {
|
|
6
|
+
name: string;
|
|
7
|
+
/** `null` while the shopper hasn't answered. */
|
|
8
|
+
get(): Consent | null;
|
|
9
|
+
subscribe(listener: (consent: Consent | null) => void): () => void;
|
|
10
|
+
};
|
|
11
|
+
/** Is told every consent answer, e.g. a platform that gates its own tracking on it. */
|
|
12
|
+
type ConsentSink = {
|
|
13
|
+
/** A sink named like the Consent Provider is skipped, so consent isn't written back to its source. */
|
|
14
|
+
name: string;
|
|
15
|
+
apply(consent: Consent): void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type { Consent, ConsentCategory, ConsentProvider, ConsentRequirement, ConsentSink };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ConsentCategory, ConsentProvider } from '../index.js';
|
|
2
|
+
|
|
3
|
+
type OneTrustOptions = {
|
|
4
|
+
/** OneTrust category ID per consent category; defaults to OneTrust's standard IDs. */
|
|
5
|
+
groups?: Partial<Record<ConsentCategory, string>>;
|
|
6
|
+
};
|
|
7
|
+
declare const onetrust: ({ groups }?: OneTrustOptions) => ConsentProvider;
|
|
8
|
+
|
|
9
|
+
export { type OneTrustOptions, onetrust };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/consent/onetrust/onetrust.ts
|
|
2
|
+
var DEFAULT_GROUPS = {
|
|
3
|
+
necessary: "C0001",
|
|
4
|
+
analytics: "C0002",
|
|
5
|
+
preferences: "C0003",
|
|
6
|
+
marketing: "C0004"
|
|
7
|
+
};
|
|
8
|
+
var onetrust = ({ groups } = {}) => {
|
|
9
|
+
const ids = { ...DEFAULT_GROUPS, ...groups };
|
|
10
|
+
const read = () => {
|
|
11
|
+
const { OneTrust, OnetrustActiveGroups } = window;
|
|
12
|
+
if (!OneTrust?.IsAlertBoxClosed?.() || OnetrustActiveGroups === void 0)
|
|
13
|
+
return null;
|
|
14
|
+
const active = new Set(OnetrustActiveGroups.split(",").filter(Boolean));
|
|
15
|
+
return {
|
|
16
|
+
necessary: true,
|
|
17
|
+
preferences: active.has(ids.preferences),
|
|
18
|
+
analytics: active.has(ids.analytics),
|
|
19
|
+
marketing: active.has(ids.marketing)
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
name: "onetrust",
|
|
24
|
+
get: read,
|
|
25
|
+
subscribe(listener) {
|
|
26
|
+
const onChange = () => listener(read());
|
|
27
|
+
window.addEventListener("OneTrustGroupsUpdated", onChange);
|
|
28
|
+
return () => window.removeEventListener("OneTrustGroupsUpdated", onChange);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { onetrust };
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/consent/onetrust/onetrust.ts"],"names":[],"mappings":";AAYA,IAAM,cAAA,GAAkD;AAAA,EACvD,SAAA,EAAW,OAAA;AAAA,EACX,SAAA,EAAW,OAAA;AAAA,EACX,WAAA,EAAa,OAAA;AAAA,EACb,SAAA,EAAW;AACZ,CAAA;AAEO,IAAM,WAAW,CAAC,EAAE,MAAA,EAAO,GAAqB,EAAC,KAAuB;AAC9E,EAAA,MAAM,GAAA,GAAM,EAAE,GAAG,cAAA,EAAgB,GAAG,MAAA,EAAO;AAE3C,EAAA,MAAM,OAAO,MAAsB;AAClC,IAAA,MAAM,EAAE,QAAA,EAAU,oBAAA,EAAqB,GAAI,MAAA;AAC3C,IAAA,IAAI,CAAC,QAAA,EAAU,gBAAA,IAAmB,IAAK,oBAAA,KAAyB,MAAA;AAC/D,MAAA,OAAO,IAAA;AACR,IAAA,MAAM,MAAA,GAAS,IAAI,GAAA,CAAI,oBAAA,CAAqB,MAAM,GAAG,CAAA,CAAE,MAAA,CAAO,OAAO,CAAC,CAAA;AACtE,IAAA,OAAO;AAAA,MACN,SAAA,EAAW,IAAA;AAAA,MACX,WAAA,EAAa,MAAA,CAAO,GAAA,CAAI,GAAA,CAAI,WAAW,CAAA;AAAA,MACvC,SAAA,EAAW,MAAA,CAAO,GAAA,CAAI,GAAA,CAAI,SAAS,CAAA;AAAA,MACnC,SAAA,EAAW,MAAA,CAAO,GAAA,CAAI,GAAA,CAAI,SAAS;AAAA,KACpC;AAAA,EACD,CAAA;AAEA,EAAA,OAAO;AAAA,IACN,IAAA,EAAM,UAAA;AAAA,IACN,GAAA,EAAK,IAAA;AAAA,IACL,UAAU,QAAA,EAAU;AACnB,MAAA,MAAM,QAAA,GAAW,MAAM,QAAA,CAAS,IAAA,EAAM,CAAA;AACtC,MAAA,MAAA,CAAO,gBAAA,CAAiB,yBAAyB,QAAQ,CAAA;AACzD,MAAA,OAAO,MACN,MAAA,CAAO,mBAAA,CAAoB,uBAAA,EAAyB,QAAQ,CAAA;AAAA,IAC9D;AAAA,GACD;AACD","file":"index.js","sourcesContent":["import type { Consent, ConsentCategory, ConsentProvider } from \"..\";\n\nexport type OneTrustOptions = {\n\t/** OneTrust category ID per consent category; defaults to OneTrust's standard IDs. */\n\tgroups?: Partial<Record<ConsentCategory, string>>;\n};\n\ntype OneTrustGlobal = {\n\tOnetrustActiveGroups?: string;\n\tOneTrust?: { IsAlertBoxClosed?(): boolean };\n};\n\nconst DEFAULT_GROUPS: Record<ConsentCategory, string> = {\n\tnecessary: \"C0001\",\n\tanalytics: \"C0002\",\n\tpreferences: \"C0003\",\n\tmarketing: \"C0004\",\n};\n\nexport const onetrust = ({ groups }: OneTrustOptions = {}): ConsentProvider => {\n\tconst ids = { ...DEFAULT_GROUPS, ...groups };\n\n\tconst read = (): Consent | null => {\n\t\tconst { OneTrust, OnetrustActiveGroups } = window as OneTrustGlobal;\n\t\tif (!OneTrust?.IsAlertBoxClosed?.() || OnetrustActiveGroups === undefined)\n\t\t\treturn null;\n\t\tconst active = new Set(OnetrustActiveGroups.split(\",\").filter(Boolean));\n\t\treturn {\n\t\t\tnecessary: true,\n\t\t\tpreferences: active.has(ids.preferences),\n\t\t\tanalytics: active.has(ids.analytics),\n\t\t\tmarketing: active.has(ids.marketing),\n\t\t};\n\t};\n\n\treturn {\n\t\tname: \"onetrust\",\n\t\tget: read,\n\t\tsubscribe(listener) {\n\t\t\tconst onChange = () => listener(read());\n\t\t\twindow.addEventListener(\"OneTrustGroupsUpdated\", onChange);\n\t\t\treturn () =>\n\t\t\t\twindow.removeEventListener(\"OneTrustGroupsUpdated\", onChange);\n\t\t},\n\t};\n};\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ConsentProvider, ConsentCategory, Consent, ConsentSink } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/** Reads consent from Shopify's Customer Privacy API, e.g. its privacy banner. */
|
|
4
|
+
declare const shopifyConsent: () => ConsentProvider;
|
|
5
|
+
|
|
6
|
+
type ShopifyCustomerPrivacyOptions = {
|
|
7
|
+
/** What Shopify's `sale_of_data` follows: a consent category, or a function of the answer. */
|
|
8
|
+
saleOfData?: ConsentCategory | ((consent: Consent) => boolean);
|
|
9
|
+
};
|
|
10
|
+
/** Forwards consent to Shopify's Customer Privacy API. */
|
|
11
|
+
declare const shopifyCustomerPrivacy: ({ saleOfData, }?: ShopifyCustomerPrivacyOptions) => ConsentSink;
|
|
12
|
+
|
|
13
|
+
export { type ShopifyCustomerPrivacyOptions, shopifyConsent, shopifyCustomerPrivacy };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { IS_DEVELOPMENT } from '../../chunk-KLN5K6PR.js';
|
|
2
|
+
|
|
3
|
+
// src/consent/shopify/customer-privacy-api.ts
|
|
4
|
+
var warn = (...args) => {
|
|
5
|
+
if (IS_DEVELOPMENT) console.warn("[tracking]", ...args);
|
|
6
|
+
};
|
|
7
|
+
var API_LOADED_EVENT = "consentTrackingApiLoaded";
|
|
8
|
+
var isLoaded = (api) => typeof api?.setTrackingConsent === "function" && api.consentStatus !== "loading";
|
|
9
|
+
var readCustomerPrivacy = () => {
|
|
10
|
+
const api = window.Shopify?.customerPrivacy;
|
|
11
|
+
return isLoaded(api) ? api : void 0;
|
|
12
|
+
};
|
|
13
|
+
var withCustomerPrivacy = (callback) => {
|
|
14
|
+
const api = readCustomerPrivacy();
|
|
15
|
+
if (api) return callback(api);
|
|
16
|
+
const shopify = window.Shopify;
|
|
17
|
+
if (!shopify?.loadFeatures) {
|
|
18
|
+
document.addEventListener(
|
|
19
|
+
API_LOADED_EVENT,
|
|
20
|
+
() => {
|
|
21
|
+
const loaded = readCustomerPrivacy();
|
|
22
|
+
if (loaded) callback(loaded);
|
|
23
|
+
else warn("Shopify's Customer Privacy API didn't load");
|
|
24
|
+
},
|
|
25
|
+
{ once: true }
|
|
26
|
+
);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
shopify.loadFeatures(
|
|
30
|
+
[{ name: "consent-tracking-api", version: "0.1" }],
|
|
31
|
+
(error) => {
|
|
32
|
+
const loaded = readCustomerPrivacy();
|
|
33
|
+
if (error || !loaded)
|
|
34
|
+
warn("Shopify's Customer Privacy API didn't load", error);
|
|
35
|
+
else callback(loaded);
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/consent/shopify/shopify-consent.ts
|
|
41
|
+
var read = () => {
|
|
42
|
+
const api = readCustomerPrivacy();
|
|
43
|
+
if (!api) return null;
|
|
44
|
+
const { analytics, marketing, preferences } = api.currentVisitorConsent();
|
|
45
|
+
const answered = !api.shouldShowBanner() || [analytics, marketing, preferences].some(Boolean);
|
|
46
|
+
if (!answered) return null;
|
|
47
|
+
return {
|
|
48
|
+
necessary: true,
|
|
49
|
+
preferences: api.preferencesProcessingAllowed(),
|
|
50
|
+
analytics: api.analyticsProcessingAllowed(),
|
|
51
|
+
marketing: api.marketingAllowed()
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
var shopifyConsent = () => ({
|
|
55
|
+
name: "shopify",
|
|
56
|
+
get: read,
|
|
57
|
+
subscribe(listener) {
|
|
58
|
+
let active = true;
|
|
59
|
+
const onChange = () => listener(read());
|
|
60
|
+
document.addEventListener("visitorConsentCollected", onChange);
|
|
61
|
+
withCustomerPrivacy(() => {
|
|
62
|
+
if (active) onChange();
|
|
63
|
+
});
|
|
64
|
+
return () => {
|
|
65
|
+
active = false;
|
|
66
|
+
document.removeEventListener("visitorConsentCollected", onChange);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// src/consent/shopify/shopify-customer-privacy.ts
|
|
72
|
+
var shopifyCustomerPrivacy = ({
|
|
73
|
+
saleOfData = "marketing"
|
|
74
|
+
} = {}) => ({
|
|
75
|
+
name: "shopify",
|
|
76
|
+
apply(consent) {
|
|
77
|
+
const { analytics, marketing, preferences } = consent;
|
|
78
|
+
const sale_of_data = typeof saleOfData === "function" ? saleOfData(consent) : consent[saleOfData];
|
|
79
|
+
withCustomerPrivacy(
|
|
80
|
+
(api) => api.setTrackingConsent(
|
|
81
|
+
{ analytics, marketing, preferences, sale_of_data },
|
|
82
|
+
(result) => {
|
|
83
|
+
if (result?.error)
|
|
84
|
+
warn("Shopify rejected the tracking consent", result.error);
|
|
85
|
+
}
|
|
86
|
+
)
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
export { shopifyConsent, shopifyCustomerPrivacy };
|
|
92
|
+
//# sourceMappingURL=index.js.map
|
|
93
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/consent/shopify/customer-privacy-api.ts","../../../src/consent/shopify/shopify-consent.ts","../../../src/consent/shopify/shopify-customer-privacy.ts"],"names":[],"mappings":"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","file":"index.js","sourcesContent":["import { IS_DEVELOPMENT } from \"../../config\";\n\ntype ConsentValue = \"yes\" | \"no\" | \"\";\n\nexport type CustomerPrivacyApi = {\n\tsetTrackingConsent(\n\t\tconsent: {\n\t\t\tanalytics: boolean;\n\t\t\tmarketing: boolean;\n\t\t\tpreferences: boolean;\n\t\t\tsale_of_data: boolean;\n\t\t},\n\t\tcallback: (result?: { error?: unknown }) => void,\n\t): void;\n\tcurrentVisitorConsent(): {\n\t\tanalytics: ConsentValue;\n\t\tmarketing: ConsentValue;\n\t\tpreferences: ConsentValue;\n\t};\n\tshouldShowBanner(): boolean;\n\tanalyticsProcessingAllowed(): boolean;\n\tmarketingAllowed(): boolean;\n\tpreferencesProcessingAllowed(): boolean;\n\t/** Set on headless storefronts, by Shopify's scripts. */\n\tconsentStatus?: \"loading\" | \"loaded\";\n};\n\ntype ShopifyGlobal = {\n\tShopify?: {\n\t\tcustomerPrivacy?: Partial<CustomerPrivacyApi>;\n\t\tloadFeatures?(\n\t\t\tfeatures: { name: string; version: string }[],\n\t\t\tcallback: (error?: unknown) => void,\n\t\t): void;\n\t};\n};\n\nexport const warn = (...args: unknown[]) => {\n\tif (IS_DEVELOPMENT) console.warn(\"[tracking]\", ...args);\n};\n\n/** Fired on `document` by Shopify's scripts on headless storefronts once the API has loaded. */\nconst API_LOADED_EVENT = \"consentTrackingApiLoaded\";\n\n// Headless storefronts expose a placeholder first: without methods, then one that allows everything while loading.\nconst isLoaded = (\n\tapi: Partial<CustomerPrivacyApi> | undefined,\n): api is CustomerPrivacyApi =>\n\ttypeof api?.setTrackingConsent === \"function\" &&\n\tapi.consentStatus !== \"loading\";\n\n/** The API once it has loaded, else `undefined`. */\nexport const readCustomerPrivacy = () => {\n\tconst api = (window as ShopifyGlobal).Shopify?.customerPrivacy;\n\treturn isLoaded(api) ? api : undefined;\n};\n\n/** Calls back with the API once it has loaded: loading it on themes, waiting for Shopify's scripts on headless storefronts. */\nexport const withCustomerPrivacy = (\n\tcallback: (api: CustomerPrivacyApi) => void,\n) => {\n\tconst api = readCustomerPrivacy();\n\tif (api) return callback(api);\n\tconst shopify = (window as ShopifyGlobal).Shopify;\n\tif (!shopify?.loadFeatures) {\n\t\tdocument.addEventListener(\n\t\t\tAPI_LOADED_EVENT,\n\t\t\t() => {\n\t\t\t\tconst loaded = readCustomerPrivacy();\n\t\t\t\tif (loaded) callback(loaded);\n\t\t\t\telse warn(\"Shopify's Customer Privacy API didn't load\");\n\t\t\t},\n\t\t\t{ once: true },\n\t\t);\n\t\treturn;\n\t}\n\tshopify.loadFeatures(\n\t\t[{ name: \"consent-tracking-api\", version: \"0.1\" }],\n\t\t(error) => {\n\t\t\tconst loaded = readCustomerPrivacy();\n\t\t\tif (error || !loaded)\n\t\t\t\twarn(\"Shopify's Customer Privacy API didn't load\", error);\n\t\t\telse callback(loaded);\n\t\t},\n\t);\n};\n","import type { Consent, ConsentProvider } from \"..\";\nimport {\n\treadCustomerPrivacy,\n\twithCustomerPrivacy,\n} from \"./customer-privacy-api\";\n\nconst read = (): Consent | null => {\n\tconst api = readCustomerPrivacy();\n\tif (!api) return null;\n\tconst { analytics, marketing, preferences } = api.currentVisitorConsent();\n\t// Where no banner is required, Shopify's regional defaults are the answer.\n\tconst answered =\n\t\t!api.shouldShowBanner() ||\n\t\t[analytics, marketing, preferences].some(Boolean);\n\tif (!answered) return null;\n\treturn {\n\t\tnecessary: true,\n\t\tpreferences: api.preferencesProcessingAllowed(),\n\t\tanalytics: api.analyticsProcessingAllowed(),\n\t\tmarketing: api.marketingAllowed(),\n\t};\n};\n\n/** Reads consent from Shopify's Customer Privacy API, e.g. its privacy banner. */\nexport const shopifyConsent = (): ConsentProvider => ({\n\tname: \"shopify\",\n\tget: read,\n\tsubscribe(listener) {\n\t\tlet active = true;\n\t\tconst onChange = () => listener(read());\n\t\tdocument.addEventListener(\"visitorConsentCollected\", onChange);\n\t\twithCustomerPrivacy(() => {\n\t\t\tif (active) onChange();\n\t\t});\n\t\treturn () => {\n\t\t\tactive = false;\n\t\t\tdocument.removeEventListener(\"visitorConsentCollected\", onChange);\n\t\t};\n\t},\n});\n","import type { Consent, ConsentCategory, ConsentSink } from \"..\";\nimport { warn, withCustomerPrivacy } from \"./customer-privacy-api\";\n\nexport type ShopifyCustomerPrivacyOptions = {\n\t/** What Shopify's `sale_of_data` follows: a consent category, or a function of the answer. */\n\tsaleOfData?: ConsentCategory | ((consent: Consent) => boolean);\n};\n\n/** Forwards consent to Shopify's Customer Privacy API. */\nexport const shopifyCustomerPrivacy = ({\n\tsaleOfData = \"marketing\",\n}: ShopifyCustomerPrivacyOptions = {}): ConsentSink => ({\n\tname: \"shopify\",\n\tapply(consent) {\n\t\tconst { analytics, marketing, preferences } = consent;\n\t\tconst sale_of_data =\n\t\t\ttypeof saleOfData === \"function\"\n\t\t\t\t? saleOfData(consent)\n\t\t\t\t: consent[saleOfData];\n\t\twithCustomerPrivacy((api) =>\n\t\t\tapi.setTrackingConsent(\n\t\t\t\t{ analytics, marketing, preferences, sale_of_data },\n\t\t\t\t(result) => {\n\t\t\t\t\tif (result?.error)\n\t\t\t\t\t\twarn(\"Shopify rejected the tracking consent\", result.error);\n\t\t\t\t},\n\t\t\t),\n\t\t);\n\t},\n});\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { c as Destination } from '../../types-BfJsv8AV.js';
|
|
2
|
+
import '../../consent/index.js';
|
|
3
|
+
import '../../types-DfPz7mBl.js';
|
|
4
|
+
|
|
5
|
+
type Ga4Event = {
|
|
6
|
+
name: string;
|
|
7
|
+
payload: Record<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
type Ga4Options = {
|
|
10
|
+
/** The GTM data layer's global name. */
|
|
11
|
+
dataLayerName?: string;
|
|
12
|
+
};
|
|
13
|
+
/** Pushes to GTM's data layer; GA4 Consent Mode in the container gates it. */
|
|
14
|
+
declare const ga4: ({ dataLayerName, }?: Ga4Options) => Destination<Ga4Event>;
|
|
15
|
+
|
|
16
|
+
export { type Ga4Event, type Ga4Options, ga4 };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/destinations/ga4/ga4.ts
|
|
2
|
+
var ga4 = ({
|
|
3
|
+
dataLayerName = "dataLayer"
|
|
4
|
+
} = {}) => ({
|
|
5
|
+
name: "ga4",
|
|
6
|
+
send({ name, payload }) {
|
|
7
|
+
const target = window;
|
|
8
|
+
const dataLayer = target[dataLayerName] ??= [];
|
|
9
|
+
dataLayer.push({ event: name, ...payload });
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export { ga4 };
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/destinations/ga4/ga4.ts"],"names":[],"mappings":";AAUO,IAAM,MAAM,CAAC;AAAA,EACnB,aAAA,GAAgB;AACjB,CAAA,GAAgB,EAAC,MAA8B;AAAA,EAC9C,IAAA,EAAM,KAAA;AAAA,EACN,IAAA,CAAK,EAAE,IAAA,EAAM,OAAA,EAAQ,EAAG;AACvB,IAAA,MAAM,MAAA,GAAS,MAAA;AACf,IAAA,MAAM,SAAA,GAAa,MAAA,CAAO,aAAa,CAAA,KAAM,EAAC;AAC9C,IAAA,SAAA,CAAU,KAAK,EAAE,KAAA,EAAO,IAAA,EAAM,GAAG,SAAS,CAAA;AAAA,EAC3C;AACD,CAAA","file":"index.js","sourcesContent":["import type { Destination } from \"..\";\n\nexport type Ga4Event = { name: string; payload: Record<string, unknown> };\n\nexport type Ga4Options = {\n\t/** The GTM data layer's global name. */\n\tdataLayerName?: string;\n};\n\n/** Pushes to GTM's data layer; GA4 Consent Mode in the container gates it. */\nexport const ga4 = ({\n\tdataLayerName = \"dataLayer\",\n}: Ga4Options = {}): Destination<Ga4Event> => ({\n\tname: \"ga4\",\n\tsend({ name, payload }) {\n\t\tconst target = window as unknown as Record<string, unknown[] | undefined>;\n\t\tconst dataLayer = (target[dataLayerName] ??= []);\n\t\tdataLayer.push({ event: name, ...payload });\n\t},\n});\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ConsentRequirement } from '../consent/index.js';
|
|
2
|
+
import { C as Catalog, B as BrokerEvents } from '../types-DfPz7mBl.js';
|
|
3
|
+
import { M as MappedDestination, D as Destinations, a as DestinationEvent, C as ConsentedDestination, b as Mapping, c as Destination } from '../types-BfJsv8AV.js';
|
|
4
|
+
|
|
5
|
+
declare const withConsent: <E extends DestinationEvent>(destination: Destination<E>, requires: ConsentRequirement) => ConsentedDestination<E>;
|
|
6
|
+
declare const map: <C extends Catalog, E extends DestinationEvent, Cx>({ destination, requires }: ConsentedDestination<E>, mapping: NoInfer<Mapping<C, E, Cx>>) => MappedDestination<C, Cx>;
|
|
7
|
+
declare const createDestinations: <C extends Catalog, Cx>(events: BrokerEvents<C, Cx>, mapped: MappedDestination<C, Cx>[]) => Destinations<C, Cx>;
|
|
8
|
+
|
|
9
|
+
export { ConsentedDestination, Destination, DestinationEvent, Destinations, MappedDestination, Mapping, createDestinations, map, withConsent };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/destinations/create-destinations.ts
|
|
2
|
+
var withConsent = (destination, requires) => ({ destination, requires });
|
|
3
|
+
var map = ({ destination, requires }, mapping) => ({
|
|
4
|
+
name: destination.name,
|
|
5
|
+
requires,
|
|
6
|
+
deliver(event) {
|
|
7
|
+
const toEvent = mapping[event.name];
|
|
8
|
+
const mapped = toEvent?.(event);
|
|
9
|
+
if (!mapped) return false;
|
|
10
|
+
return destination.send(mapped);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var createDestinations = (events, mapped) => ({ events, mapped });
|
|
14
|
+
|
|
15
|
+
export { createDestinations, map, withConsent };
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/destinations/create-destinations.ts"],"names":[],"mappings":";AAgBO,IAAM,cAAc,CAC1B,WAAA,EACA,QAAA,MAC8B,EAAE,aAAa,QAAA,EAAS;AAEhD,IAAM,MAAM,CAClB,EAAE,WAAA,EAAa,QAAA,IAEf,OAAA,MAC+B;AAAA,EAC/B,MAAM,WAAA,CAAY,IAAA;AAAA,EAClB,QAAA;AAAA,EACA,QAAQ,KAAA,EAAO;AACd,IAAA,MAAM,OAAA,GAAU,OAAA,CAAQ,KAAA,CAAM,IAAI,CAAA;AAGlC,IAAA,MAAM,MAAA,GAAS,UAAU,KAAK,CAAA;AAC9B,IAAA,IAAI,CAAC,QAAQ,OAAO,KAAA;AACpB,IAAA,OAAO,WAAA,CAAY,KAAK,MAAM,CAAA;AAAA,EAC/B;AACD,CAAA;AAEO,IAAM,qBAAqB,CACjC,MAAA,EACA,MAAA,MAC0B,EAAE,QAAQ,MAAA,EAAO","file":"index.js","sourcesContent":["import type { ConsentRequirement } from \"../consent\";\nimport type {\n\tBrokerEvents,\n\tCatalog,\n\tEventName,\n\tTrackingEvent,\n} from \"../events\";\nimport type {\n\tConsentedDestination,\n\tDestination,\n\tDestinationEvent,\n\tDestinations,\n\tMappedDestination,\n\tMapping,\n} from \"./types\";\n\nexport const withConsent = <E extends DestinationEvent>(\n\tdestination: Destination<E>,\n\trequires: ConsentRequirement,\n): ConsentedDestination<E> => ({ destination, requires });\n\nexport const map = <C extends Catalog, E extends DestinationEvent, Cx>(\n\t{ destination, requires }: ConsentedDestination<E>,\n\t// Only createDestinations()'s catalog decides C and Cx, so a Mapping ignoring its Event still gets typed.\n\tmapping: NoInfer<Mapping<C, E, Cx>>,\n): MappedDestination<C, Cx> => ({\n\tname: destination.name,\n\trequires,\n\tdeliver(event) {\n\t\tconst toEvent = mapping[event.name] as\n\t\t\t| ((event: TrackingEvent<C, EventName<C>, Cx>) => E | null | undefined)\n\t\t\t| undefined;\n\t\tconst mapped = toEvent?.(event);\n\t\tif (!mapped) return false;\n\t\treturn destination.send(mapped);\n\t},\n});\n\nexport const createDestinations = <C extends Catalog, Cx>(\n\tevents: BrokerEvents<C, Cx>,\n\tmapped: MappedDestination<C, Cx>[],\n): Destinations<C, Cx> => ({ events, mapped });\n"]}
|