@fire-spark/analytics-tiktok 0.18.0 → 0.19.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 +14 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +133 -37
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @fire-spark/analytics-tiktok
|
|
2
|
+
|
|
3
|
+
TikTok destination for `@fire-spark/analytics`: TikTok Pixel on web,
|
|
4
|
+
TikTok Business SDK in React Native.
|
|
5
|
+
|
|
6
|
+
```typescript
|
|
7
|
+
import { tiktok } from "@fire-spark/analytics-tiktok";
|
|
8
|
+
|
|
9
|
+
tiktok({ pixelId: "CXXXXXXXXXXXXXXX" }); // web
|
|
10
|
+
tiktok({ logEvent, identify, logout, setConsent }); // React Native
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Pass it to `adapters` in `configure()`. Options and behavior:
|
|
14
|
+
https://firespark.cloud/docs/guides/analytics/destinations#tiktok
|
package/dist/index.d.ts
CHANGED
|
@@ -2,8 +2,16 @@ import { AnalyticsAdapter } from "@fire-spark/analytics";
|
|
|
2
2
|
//#region src/index.d.ts
|
|
3
3
|
type TikTokLogEvent = (name: string, params?: Record<string, unknown>) => Promise<void> | void;
|
|
4
4
|
type TikTokOptions = {
|
|
5
|
+
/** Web: TikTok Pixel id. Events are sent to this pixel's instance. */
|
|
5
6
|
pixelId?: string;
|
|
7
|
+
/** React Native: TikTok Business SDK track call. */
|
|
6
8
|
logEvent?: TikTokLogEvent;
|
|
9
|
+
/** React Native: TikTok Business SDK identify (hashed user data). */
|
|
10
|
+
identify?: (userData: Record<string, string>) => Promise<void> | void;
|
|
11
|
+
/** React Native: TikTok Business SDK logout (user cleared). */
|
|
12
|
+
logout?: () => Promise<void> | void;
|
|
13
|
+
/** React Native: consent handling of the TikTok Business SDK. */
|
|
14
|
+
setConsent?: (granted: boolean) => Promise<void> | void;
|
|
7
15
|
};
|
|
8
16
|
declare const tiktok: (options?: TikTokOptions) => AnalyticsAdapter;
|
|
9
17
|
//#endregion
|
package/dist/index.js
CHANGED
|
@@ -1,53 +1,149 @@
|
|
|
1
|
+
import { compactPayload, loadScript } from "@fire-spark/analytics";
|
|
1
2
|
//#region src/index.ts
|
|
2
|
-
const
|
|
3
|
+
const EVENTS_SRC = "https://analytics.tiktok.com/i18n/pixel/events.js";
|
|
4
|
+
const SCRIPT_ID = "firespark-tiktok-ttq";
|
|
5
|
+
const METHODS = [
|
|
6
|
+
"page",
|
|
7
|
+
"track",
|
|
8
|
+
"identify",
|
|
9
|
+
"instances",
|
|
10
|
+
"debug",
|
|
11
|
+
"on",
|
|
12
|
+
"off",
|
|
13
|
+
"once",
|
|
14
|
+
"ready",
|
|
15
|
+
"alias",
|
|
16
|
+
"group",
|
|
17
|
+
"enableCookie",
|
|
18
|
+
"disableCookie",
|
|
19
|
+
"holdConsent",
|
|
20
|
+
"revokeConsent",
|
|
21
|
+
"grantConsent"
|
|
22
|
+
];
|
|
23
|
+
/** Stubs installed by this adapter: their events.js loads are ours. */
|
|
24
|
+
const ownStubs = /* @__PURE__ */ new WeakSet();
|
|
25
|
+
const browser = () => {
|
|
3
26
|
const root = globalThis;
|
|
4
|
-
if (root.
|
|
5
|
-
return
|
|
27
|
+
if (typeof root.document?.createElement !== "function") return null;
|
|
28
|
+
return root.window ?? root;
|
|
6
29
|
};
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
30
|
+
const defer = (target, name) => {
|
|
31
|
+
target[name] = (...args) => {
|
|
32
|
+
target.push([name, ...args]);
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/** The official base code; `load` injects events.js through `loadScript`. */
|
|
36
|
+
const installStub = (win) => {
|
|
37
|
+
if (win.ttq) return win.ttq;
|
|
38
|
+
win.TiktokAnalyticsObject = "ttq";
|
|
39
|
+
const ttq = [];
|
|
40
|
+
ttq.methods = METHODS;
|
|
41
|
+
for (const name of METHODS) defer(ttq, name);
|
|
42
|
+
ttq.instance = (pixelId) => {
|
|
43
|
+
const queue = ttq._i?.[pixelId] ?? [];
|
|
44
|
+
for (const name of METHODS) defer(queue, name);
|
|
45
|
+
return queue;
|
|
46
|
+
};
|
|
47
|
+
ttq.load = (pixelId, options = {}) => {
|
|
48
|
+
const queue = [];
|
|
49
|
+
queue._u = EVENTS_SRC;
|
|
50
|
+
ttq._i = {
|
|
51
|
+
...ttq._i,
|
|
52
|
+
[pixelId]: queue
|
|
53
|
+
};
|
|
54
|
+
ttq._t = {
|
|
55
|
+
...ttq._t,
|
|
56
|
+
[pixelId]: Date.now()
|
|
57
|
+
};
|
|
58
|
+
ttq._o = {
|
|
59
|
+
...ttq._o,
|
|
60
|
+
[pixelId]: options
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
win.ttq = ttq;
|
|
64
|
+
ownStubs.add(ttq);
|
|
65
|
+
return ttq;
|
|
19
66
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (!pixelId
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
await injectScript(src, "firespark-tiktok-ttq");
|
|
29
|
-
const ttq = existingTtq();
|
|
30
|
-
ttq?.load?.(pixelId);
|
|
31
|
-
return ttq;
|
|
32
|
-
})();
|
|
33
|
-
try {
|
|
34
|
-
return await loading;
|
|
35
|
-
} finally {
|
|
36
|
-
loading = null;
|
|
37
|
-
}
|
|
67
|
+
/**
|
|
68
|
+
* Registers the pixel once and loads its events.js. A failed load is
|
|
69
|
+
* reported, the script removed, and the next event retries it.
|
|
70
|
+
*/
|
|
71
|
+
const pixelReady = (ttq, pixelId) => {
|
|
72
|
+
if (!ttq._i?.[pixelId]) ttq.load?.(pixelId);
|
|
73
|
+
if (!ownStubs.has(ttq)) return Promise.resolve();
|
|
74
|
+
return loadScript(`${EVENTS_SRC}?sdkid=${encodeURIComponent(pixelId)}&lib=ttq`, `${SCRIPT_ID}-${pixelId}`) ?? Promise.resolve();
|
|
38
75
|
};
|
|
39
76
|
const tiktok = (options = {}) => {
|
|
77
|
+
let identified = "{}";
|
|
78
|
+
let appliedUserId = null;
|
|
79
|
+
const pixel = () => {
|
|
80
|
+
const win = browser();
|
|
81
|
+
if (!win) return null;
|
|
82
|
+
const ttq = installStub(win);
|
|
83
|
+
const id = options.pixelId;
|
|
84
|
+
if (!id) return {
|
|
85
|
+
target: ttq,
|
|
86
|
+
ready: Promise.resolve()
|
|
87
|
+
};
|
|
88
|
+
const loading = pixelReady(ttq, id);
|
|
89
|
+
return {
|
|
90
|
+
target: ttq.instance?.(id) ?? ttq,
|
|
91
|
+
ready: loading
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
const sendWeb = async (event) => {
|
|
95
|
+
const page = pixel();
|
|
96
|
+
if (!page) return;
|
|
97
|
+
const target = page.target;
|
|
98
|
+
const userData = event.userData ?? {};
|
|
99
|
+
const key = JSON.stringify(userData);
|
|
100
|
+
if (key !== identified && Object.keys(userData).length > 0) {
|
|
101
|
+
target.identify?.(userData);
|
|
102
|
+
identified = key;
|
|
103
|
+
}
|
|
104
|
+
const opts = event.eventId ? { event_id: event.eventId } : {};
|
|
105
|
+
target.track?.(event.name, event.params, opts);
|
|
106
|
+
await page.ready;
|
|
107
|
+
};
|
|
108
|
+
const syncNativeUser = async (userId) => {
|
|
109
|
+
if (userId === appliedUserId) return;
|
|
110
|
+
appliedUserId = userId;
|
|
111
|
+
if (userId) {
|
|
112
|
+
await options.identify?.({ external_id: userId });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
await options.logout?.();
|
|
116
|
+
};
|
|
40
117
|
return {
|
|
41
118
|
provider: "tiktok",
|
|
42
119
|
send: async (event) => {
|
|
43
120
|
if (event.runtime === "react-native") {
|
|
44
121
|
if (!options.logEvent) return;
|
|
45
|
-
await
|
|
122
|
+
await syncNativeUser(event.user?.id ?? null);
|
|
123
|
+
await options.logEvent(event.name, compactPayload(event.params));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
await sendWeb(event);
|
|
127
|
+
},
|
|
128
|
+
setUser: async (user, runtime) => {
|
|
129
|
+
if (runtime === "react-native") {
|
|
130
|
+
await syncNativeUser(user.id ?? null);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (!user.id) return;
|
|
134
|
+
const target = pixel()?.target;
|
|
135
|
+
const userData = { external_id: user.id };
|
|
136
|
+
target?.identify?.(userData);
|
|
137
|
+
identified = JSON.stringify(userData);
|
|
138
|
+
},
|
|
139
|
+
setConsent: async (consent, runtime) => {
|
|
140
|
+
if (runtime === "react-native") {
|
|
141
|
+
await options.setConsent?.(consent.ads);
|
|
46
142
|
return;
|
|
47
143
|
}
|
|
48
|
-
const
|
|
49
|
-
if (!
|
|
50
|
-
|
|
144
|
+
const win = browser();
|
|
145
|
+
if (!win) return;
|
|
146
|
+
installStub(win)[consent.ads ? "grantConsent" : "revokeConsent"]?.();
|
|
51
147
|
}
|
|
52
148
|
};
|
|
53
149
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fire-spark/analytics-tiktok",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "Fire Spark TikTok analytics adapter (web ttq + RN inject)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -17,13 +17,14 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@fire-spark/analytics": "^0.
|
|
20
|
+
"@fire-spark/analytics": "^0.19.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
+
"jsdom": "^22.1.0",
|
|
23
24
|
"tsdown": "^0.22.14",
|
|
24
25
|
"typescript": "^7.0.2",
|
|
25
26
|
"vitest": "^2.1.8",
|
|
26
|
-
"@fire-spark/analytics": "0.
|
|
27
|
+
"@fire-spark/analytics": "0.19.0"
|
|
27
28
|
},
|
|
28
29
|
"publishConfig": {
|
|
29
30
|
"access": "public"
|