@adhub-so/adhub-nextjs-sdk 0.3.0 → 0.4.1
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 +25 -0
- package/dist/chunk-NF56G46G.js +140 -0
- package/dist/chunk-NF56G46G.js.map +1 -0
- package/dist/index.cjs +112 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +112 -6
- package/dist/index.js.map +1 -1
- package/dist/server.cjs +117 -36
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +5 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.js +117 -36
- package/dist/server.js.map +1 -1
- package/dist/testing.cjs +149 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +6 -0
- package/dist/testing.d.ts +6 -0
- package/dist/testing.js +72 -0
- package/dist/testing.js.map +1 -0
- package/dist/types-DMUJO8Kj.d.cts +62 -0
- package/dist/types-DMUJO8Kj.d.ts +62 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,6 +13,7 @@ function hashKey(input) {
|
|
|
13
13
|
}
|
|
14
14
|
return (hash >>> 0).toString(36);
|
|
15
15
|
}
|
|
16
|
+
var ANONYMOUS_ID_COOKIE_MAX_AGE_SECONDS = 60 * 60 * 24 * 395;
|
|
16
17
|
function validAnonymousId(value) {
|
|
17
18
|
return value !== null && value.length >= 8 && value.length <= 255 && !/\s/.test(value);
|
|
18
19
|
}
|
|
@@ -34,6 +35,27 @@ function readAdHubAnonymousIdFromCookie(cookieHeader, projectKey) {
|
|
|
34
35
|
}
|
|
35
36
|
return null;
|
|
36
37
|
}
|
|
38
|
+
function cookieDomainForHost(domain, hostname) {
|
|
39
|
+
const candidate = domain?.trim().toLowerCase().replace(/^\./, "").replace(/\.$/, "");
|
|
40
|
+
const host = hostname?.trim().toLowerCase().replace(/\.$/, "");
|
|
41
|
+
if (!candidate || !host) return null;
|
|
42
|
+
if (!/^[a-z0-9-]+(\.[a-z0-9-]+)+$/.test(candidate)) return null;
|
|
43
|
+
if (/^[\d.]+$/.test(candidate)) return null;
|
|
44
|
+
return host === candidate || host.endsWith(`.${candidate}`) ? candidate : null;
|
|
45
|
+
}
|
|
46
|
+
function serializeAdHubAnonymousIdCookie(input) {
|
|
47
|
+
const parts = [
|
|
48
|
+
`${getAdHubAnonymousIdCookieName(input.projectKey)}=${encodeURIComponent(input.anonymousId)}`
|
|
49
|
+
];
|
|
50
|
+
if (input.domain) parts.push(`Domain=${input.domain}`);
|
|
51
|
+
parts.push(
|
|
52
|
+
"Path=/",
|
|
53
|
+
`Max-Age=${input.maxAgeSeconds ?? ANONYMOUS_ID_COOKIE_MAX_AGE_SECONDS}`,
|
|
54
|
+
"SameSite=Lax"
|
|
55
|
+
);
|
|
56
|
+
if (input.secure) parts.push("Secure");
|
|
57
|
+
return parts.join("; ");
|
|
58
|
+
}
|
|
37
59
|
|
|
38
60
|
// src/utils.ts
|
|
39
61
|
var MAX_PARAM_LENGTH = 2048;
|
|
@@ -243,7 +265,6 @@ function sanitizeProfile(profile) {
|
|
|
243
265
|
}
|
|
244
266
|
|
|
245
267
|
// src/storage.ts
|
|
246
|
-
var COOKIE_MAX_AGE_SECONDS = 60 * 60 * 24 * 395;
|
|
247
268
|
var MAX_QUEUE_SIZE = 100;
|
|
248
269
|
function isPendingRequest(value) {
|
|
249
270
|
if (!value || typeof value !== "object") return false;
|
|
@@ -252,6 +273,8 @@ function isPendingRequest(value) {
|
|
|
252
273
|
}
|
|
253
274
|
var BrowserStore = class {
|
|
254
275
|
constructor(projectKey) {
|
|
276
|
+
this.cookieDomain = null;
|
|
277
|
+
this.projectKey = projectKey;
|
|
255
278
|
this.suffix = getAdHubAnonymousIdCookieName(projectKey).slice("adhub_project_anonymous_id_".length);
|
|
256
279
|
}
|
|
257
280
|
get anonymousKey() {
|
|
@@ -263,6 +286,9 @@ var BrowserStore = class {
|
|
|
263
286
|
get profileKey() {
|
|
264
287
|
return `adhub_project_profile_id_${this.suffix}`;
|
|
265
288
|
}
|
|
289
|
+
get cookieDomainKey() {
|
|
290
|
+
return `adhub_project_cookie_domain_${this.suffix}`;
|
|
291
|
+
}
|
|
266
292
|
get sessionKey() {
|
|
267
293
|
return `adhub_project_session_${this.suffix}`;
|
|
268
294
|
}
|
|
@@ -272,11 +298,48 @@ var BrowserStore = class {
|
|
|
272
298
|
const fromLocal = this.readLocal(this.anonymousKey);
|
|
273
299
|
return this.validAnonymousId(fromLocal) ? fromLocal : null;
|
|
274
300
|
}
|
|
301
|
+
/**
|
|
302
|
+
* Shares the anonymous-id cookie with every subdomain of `domain`.
|
|
303
|
+
* Returns false, and keeps the host-only cookie, when this page is not on that domain.
|
|
304
|
+
*/
|
|
305
|
+
setCookieDomain(domain) {
|
|
306
|
+
let hostname = null;
|
|
307
|
+
try {
|
|
308
|
+
hostname = window.location.hostname;
|
|
309
|
+
} catch {
|
|
310
|
+
hostname = null;
|
|
311
|
+
}
|
|
312
|
+
this.cookieDomain = cookieDomainForHost(domain, hostname);
|
|
313
|
+
return this.cookieDomain !== null;
|
|
314
|
+
}
|
|
275
315
|
writeAnonymousId(anonymousId) {
|
|
276
316
|
this.writeLocal(this.anonymousKey, anonymousId);
|
|
277
317
|
try {
|
|
278
|
-
const
|
|
279
|
-
|
|
318
|
+
const stored = this.readCookieValues(this.anonymousKey);
|
|
319
|
+
const unchanged = stored.length > 0 && stored.every((value) => value === anonymousId);
|
|
320
|
+
const domainApplied = (this.readLocal(this.cookieDomainKey) || null) === this.cookieDomain;
|
|
321
|
+
if (unchanged && domainApplied) return;
|
|
322
|
+
const secure = window.location.protocol === "https:";
|
|
323
|
+
const write = (domain, maxAgeSeconds = ANONYMOUS_ID_COOKIE_MAX_AGE_SECONDS) => {
|
|
324
|
+
document.cookie = serializeAdHubAnonymousIdCookie({
|
|
325
|
+
projectKey: this.projectKey,
|
|
326
|
+
anonymousId,
|
|
327
|
+
domain,
|
|
328
|
+
secure,
|
|
329
|
+
maxAgeSeconds
|
|
330
|
+
});
|
|
331
|
+
};
|
|
332
|
+
if (this.cookieDomain) {
|
|
333
|
+
write(null, 0);
|
|
334
|
+
write(this.cookieDomain);
|
|
335
|
+
if (this.readCookieValues(this.anonymousKey).includes(anonymousId)) {
|
|
336
|
+
this.writeLocal(this.cookieDomainKey, this.cookieDomain);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
this.cookieDomain = null;
|
|
340
|
+
}
|
|
341
|
+
write(null);
|
|
342
|
+
this.writeLocal(this.cookieDomainKey, "");
|
|
280
343
|
} catch {
|
|
281
344
|
}
|
|
282
345
|
}
|
|
@@ -335,6 +398,19 @@ var BrowserStore = class {
|
|
|
335
398
|
}
|
|
336
399
|
return null;
|
|
337
400
|
}
|
|
401
|
+
readCookieValues(name) {
|
|
402
|
+
const values = [];
|
|
403
|
+
for (const part of document.cookie.split(";")) {
|
|
404
|
+
const [rawName, ...rawValue] = part.trim().split("=");
|
|
405
|
+
if (rawName !== name) continue;
|
|
406
|
+
try {
|
|
407
|
+
values.push(decodeURIComponent(rawValue.join("=")));
|
|
408
|
+
} catch {
|
|
409
|
+
values.push("");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return values;
|
|
413
|
+
}
|
|
338
414
|
readLocal(key) {
|
|
339
415
|
try {
|
|
340
416
|
return localStorage.getItem(key);
|
|
@@ -356,7 +432,7 @@ var BrowserStore = class {
|
|
|
356
432
|
// src/client.ts
|
|
357
433
|
var DEFAULT_ENDPOINT = "https://api.adhub.so";
|
|
358
434
|
var SDK_NAME = "nextjs";
|
|
359
|
-
var SDK_VERSION = "0.
|
|
435
|
+
var SDK_VERSION = "0.4.1";
|
|
360
436
|
var REQUEST_TIMEOUT_MS = 5e3;
|
|
361
437
|
var RETRY_INTERVAL_MS = 15e3;
|
|
362
438
|
var PAGE_ENGAGEMENT_INTERVAL_MS = 5e3;
|
|
@@ -587,6 +663,9 @@ var AdHubBrowserClient = class {
|
|
|
587
663
|
this.store = null;
|
|
588
664
|
this.queue = [];
|
|
589
665
|
this.endpoint = DEFAULT_ENDPOINT;
|
|
666
|
+
this.cookieDomainOption = null;
|
|
667
|
+
/** The endpoint confirmed that it accepts and sets first-party cookies. */
|
|
668
|
+
this.endpointSharesCookies = false;
|
|
590
669
|
this.projectKey = "";
|
|
591
670
|
this.debug = false;
|
|
592
671
|
this.flushPromise = null;
|
|
@@ -669,6 +748,8 @@ var AdHubBrowserClient = class {
|
|
|
669
748
|
}
|
|
670
749
|
this.projectKey = projectKey;
|
|
671
750
|
this.endpoint = endpoint;
|
|
751
|
+
this.cookieDomainOption = options.cookieDomain?.trim() || null;
|
|
752
|
+
this.endpointSharesCookies = false;
|
|
672
753
|
this.debug = options.debug ?? false;
|
|
673
754
|
this.waitForConsent = options.waitForConsent === true;
|
|
674
755
|
this.autoFlush = options.autoFlush ?? true;
|
|
@@ -678,6 +759,9 @@ var AdHubBrowserClient = class {
|
|
|
678
759
|
this.autoTrackDataAttributes = options.autoTrackDataAttributes ?? true;
|
|
679
760
|
this.trackHashChanges = options.trackHashChanges ?? false;
|
|
680
761
|
this.store = new BrowserStore(projectKey);
|
|
762
|
+
if (this.cookieDomainOption && !this.store.setCookieDomain(this.cookieDomainOption)) {
|
|
763
|
+
this.log("cookieDomain was ignored because this page is not on that domain.");
|
|
764
|
+
}
|
|
681
765
|
this.queue = this.store.loadQueue();
|
|
682
766
|
this.sessionTimeoutMs = Number.isFinite(options.sessionTimeoutMinutes) ? Math.max(1, Math.min(24 * 60, options.sessionTimeoutMinutes ?? 30)) * 60 * 1e3 : DEFAULT_SESSION_TIMEOUT_MS;
|
|
683
767
|
const anonymousId = this.store.readAnonymousId() ?? randomId("anon");
|
|
@@ -1064,7 +1148,7 @@ var AdHubBrowserClient = class {
|
|
|
1064
1148
|
}),
|
|
1065
1149
|
body: JSON.stringify(item.body),
|
|
1066
1150
|
keepalive: true,
|
|
1067
|
-
credentials:
|
|
1151
|
+
credentials: this.credentialsMode(),
|
|
1068
1152
|
signal: controller.signal
|
|
1069
1153
|
});
|
|
1070
1154
|
if (response.ok) {
|
|
@@ -1130,7 +1214,7 @@ var AdHubBrowserClient = class {
|
|
|
1130
1214
|
const response = await fetch(`${this.endpoint}${SDK_CONFIG_PATH}`, {
|
|
1131
1215
|
method: "GET",
|
|
1132
1216
|
headers: this.sdkHeaders(),
|
|
1133
|
-
credentials: "omit",
|
|
1217
|
+
credentials: this.endpointIsSameOrigin() ? "same-origin" : "omit",
|
|
1134
1218
|
signal: abort.signal
|
|
1135
1219
|
});
|
|
1136
1220
|
if (abort.signal.aborted) return;
|
|
@@ -1146,6 +1230,7 @@ var AdHubBrowserClient = class {
|
|
|
1146
1230
|
return;
|
|
1147
1231
|
}
|
|
1148
1232
|
if (abort.signal.aborted) return;
|
|
1233
|
+
this.applyIdentityCookieDomain(payload);
|
|
1149
1234
|
this.applyGoogleAdsWebsiteTag(payload);
|
|
1150
1235
|
this.applyClarity(payload);
|
|
1151
1236
|
} catch (error) {
|
|
@@ -1155,6 +1240,27 @@ var AdHubBrowserClient = class {
|
|
|
1155
1240
|
clearTimeout(timeout);
|
|
1156
1241
|
}
|
|
1157
1242
|
}
|
|
1243
|
+
endpointIsSameOrigin() {
|
|
1244
|
+
return this.endpoint === "" || this.endpoint.startsWith("/");
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Cookies travel to a same-origin proxy always, and to another host only
|
|
1248
|
+
* after its SDK config confirmed a shared cookie domain. The default API
|
|
1249
|
+
* answers with a wildcard CORS origin, which browsers reject for
|
|
1250
|
+
* credentialed requests, so it must stay "omit".
|
|
1251
|
+
*/
|
|
1252
|
+
credentialsMode() {
|
|
1253
|
+
if (this.endpointIsSameOrigin()) return "same-origin";
|
|
1254
|
+
return this.endpointSharesCookies ? "include" : "omit";
|
|
1255
|
+
}
|
|
1256
|
+
applyIdentityCookieDomain(payload) {
|
|
1257
|
+
if (!this.store || !payload || typeof payload !== "object") return;
|
|
1258
|
+
const domain = payload.identityCookieDomain;
|
|
1259
|
+
if (typeof domain !== "string" || !domain.trim()) return;
|
|
1260
|
+
if (!this.store.setCookieDomain(this.cookieDomainOption ?? domain)) return;
|
|
1261
|
+
this.endpointSharesCookies = true;
|
|
1262
|
+
if (this.state.anonymousId) this.store.writeAnonymousId(this.state.anonymousId);
|
|
1263
|
+
}
|
|
1158
1264
|
applyGoogleAdsWebsiteTag(payload) {
|
|
1159
1265
|
const decision = decideGoogleAdsTag(payload);
|
|
1160
1266
|
if (decision.action === "skip") return;
|