@escape-game-over/atlas 0.1.54 → 0.1.56
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 +1 -1
- package/src/analytics/index.ts +14 -0
- package/src/analytics/meta-pixel.ts +23 -0
- package/src/analytics/tiktok-pixel.ts +23 -0
- package/src/astro/ConsentBanner.astro +10 -4
- package/src/astro/MetaPixel.astro +18 -0
- package/src/astro/TikTokPixel.astro +18 -0
- package/src/astro/append-script.ts +8 -0
- package/src/astro/client.ts +2 -0
- package/src/astro/consent-gate.ts +48 -0
- package/src/astro/google-map.ts +10 -7
- package/src/astro/meta-event.ts +61 -0
- package/src/astro/meta-pixel.ts +74 -0
- package/src/astro/tiktok-event.ts +59 -0
- package/src/astro/tiktok-pixel.ts +112 -0
- package/src/index.ts +2 -0
package/package.json
CHANGED
package/src/analytics/index.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { MetaTag } from "../meta/tag.ts";
|
|
2
2
|
import type { HttpsUrl } from "../url.ts";
|
|
3
3
|
import { type GoogleSettings, googleEmits, googleScripts } from "./google.ts";
|
|
4
|
+
import { checkMetaPixel, type MetaPixelSettings } from "./meta-pixel.ts";
|
|
4
5
|
import type { AnalyticsTags } from "./tags.ts";
|
|
6
|
+
import { checkTikTokPixel, type TikTokPixelSettings } from "./tiktok-pixel.ts";
|
|
5
7
|
import {
|
|
6
8
|
checkUmamiDomains,
|
|
7
9
|
type UmamiSettings,
|
|
@@ -29,7 +31,9 @@ export {
|
|
|
29
31
|
type ConsentState,
|
|
30
32
|
type GoogleSettings,
|
|
31
33
|
} from "./google.ts";
|
|
34
|
+
export type { MetaPixelSettings } from "./meta-pixel.ts";
|
|
32
35
|
export type { AnalyticsTags } from "./tags.ts";
|
|
36
|
+
export type { TikTokPixelSettings } from "./tiktok-pixel.ts";
|
|
33
37
|
export type {
|
|
34
38
|
UmamiReplay,
|
|
35
39
|
UmamiSettings,
|
|
@@ -40,6 +44,10 @@ export type {
|
|
|
40
44
|
export interface AnalyticsSettings {
|
|
41
45
|
readonly umami?: UmamiSettings;
|
|
42
46
|
readonly google?: GoogleSettings;
|
|
47
|
+
/** Loaded by `ConsentBanner`, after marketing is granted. */
|
|
48
|
+
readonly meta?: MetaPixelSettings;
|
|
49
|
+
/** Loaded by `ConsentBanner`, after marketing is granted. */
|
|
50
|
+
readonly tiktok?: TikTokPixelSettings;
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
/**
|
|
@@ -60,6 +68,8 @@ export function analyticsScripts(
|
|
|
60
68
|
if (analytics === undefined) return { head: [], body: [] };
|
|
61
69
|
|
|
62
70
|
checkUmamiDomains(analytics.umami, origin);
|
|
71
|
+
checkMetaPixel(analytics.meta, origin);
|
|
72
|
+
checkTikTokPixel(analytics.tiktok, origin);
|
|
63
73
|
const google = googleScripts(analytics.google, origin);
|
|
64
74
|
return {
|
|
65
75
|
head: [...google.head, ...umamiScripts(analytics.umami)],
|
|
@@ -98,6 +108,10 @@ export function consentVendors(
|
|
|
98
108
|
umami: false,
|
|
99
109
|
// Consent Mode, but only once there is a tag reading it.
|
|
100
110
|
google: googleEmits(analytics.google),
|
|
111
|
+
// No cookieless mode: nothing loads until marketing is granted.
|
|
112
|
+
meta: analytics.meta !== undefined,
|
|
113
|
+
// Its Cookie Consent Mode holds events back; it is not cookieless.
|
|
114
|
+
tiktok: analytics.tiktok !== undefined,
|
|
101
115
|
};
|
|
102
116
|
|
|
103
117
|
return (Object.keys(VENDORS) as (keyof AnalyticsSettings)[]).filter(
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NonEmpty } from "../types.ts";
|
|
2
|
+
import { warn } from "../warn.ts";
|
|
3
|
+
|
|
4
|
+
/** Meta's pixel. Marketing: it loads only once a visitor grants that. */
|
|
5
|
+
export interface MetaPixelSettings {
|
|
6
|
+
/** From Events Manager: 15 or 16 digits. Several share one script. */
|
|
7
|
+
readonly pixelIds: NonEmpty<string>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Warns about an id that is not a pixel id: it would report nowhere. */
|
|
11
|
+
export function checkMetaPixel(
|
|
12
|
+
meta: MetaPixelSettings | undefined,
|
|
13
|
+
at: string
|
|
14
|
+
): void {
|
|
15
|
+
for (const id of meta?.pixelIds ?? []) {
|
|
16
|
+
if (!/^\d{15,16}$/.test(id)) {
|
|
17
|
+
warn(
|
|
18
|
+
at,
|
|
19
|
+
`Meta pixel id "${id}" is not 15 or 16 digits, so it reports nowhere. Copy it from Events Manager.`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NonEmpty } from "../types.ts";
|
|
2
|
+
import { warn } from "../warn.ts";
|
|
3
|
+
|
|
4
|
+
/** TikTok's pixel. Marketing: it loads only once a visitor grants that. */
|
|
5
|
+
export interface TikTokPixelSettings {
|
|
6
|
+
/** From TikTok Events Manager: 20 capital letters and digits. */
|
|
7
|
+
readonly pixelIds: NonEmpty<string>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Warns about an id that is not a pixel id: it would report nowhere. */
|
|
11
|
+
export function checkTikTokPixel(
|
|
12
|
+
tiktok: TikTokPixelSettings | undefined,
|
|
13
|
+
at: string
|
|
14
|
+
): void {
|
|
15
|
+
for (const id of tiktok?.pixelIds ?? []) {
|
|
16
|
+
if (!/^[A-Z0-9]{20}$/.test(id)) {
|
|
17
|
+
warn(
|
|
18
|
+
at,
|
|
19
|
+
`TikTok pixel id "${id}" is not 20 capital letters and digits, so it reports nowhere. Copy it from TikTok Events Manager.`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -5,12 +5,14 @@
|
|
|
5
5
|
* buttons.
|
|
6
6
|
*
|
|
7
7
|
* Renders nothing — and ships no script — when this site's analytics need no
|
|
8
|
-
* permission. Otherwise the banner starts hidden and shows only to a visitor
|
|
8
|
+
* permission. Also loads the vendors that wait for consent, such as Meta's and TikTok's pixels. 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
10
|
*/
|
|
11
11
|
import type { HTMLAttributes } from "astro/types";
|
|
12
12
|
import { type AnalyticsSettings, consentRequired } from "../analytics/index.ts";
|
|
13
13
|
import ConsentElement from "./ConsentElement.astro";
|
|
14
|
+
import MetaPixel from "./MetaPixel.astro";
|
|
15
|
+
import TikTokPixel from "./TikTokPixel.astro";
|
|
14
16
|
|
|
15
17
|
type Props = HTMLAttributes<"div"> & {
|
|
16
18
|
readonly analytics: AnalyticsSettings | undefined;
|
|
@@ -20,7 +22,11 @@ const { analytics, ...attrs } = Astro.props;
|
|
|
20
22
|
---
|
|
21
23
|
|
|
22
24
|
{consentRequired(analytics) && (
|
|
23
|
-
|
|
24
|
-
<
|
|
25
|
-
|
|
25
|
+
<>
|
|
26
|
+
<ConsentElement {...attrs}>
|
|
27
|
+
<slot />
|
|
28
|
+
</ConsentElement>
|
|
29
|
+
{analytics?.meta && <MetaPixel settings={analytics.meta} />}
|
|
30
|
+
{analytics?.tiktok && <TikTokPixel settings={analytics.tiktok} />}
|
|
31
|
+
</>
|
|
26
32
|
)}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
/>
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
/>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Fetches a vendor's script without blocking the page. */
|
|
2
|
+
export function appendScript(src: string): HTMLScriptElement {
|
|
3
|
+
const script = document.createElement("script");
|
|
4
|
+
script.async = true;
|
|
5
|
+
script.src = src;
|
|
6
|
+
document.head.append(script);
|
|
7
|
+
return script;
|
|
8
|
+
}
|
package/src/astro/client.ts
CHANGED
|
@@ -23,6 +23,8 @@ export {
|
|
|
23
23
|
} from "./element.ts";
|
|
24
24
|
export * from "./filters.ts";
|
|
25
25
|
export * from "./filters-view.ts";
|
|
26
|
+
export * from "./meta-event.ts";
|
|
26
27
|
export { data, ref, refs } from "./ref.ts";
|
|
28
|
+
export * from "./tiktok-event.ts";
|
|
27
29
|
export * from "./umami-event.ts";
|
|
28
30
|
export * from "./youtube.ts";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONSENT_EVENT,
|
|
3
|
+
type ConsentCategory,
|
|
4
|
+
type ConsentChoices,
|
|
5
|
+
readConsent,
|
|
6
|
+
} from "./consent.ts";
|
|
7
|
+
|
|
8
|
+
/** What a vendor does as its category is granted and withdrawn. */
|
|
9
|
+
export interface ConsentGated {
|
|
10
|
+
/** After `load`, each time the category is granted: a stored answer, or one given now. */
|
|
11
|
+
readonly grant: () => void;
|
|
12
|
+
/** Each time a grant is withdrawn on this page. */
|
|
13
|
+
readonly revoke?: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Runs `vendor` as the visitor's answer for `category` says, until `signal` aborts. */
|
|
17
|
+
export function whenGranted(
|
|
18
|
+
category: ConsentCategory,
|
|
19
|
+
vendor: ConsentGated,
|
|
20
|
+
signal?: AbortSignal
|
|
21
|
+
): void {
|
|
22
|
+
let granted = readConsent()?.[category] === "granted";
|
|
23
|
+
|
|
24
|
+
// After `load`, so a vendor does not compete with the page. Checked again
|
|
25
|
+
// then: an answer withdrawn before `load` cancels the grant.
|
|
26
|
+
const grantAfterLoad = () => {
|
|
27
|
+
const run = () => {
|
|
28
|
+
if (granted) vendor.grant();
|
|
29
|
+
};
|
|
30
|
+
if (document.readyState === "complete") run();
|
|
31
|
+
else addEventListener("load", run, { once: true, signal });
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
if (granted) grantAfterLoad();
|
|
35
|
+
|
|
36
|
+
addEventListener(
|
|
37
|
+
CONSENT_EVENT,
|
|
38
|
+
(event) => {
|
|
39
|
+
const choices = (event as CustomEvent<ConsentChoices>).detail;
|
|
40
|
+
const now = choices[category] === "granted";
|
|
41
|
+
if (now === granted) return;
|
|
42
|
+
granted = now;
|
|
43
|
+
if (now) grantAfterLoad();
|
|
44
|
+
else vendor.revoke?.();
|
|
45
|
+
},
|
|
46
|
+
{ signal }
|
|
47
|
+
);
|
|
48
|
+
}
|
package/src/astro/google-map.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="google.maps" />
|
|
2
2
|
import type { MapFrame } from "../map.ts";
|
|
3
|
+
import { appendScript } from "./append-script.ts";
|
|
3
4
|
import { reportDevError } from "./dev-log.ts";
|
|
4
5
|
import { element } from "./element.ts";
|
|
5
6
|
import { data } from "./ref.ts";
|
|
@@ -33,17 +34,19 @@ export function loadGoogleMaps(apiKey: string): Promise<typeof google.maps> {
|
|
|
33
34
|
loading ??= new Promise((resolve, reject) => {
|
|
34
35
|
(window as unknown as Record<string, () => void>)[CALLBACK] = () =>
|
|
35
36
|
resolve(google.maps);
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const params = {
|
|
38
|
+
key: apiKey,
|
|
39
|
+
v: "weekly",
|
|
40
|
+
loading: "async",
|
|
41
|
+
callback: CALLBACK,
|
|
42
|
+
};
|
|
43
|
+
appendScript(
|
|
44
|
+
`https://maps.googleapis.com/maps/api/js?${new URLSearchParams(params)}`
|
|
45
|
+
).onerror = () => {
|
|
42
46
|
// So the next click tries again.
|
|
43
47
|
loading = undefined;
|
|
44
48
|
reject(new Error("the Maps JavaScript API failed to load"));
|
|
45
49
|
};
|
|
46
|
-
document.head.append(script);
|
|
47
50
|
});
|
|
48
51
|
return loading;
|
|
49
52
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { CurrencyCode } from "../money.ts";
|
|
2
|
+
|
|
3
|
+
/** Meta's standard events, which its ad optimisation understands by name. */
|
|
4
|
+
export type MetaStandardEvent =
|
|
5
|
+
| "AddPaymentInfo"
|
|
6
|
+
| "AddToCart"
|
|
7
|
+
| "AddToWishlist"
|
|
8
|
+
| "CompleteRegistration"
|
|
9
|
+
| "Contact"
|
|
10
|
+
| "CustomizeProduct"
|
|
11
|
+
| "Donate"
|
|
12
|
+
| "FindLocation"
|
|
13
|
+
| "InitiateCheckout"
|
|
14
|
+
| "Lead"
|
|
15
|
+
| "Purchase"
|
|
16
|
+
| "Schedule"
|
|
17
|
+
| "Search"
|
|
18
|
+
| "StartTrial"
|
|
19
|
+
| "SubmitApplication"
|
|
20
|
+
| "Subscribe"
|
|
21
|
+
| "ViewContent";
|
|
22
|
+
|
|
23
|
+
/** Meta's standard parameters. What the visitor chose, never who they are. */
|
|
24
|
+
export interface MetaEventData {
|
|
25
|
+
readonly value?: number;
|
|
26
|
+
readonly currency?: CurrencyCode;
|
|
27
|
+
readonly content_name?: string;
|
|
28
|
+
readonly content_category?: string;
|
|
29
|
+
readonly content_ids?: readonly string[];
|
|
30
|
+
readonly content_type?: "product" | "product_group";
|
|
31
|
+
readonly num_items?: number;
|
|
32
|
+
readonly search_string?: string;
|
|
33
|
+
readonly status?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `Purchase` is the one event Meta rejects without a value and currency. */
|
|
37
|
+
type MetaEventArgs<E extends MetaStandardEvent> = E extends "Purchase"
|
|
38
|
+
? [data: MetaEventData & { value: number; currency: CurrencyCode }]
|
|
39
|
+
: [data?: MetaEventData];
|
|
40
|
+
|
|
41
|
+
/** What `meta-pixel.ts` defines once marketing is granted. */
|
|
42
|
+
interface MetaWindow {
|
|
43
|
+
fbq?: (...args: unknown[]) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Records a standard event with Meta's pixel — a lead sent, a booking made.
|
|
48
|
+
*
|
|
49
|
+
* `fbq` exists only once the visitor granted marketing and the pixel loaded.
|
|
50
|
+
* Before that, without consent, or where Meta is not configured, the event is
|
|
51
|
+
* dropped: no consent means nothing to send, and an analytics call must never
|
|
52
|
+
* be the reason a page misbehaves.
|
|
53
|
+
*/
|
|
54
|
+
export function metaEvent<E extends MetaStandardEvent>(
|
|
55
|
+
event: E,
|
|
56
|
+
...[data]: MetaEventArgs<E>
|
|
57
|
+
): void {
|
|
58
|
+
const fbq = (window as unknown as MetaWindow).fbq;
|
|
59
|
+
if (data === undefined) fbq?.("track", event);
|
|
60
|
+
else fbq?.("track", event, data);
|
|
61
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { appendScript } from "./append-script.ts";
|
|
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
|
+
|
|
7
|
+
/** Meta's `fbq`: a queue until `fbevents.js` arrives and replays it. */
|
|
8
|
+
interface Fbq {
|
|
9
|
+
(...args: unknown[]): void;
|
|
10
|
+
callMethod?: (...args: unknown[]) => void;
|
|
11
|
+
queue: unknown[];
|
|
12
|
+
push: Fbq;
|
|
13
|
+
loaded: boolean;
|
|
14
|
+
version: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface PixelWindow {
|
|
18
|
+
fbq?: Fbq;
|
|
19
|
+
_fbq?: Fbq;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const SCRIPT = "https://connect.facebook.net/en_US/fbevents.js";
|
|
23
|
+
|
|
24
|
+
/** Meta's own base code, as it defines `fbq` before the script loads. */
|
|
25
|
+
function defineFbq(): Fbq {
|
|
26
|
+
const global = window as unknown as PixelWindow;
|
|
27
|
+
if (global.fbq !== undefined) return global.fbq;
|
|
28
|
+
const fbq = function () {
|
|
29
|
+
// biome-ignore lint/complexity/noArguments: Meta's queue takes the arguments object, as its snippet pushes it.
|
|
30
|
+
if (fbq.callMethod) Reflect.apply(fbq.callMethod, fbq, arguments);
|
|
31
|
+
// biome-ignore lint/complexity/noArguments: as above.
|
|
32
|
+
else fbq.queue.push(arguments);
|
|
33
|
+
} as Fbq;
|
|
34
|
+
fbq.push = fbq;
|
|
35
|
+
fbq.loaded = true;
|
|
36
|
+
fbq.version = "2.0";
|
|
37
|
+
fbq.queue = [];
|
|
38
|
+
global.fbq = fbq;
|
|
39
|
+
global._fbq ??= fbq;
|
|
40
|
+
return fbq;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The pixel: loaded on the first grant, paused and resumed by later answers. */
|
|
44
|
+
export function metaPixel(pixelIds: readonly string[]): ConsentGated {
|
|
45
|
+
let loaded = false;
|
|
46
|
+
return {
|
|
47
|
+
grant: () => {
|
|
48
|
+
const fbq = defineFbq();
|
|
49
|
+
if (loaded) {
|
|
50
|
+
fbq("consent", "grant");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
loaded = true;
|
|
54
|
+
for (const id of pixelIds) fbq("init", id);
|
|
55
|
+
fbq("track", "PageView");
|
|
56
|
+
appendScript(SCRIPT);
|
|
57
|
+
},
|
|
58
|
+
revoke: () => {
|
|
59
|
+
(window as unknown as PixelWindow).fbq?.("consent", "revoke");
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Internal to `MetaPixel.astro`. */
|
|
65
|
+
export const metaPixelElement = element(
|
|
66
|
+
"atlas-meta-pixel",
|
|
67
|
+
({ root, signal }) => {
|
|
68
|
+
whenGranted(
|
|
69
|
+
"marketing",
|
|
70
|
+
metaPixel(JSON.parse(data(root, "pixel-ids"))),
|
|
71
|
+
signal
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { CurrencyCode } from "../money.ts";
|
|
2
|
+
|
|
3
|
+
/** TikTok's standard events, by their current names (May 2025 onwards). */
|
|
4
|
+
export type TikTokStandardEvent =
|
|
5
|
+
| "AddPaymentInfo"
|
|
6
|
+
| "AddToCart"
|
|
7
|
+
| "AddToWishlist"
|
|
8
|
+
| "CompleteRegistration"
|
|
9
|
+
| "Contact"
|
|
10
|
+
| "Download"
|
|
11
|
+
| "InitiateCheckout"
|
|
12
|
+
| "Lead"
|
|
13
|
+
| "Login"
|
|
14
|
+
| "Purchase"
|
|
15
|
+
| "Search"
|
|
16
|
+
| "StartTrial"
|
|
17
|
+
| "SubmitApplication"
|
|
18
|
+
| "Subscribe"
|
|
19
|
+
| "ViewContent";
|
|
20
|
+
|
|
21
|
+
/** TikTok's standard parameters. What the visitor chose, never who they are. */
|
|
22
|
+
export interface TikTokEventData {
|
|
23
|
+
readonly value?: number;
|
|
24
|
+
readonly currency?: CurrencyCode;
|
|
25
|
+
readonly content_id?: string;
|
|
26
|
+
readonly content_type?: "product" | "product_group";
|
|
27
|
+
readonly content_name?: string;
|
|
28
|
+
readonly content_category?: string;
|
|
29
|
+
readonly quantity?: number;
|
|
30
|
+
readonly query?: string;
|
|
31
|
+
readonly description?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `Purchase` needs a value and currency to be worth anything to TikTok. */
|
|
35
|
+
type TikTokEventArgs<E extends TikTokStandardEvent> = E extends "Purchase"
|
|
36
|
+
? [data: TikTokEventData & { value: number; currency: CurrencyCode }]
|
|
37
|
+
: [data?: TikTokEventData];
|
|
38
|
+
|
|
39
|
+
/** What `tiktok-pixel.ts` defines once marketing is granted. */
|
|
40
|
+
interface TikTokWindow {
|
|
41
|
+
ttq?: { track(event: string, data?: object): void };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Records a standard event with TikTok's pixel — a lead sent, a booking made.
|
|
46
|
+
*
|
|
47
|
+
* `ttq` exists only once the visitor granted marketing and the pixel loaded.
|
|
48
|
+
* Before that, without consent, or where TikTok is not configured, the event is
|
|
49
|
+
* dropped: no consent means nothing to send, and an analytics call must never
|
|
50
|
+
* be the reason a page misbehaves.
|
|
51
|
+
*/
|
|
52
|
+
export function tiktokEvent<E extends TikTokStandardEvent>(
|
|
53
|
+
event: E,
|
|
54
|
+
...[data]: TikTokEventArgs<E>
|
|
55
|
+
): void {
|
|
56
|
+
const ttq = (window as unknown as TikTokWindow).ttq;
|
|
57
|
+
if (data === undefined) ttq?.track(event);
|
|
58
|
+
else ttq?.track(event, data);
|
|
59
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { appendScript } from "./append-script.ts";
|
|
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
|
+
|
|
7
|
+
/** TikTok's `ttq`: a queue of calls until `events.js` arrives and replays it. */
|
|
8
|
+
interface Ttq extends Array<unknown> {
|
|
9
|
+
methods: readonly string[];
|
|
10
|
+
setAndDefer(target: unknown[], method: string): void;
|
|
11
|
+
instance(id: string): unknown[];
|
|
12
|
+
load(id: string, options?: object): void;
|
|
13
|
+
_i: Record<string, unknown[] & { _u?: string }>;
|
|
14
|
+
_t: Record<string, number>;
|
|
15
|
+
_o: Record<string, object>;
|
|
16
|
+
[method: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface PixelWindow {
|
|
20
|
+
ttq?: Ttq;
|
|
21
|
+
TiktokAnalyticsObject?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const SCRIPT = "https://analytics.tiktok.com/i18n/pixel/events.js";
|
|
25
|
+
|
|
26
|
+
const METHODS = [
|
|
27
|
+
"page",
|
|
28
|
+
"track",
|
|
29
|
+
"identify",
|
|
30
|
+
"instances",
|
|
31
|
+
"debug",
|
|
32
|
+
"on",
|
|
33
|
+
"off",
|
|
34
|
+
"once",
|
|
35
|
+
"ready",
|
|
36
|
+
"alias",
|
|
37
|
+
"group",
|
|
38
|
+
"enableCookie",
|
|
39
|
+
"disableCookie",
|
|
40
|
+
"holdConsent",
|
|
41
|
+
"revokeConsent",
|
|
42
|
+
"grantConsent",
|
|
43
|
+
] as const;
|
|
44
|
+
|
|
45
|
+
/** TikTok's own base code, as it defines `ttq` before the script loads. */
|
|
46
|
+
function defineTtq(): Ttq {
|
|
47
|
+
const global = window as unknown as PixelWindow;
|
|
48
|
+
if (global.ttq !== undefined) return global.ttq;
|
|
49
|
+
global.TiktokAnalyticsObject = "ttq";
|
|
50
|
+
const ttq = [] as unknown as Ttq;
|
|
51
|
+
ttq.methods = METHODS;
|
|
52
|
+
ttq.setAndDefer = (target, method) => {
|
|
53
|
+
(target as unknown as Record<string, unknown>)[method] = (
|
|
54
|
+
...args: unknown[]
|
|
55
|
+
) => target.push([method, ...args]);
|
|
56
|
+
};
|
|
57
|
+
for (const method of METHODS) ttq.setAndDefer(ttq, method);
|
|
58
|
+
ttq.instance = (id) => {
|
|
59
|
+
const queue = ttq._i?.[id] ?? [];
|
|
60
|
+
for (const method of METHODS) ttq.setAndDefer(queue, method);
|
|
61
|
+
return queue;
|
|
62
|
+
};
|
|
63
|
+
ttq.load = (id, options = {}) => {
|
|
64
|
+
ttq._i ??= {};
|
|
65
|
+
ttq._i[id] = Object.assign([], { _u: SCRIPT });
|
|
66
|
+
ttq._t ??= {};
|
|
67
|
+
ttq._t[id] = Date.now();
|
|
68
|
+
ttq._o ??= {};
|
|
69
|
+
ttq._o[id] = options;
|
|
70
|
+
appendScript(
|
|
71
|
+
`${SCRIPT}?${new URLSearchParams({ sdkid: id, lib: "ttq" })}`
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
global.ttq = ttq;
|
|
75
|
+
return ttq;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const call = (ttq: Ttq, method: (typeof METHODS)[number], ...args: unknown[]) =>
|
|
79
|
+
(ttq[method] as (...args: unknown[]) => void)(...args);
|
|
80
|
+
|
|
81
|
+
/** The pixel: loaded on the first grant, paused and resumed by later answers. */
|
|
82
|
+
export function tiktokPixel(pixelIds: readonly string[]): ConsentGated {
|
|
83
|
+
let loaded = false;
|
|
84
|
+
return {
|
|
85
|
+
grant: () => {
|
|
86
|
+
const ttq = defineTtq();
|
|
87
|
+
// Also lifts the hold of TikTok's Cookie Consent Mode, where a
|
|
88
|
+
// pixel has it on in Events Manager.
|
|
89
|
+
call(ttq, "grantConsent");
|
|
90
|
+
if (loaded) return;
|
|
91
|
+
loaded = true;
|
|
92
|
+
for (const id of pixelIds) ttq.load(id);
|
|
93
|
+
call(ttq, "page");
|
|
94
|
+
},
|
|
95
|
+
revoke: () => {
|
|
96
|
+
const ttq = (window as unknown as PixelWindow).ttq;
|
|
97
|
+
if (ttq !== undefined) call(ttq, "revokeConsent");
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
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
|
+
);
|