@escape-game-over/atlas 0.1.60 → 0.1.61

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@escape-game-over/atlas",
3
- "version": "0.1.60",
3
+ "version": "0.1.61",
4
4
  "type": "module",
5
5
  "description": "Typed, data-driven machinery for static multi-locale, multi-deployment Astro sites.",
6
6
  "private": false,
@@ -5,36 +5,37 @@
5
5
  * buttons.
6
6
  *
7
7
  * Renders nothing — and ships no script — when this site's analytics need no
8
- * permission. Also loads the vendors that wait for consent, such as the Meta, TikTok, Snap and Axon pixels, Drip and Clarity. Otherwise the banner starts hidden and shows only to a visitor
8
+ * permission. Otherwise the banner starts hidden and shows only to a visitor
9
9
  * with no answer on record, or when a `data-consent-reopen` control is pressed.
10
+ *
11
+ * It also starts the vendors that wait on the answer — the Meta, TikTok, Snap
12
+ * and Axon pixels, Drip and Clarity — fetching each one's code only once its
13
+ * category is granted.
10
14
  */
11
15
  import type { HTMLAttributes } from "astro/types";
12
16
  import { type AnalyticsSettings, consentRequired } from "../analytics/index.ts";
13
- import AxonPixel from "./AxonPixel.astro";
14
- import Clarity from "./Clarity.astro";
15
17
  import ConsentElement from "./ConsentElement.astro";
16
- import Drip from "./Drip.astro";
17
- import MetaPixel from "./MetaPixel.astro";
18
- import SnapPixel from "./SnapPixel.astro";
19
- import TikTokPixel from "./TikTokPixel.astro";
18
+ import type { ConsentVendors } from "./consent-vendors.ts";
20
19
 
21
20
  type Props = HTMLAttributes<"div"> & {
22
21
  readonly analytics: AnalyticsSettings | undefined;
23
22
  };
24
23
 
25
24
  const { analytics, ...attrs } = Astro.props;
25
+ const vendors: ConsentVendors = {
26
+ meta: analytics?.meta,
27
+ tiktok: analytics?.tiktok,
28
+ snapchat: analytics?.snapchat,
29
+ axon: analytics?.axon,
30
+ drip: analytics?.drip,
31
+ clarity: analytics?.clarity,
32
+ };
26
33
  ---
27
34
 
28
35
  {consentRequired(analytics) && (
29
- <>
30
- <ConsentElement {...attrs}>
31
- <slot />
32
- </ConsentElement>
33
- {analytics?.meta && <MetaPixel settings={analytics.meta} />}
34
- {analytics?.tiktok && <TikTokPixel settings={analytics.tiktok} />}
35
- {analytics?.snapchat && <SnapPixel settings={analytics.snapchat} />}
36
- {analytics?.axon && <AxonPixel settings={analytics.axon} />}
37
- {analytics?.drip && <Drip settings={analytics.drip} />}
38
- {analytics?.clarity && <Clarity settings={analytics.clarity} />}
39
- </>
36
+ <ConsentElement
37
+ {...attrs}
38
+ vendors={vendors}>
39
+ <slot />
40
+ </ConsentElement>
40
41
  )}
@@ -3,15 +3,21 @@
3
3
  import type { HTMLAttributes } from "astro/types";
4
4
  import AtlasElement from "./AtlasElement.astro";
5
5
  import { consentBanner } from "./consent-element.ts";
6
+ import type { ConsentVendors } from "./consent-vendors.ts";
6
7
 
7
- type Props = HTMLAttributes<"div">;
8
+ type Props = HTMLAttributes<"div"> & {
9
+ readonly vendors: ConsentVendors;
10
+ };
11
+
12
+ const { vendors, ...attrs } = Astro.props;
8
13
  ---
9
14
 
10
15
  <script src="./consent-element.ts" />
11
16
 
12
17
  <AtlasElement
13
18
  of={consentBanner}
14
- {...Astro.props}
19
+ {...attrs}
20
+ data-vendors={JSON.stringify(vendors)}
15
21
  hidden
16
22
  role="dialog"
17
23
  aria-modal="false"
@@ -1,8 +1,5 @@
1
1
  import { appendScript } from "./append-script.ts";
2
2
  import type { ConsentGated } from "./consent-gate.ts";
3
- import { whenGranted } from "./consent-gate.ts";
4
- import { element } from "./element.ts";
5
- import { data } from "./ref.ts";
6
3
 
7
4
  /** Axon's `axon`: a queue until its scripts arrive and replay it. */
8
5
  interface Axon {
@@ -62,11 +59,3 @@ export function axonPixel(eventKey: string): ConsentGated {
62
59
  },
63
60
  };
64
61
  }
65
-
66
- /** Internal to `AxonPixel.astro`. */
67
- export const axonPixelElement = element(
68
- "atlas-axon-pixel",
69
- ({ root, signal }) => {
70
- whenGranted("marketing", axonPixel(data(root, "event-key")), signal);
71
- }
72
- );
@@ -1,8 +1,5 @@
1
1
  import { appendScript } from "./append-script.ts";
2
2
  import type { ConsentChoices } from "./consent.ts";
3
- import { withConsent } from "./consent-gate.ts";
4
- import { element } from "./element.ts";
5
- import { data } from "./ref.ts";
6
3
 
7
4
  /** Clarity's `clarity`: a queue until its script arrives and replays it. */
8
5
  interface Clarity {
@@ -39,22 +36,18 @@ const consentv2 = (choices: ConsentChoices) => ({
39
36
  });
40
37
 
41
38
  /**
42
- * Loads Clarity with the answer so far, then passes on every answer. Stated
39
+ * Clarity, loaded with the first answer passed to what this returns, which
40
+ * `startVendors` calls with the answer so far and then every new one. Stated
43
41
  * even when denied: a Clarity project set to use cookies by default would
44
42
  * otherwise set them before anyone was asked.
45
43
  */
46
- export function clarity(projectId: string, signal?: AbortSignal): void {
44
+ export function clarity(projectId: string): (choices: ConsentChoices) => void {
47
45
  let loaded = false;
48
- withConsent((choices) => {
46
+ return (choices) => {
49
47
  const queue = defineClarity();
50
48
  queue("consentv2", consentv2(choices));
51
49
  if (loaded) return;
52
50
  loaded = true;
53
51
  appendScript(`https://www.clarity.ms/tag/${projectId}`);
54
- }, signal);
52
+ };
55
53
  }
56
-
57
- /** Internal to `Clarity.astro`. */
58
- export const clarityElement = element("atlas-clarity", ({ root, signal }) => {
59
- clarity(data(root, "project-id"), signal);
60
- });
@@ -7,6 +7,7 @@ import {
7
7
  readConsent,
8
8
  recordConsent,
9
9
  } from "./consent.ts";
10
+ import { startVendors } from "./consent-vendors.ts";
10
11
  import { element } from "./element.ts";
11
12
  import { data, ref, refs } from "./ref.ts";
12
13
 
@@ -38,6 +39,8 @@ import { data, ref, refs } from "./ref.ts";
38
39
  * withdraw.
39
40
  */
40
41
  export const consentBanner = element(CONSENT_TAG, ({ root, signal }) => {
42
+ startVendors(JSON.parse(data(root, "vendors")), signal);
43
+
41
44
  const boxes = refs<HTMLInputElement>(root, "[data-consent-category]").map(
42
45
  (box) => {
43
46
  const category = data(box, "consent-category");
@@ -0,0 +1,100 @@
1
+ import type { AnalyticsSettings } from "../analytics/index.ts";
2
+ import type { ConsentChoices } from "./consent.ts";
3
+ import { type ConsentGated, whenGranted, withConsent } from "./consent-gate.ts";
4
+ import { reportDevError } from "./dev-log.ts";
5
+
6
+ /** The vendors the banner starts: the analytics settings loaded in the browser. */
7
+ export type ConsentVendors = Pick<
8
+ AnalyticsSettings,
9
+ "meta" | "tiktok" | "snapchat" | "axon" | "drip" | "clarity"
10
+ >;
11
+
12
+ /**
13
+ * A vendor whose code is fetched on its first grant, not with the page. Calls
14
+ * wait on the one import, so they reach the vendor in the order they were made.
15
+ */
16
+ function lazy(load: () => Promise<ConsentGated>): ConsentGated {
17
+ let vendor: Promise<ConsentGated | undefined> | undefined;
18
+ const loaded = () => {
19
+ vendor ??= load().catch((error) => {
20
+ reportDevError("consent vendors", error);
21
+ return undefined;
22
+ });
23
+ return vendor;
24
+ };
25
+ return {
26
+ grant: () => {
27
+ loaded().then((ready) => ready?.grant());
28
+ },
29
+ revoke: () => {
30
+ vendor?.then((ready) => ready?.revoke?.());
31
+ },
32
+ };
33
+ }
34
+
35
+ /**
36
+ * Starts every configured vendor. Nothing vendor-specific is downloaded until
37
+ * its consent category is granted — except Clarity, which runs cookieless
38
+ * without consent and so loads with the page.
39
+ */
40
+ export function startVendors(
41
+ vendors: ConsentVendors,
42
+ signal?: AbortSignal
43
+ ): void {
44
+ const { meta, tiktok, snapchat, axon, drip, clarity } = vendors;
45
+ if (meta !== undefined) {
46
+ whenGranted(
47
+ "marketing",
48
+ lazy(async () =>
49
+ (await import("./meta-pixel.ts")).metaPixel(meta.pixelIds)
50
+ ),
51
+ signal
52
+ );
53
+ }
54
+ if (tiktok !== undefined) {
55
+ whenGranted(
56
+ "marketing",
57
+ lazy(async () =>
58
+ (await import("./tiktok-pixel.ts")).tiktokPixel(tiktok.pixelIds)
59
+ ),
60
+ signal
61
+ );
62
+ }
63
+ if (snapchat !== undefined) {
64
+ whenGranted(
65
+ "marketing",
66
+ lazy(async () =>
67
+ (await import("./snap-pixel.ts")).snapPixel(snapchat.pixelIds)
68
+ ),
69
+ signal
70
+ );
71
+ }
72
+ if (axon !== undefined) {
73
+ whenGranted(
74
+ "marketing",
75
+ lazy(async () =>
76
+ (await import("./axon-pixel.ts")).axonPixel(axon.eventKey)
77
+ ),
78
+ signal
79
+ );
80
+ }
81
+ if (drip !== undefined) {
82
+ whenGranted(
83
+ "marketing",
84
+ lazy(async () => (await import("./drip.ts")).drip(drip.accountId)),
85
+ signal
86
+ );
87
+ }
88
+ if (clarity !== undefined) {
89
+ let apply: Promise<((choices: ConsentChoices) => void) | undefined>;
90
+ withConsent((choices) => {
91
+ apply ??= import("./clarity.ts")
92
+ .then((module) => module.clarity(clarity.projectId))
93
+ .catch((error) => {
94
+ reportDevError("consent vendors", error);
95
+ return undefined;
96
+ });
97
+ apply.then((ready) => ready?.(choices));
98
+ }, signal);
99
+ }
100
+ }
package/src/astro/drip.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import { appendScript } from "./append-script.ts";
2
2
  import type { ConsentGated } from "./consent-gate.ts";
3
- import { whenGranted } from "./consent-gate.ts";
4
- import { element } from "./element.ts";
5
- import { data } from "./ref.ts";
6
3
 
7
4
  /** Drip's globals: a queue of calls, and the account its script reports to. */
8
5
  interface DripWindow {
@@ -30,8 +27,3 @@ export function drip(accountId: string): ConsentGated {
30
27
  },
31
28
  };
32
29
  }
33
-
34
- /** Internal to `Drip.astro`. */
35
- export const dripElement = element("atlas-drip", ({ root, signal }) => {
36
- whenGranted("marketing", drip(data(root, "account-id")), signal);
37
- });
@@ -1,8 +1,5 @@
1
1
  import { appendScript } from "./append-script.ts";
2
2
  import type { ConsentGated } from "./consent-gate.ts";
3
- import { whenGranted } from "./consent-gate.ts";
4
- import { element } from "./element.ts";
5
- import { data } from "./ref.ts";
6
3
 
7
4
  /** Meta's `fbq`: a queue until `fbevents.js` arrives and replays it. */
8
5
  interface Fbq {
@@ -64,15 +61,3 @@ export function metaPixel(pixelIds: readonly string[]): ConsentGated {
64
61
  },
65
62
  };
66
63
  }
67
-
68
- /** Internal to `MetaPixel.astro`. */
69
- export const metaPixelElement = element(
70
- "atlas-meta-pixel",
71
- ({ root, signal }) => {
72
- whenGranted(
73
- "marketing",
74
- metaPixel(JSON.parse(data(root, "pixel-ids"))),
75
- signal
76
- );
77
- }
78
- );
@@ -1,8 +1,5 @@
1
1
  import { appendScript } from "./append-script.ts";
2
2
  import type { ConsentGated } from "./consent-gate.ts";
3
- import { whenGranted } from "./consent-gate.ts";
4
- import { element } from "./element.ts";
5
- import { data } from "./ref.ts";
6
3
 
7
4
  /** Snap's `snaptr`: a queue until `scevent.min.js` arrives and replays it. */
8
5
  interface Snaptr {
@@ -55,15 +52,3 @@ export function snapPixel(pixelIds: readonly string[]): ConsentGated {
55
52
  },
56
53
  };
57
54
  }
58
-
59
- /** Internal to `SnapPixel.astro`. */
60
- export const snapPixelElement = element(
61
- "atlas-snap-pixel",
62
- ({ root, signal }) => {
63
- whenGranted(
64
- "marketing",
65
- snapPixel(JSON.parse(data(root, "pixel-ids"))),
66
- signal
67
- );
68
- }
69
- );
@@ -1,8 +1,5 @@
1
1
  import { appendScript } from "./append-script.ts";
2
2
  import type { ConsentGated } from "./consent-gate.ts";
3
- import { whenGranted } from "./consent-gate.ts";
4
- import { element } from "./element.ts";
5
- import { data } from "./ref.ts";
6
3
 
7
4
  /** TikTok's `ttq`: a queue of calls until `events.js` arrives and replays it. */
8
5
  interface Ttq extends Array<unknown> {
@@ -98,15 +95,3 @@ export function tiktokPixel(pixelIds: readonly string[]): ConsentGated {
98
95
  },
99
96
  };
100
97
  }
101
-
102
- /** Internal to `TikTokPixel.astro`. */
103
- export const tiktokPixelElement = element(
104
- "atlas-tiktok-pixel",
105
- ({ root, signal }) => {
106
- whenGranted(
107
- "marketing",
108
- tiktokPixel(JSON.parse(data(root, "pixel-ids"))),
109
- signal
110
- );
111
- }
112
- );
@@ -1,18 +0,0 @@
1
- ---
2
- /** Internal to `ConsentBanner.astro`, so the pixel's script ships only where it is configured. */
3
- import type { AxonPixelSettings } from "../analytics/axon-pixel.ts";
4
- import AtlasElement from "./AtlasElement.astro";
5
- import { axonPixelElement } from "./axon-pixel.ts";
6
-
7
- interface Props {
8
- readonly settings: AxonPixelSettings;
9
- }
10
- ---
11
-
12
- <script src="./axon-pixel.ts" />
13
-
14
- <AtlasElement
15
- of={axonPixelElement}
16
- data-event-key={Astro.props.settings.eventKey}
17
- hidden
18
- />
@@ -1,18 +0,0 @@
1
- ---
2
- /** Internal to `ConsentBanner.astro`, so Clarity's script ships only where it is configured. */
3
- import type { ClaritySettings } from "../analytics/clarity.ts";
4
- import AtlasElement from "./AtlasElement.astro";
5
- import { clarityElement } from "./clarity.ts";
6
-
7
- interface Props {
8
- readonly settings: ClaritySettings;
9
- }
10
- ---
11
-
12
- <script src="./clarity.ts" />
13
-
14
- <AtlasElement
15
- of={clarityElement}
16
- data-project-id={Astro.props.settings.projectId}
17
- hidden
18
- />
@@ -1,18 +0,0 @@
1
- ---
2
- /** Internal to `ConsentBanner.astro`, so Drip's script ships only where it is configured. */
3
- import type { DripSettings } from "../analytics/drip.ts";
4
- import AtlasElement from "./AtlasElement.astro";
5
- import { dripElement } from "./drip.ts";
6
-
7
- interface Props {
8
- readonly settings: DripSettings;
9
- }
10
- ---
11
-
12
- <script src="./drip.ts" />
13
-
14
- <AtlasElement
15
- of={dripElement}
16
- data-account-id={Astro.props.settings.accountId}
17
- hidden
18
- />
@@ -1,18 +0,0 @@
1
- ---
2
- /** Internal to `ConsentBanner.astro`, so the pixel's script ships only where it is configured. */
3
- import type { MetaPixelSettings } from "../analytics/meta-pixel.ts";
4
- import AtlasElement from "./AtlasElement.astro";
5
- import { metaPixelElement } from "./meta-pixel.ts";
6
-
7
- interface Props {
8
- readonly settings: MetaPixelSettings;
9
- }
10
- ---
11
-
12
- <script src="./meta-pixel.ts" />
13
-
14
- <AtlasElement
15
- of={metaPixelElement}
16
- data-pixel-ids={JSON.stringify(Astro.props.settings.pixelIds)}
17
- hidden
18
- />
@@ -1,18 +0,0 @@
1
- ---
2
- /** Internal to `ConsentBanner.astro`, so the pixel's script ships only where it is configured. */
3
- import type { SnapPixelSettings } from "../analytics/snap-pixel.ts";
4
- import AtlasElement from "./AtlasElement.astro";
5
- import { snapPixelElement } from "./snap-pixel.ts";
6
-
7
- interface Props {
8
- readonly settings: SnapPixelSettings;
9
- }
10
- ---
11
-
12
- <script src="./snap-pixel.ts" />
13
-
14
- <AtlasElement
15
- of={snapPixelElement}
16
- data-pixel-ids={JSON.stringify(Astro.props.settings.pixelIds)}
17
- hidden
18
- />
@@ -1,18 +0,0 @@
1
- ---
2
- /** Internal to `ConsentBanner.astro`, so the pixel's script ships only where it is configured. */
3
- import type { TikTokPixelSettings } from "../analytics/tiktok-pixel.ts";
4
- import AtlasElement from "./AtlasElement.astro";
5
- import { tiktokPixelElement } from "./tiktok-pixel.ts";
6
-
7
- interface Props {
8
- readonly settings: TikTokPixelSettings;
9
- }
10
- ---
11
-
12
- <script src="./tiktok-pixel.ts" />
13
-
14
- <AtlasElement
15
- of={tiktokPixelElement}
16
- data-pixel-ids={JSON.stringify(Astro.props.settings.pixelIds)}
17
- hidden
18
- />