@aranova/tracking-next 0.2.3 → 0.4.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/dist/index.d.mts +499 -5
- package/dist/index.d.ts +499 -5
- package/dist/index.js +604 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +609 -30
- package/dist/index.mjs.map +1 -1
- package/dist/middleware.js +112 -0
- package/dist/middleware.js.map +1 -1
- package/dist/middleware.mjs +112 -0
- package/dist/middleware.mjs.map +1 -1
- package/dist/server.d.mts +5 -1
- package/dist/server.d.ts +5 -1
- package/dist/server.js +112 -0
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +112 -0
- package/dist/server.mjs.map +1 -1
- package/dist/{server-BMTa7njW.d.mts → types-Gp0ioRiQ.d.mts} +3 -3
- package/dist/{server-BMTa7njW.d.ts → types-Gp0ioRiQ.d.ts} +3 -3
- package/package.json +3 -2
package/dist/index.mjs
CHANGED
|
@@ -40,6 +40,7 @@ function createTrackingClientContext(surface, input = {}) {
|
|
|
40
40
|
function createTrackingSessionUpsertPayload(trackingParams, input, context) {
|
|
41
41
|
return {
|
|
42
42
|
session_id: input.sessionId,
|
|
43
|
+
visitor_id: input.visitorId ?? null,
|
|
43
44
|
gclid: trackingParams.gclid,
|
|
44
45
|
fbclid: trackingParams.fbclid,
|
|
45
46
|
utm_source: trackingParams.utm_source,
|
|
@@ -145,8 +146,8 @@ function getTrackingQueryValues(searchParams) {
|
|
|
145
146
|
function getCookieValueFromDocument(key) {
|
|
146
147
|
if (typeof document === "undefined")
|
|
147
148
|
return null;
|
|
148
|
-
const
|
|
149
|
-
const match =
|
|
149
|
+
const cookies = document.cookie ? document.cookie.split("; ") : [];
|
|
150
|
+
const match = cookies.find((cookie) => cookie.startsWith(`${key}=`));
|
|
150
151
|
if (!match)
|
|
151
152
|
return null;
|
|
152
153
|
const [, rawValue = ""] = match.split("=");
|
|
@@ -171,6 +172,515 @@ function captureTrackingParamsFromLocation(url = typeof window === "undefined" ?
|
|
|
171
172
|
return getTrackingParamsFromCookieReader(getCookieValueFromDocument);
|
|
172
173
|
}
|
|
173
174
|
|
|
175
|
+
// ../tracking-core/src/session.ts
|
|
176
|
+
var VISITOR_STORAGE_KEY = "aranova_tracking_visitor";
|
|
177
|
+
var SESSION_STORAGE_KEY = "aranova_tracking_session";
|
|
178
|
+
var SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
179
|
+
function safeUuid() {
|
|
180
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function")
|
|
181
|
+
return crypto.randomUUID();
|
|
182
|
+
return `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}-${Math.random().toString(16).slice(2)}`;
|
|
183
|
+
}
|
|
184
|
+
function readLocalStorage(key) {
|
|
185
|
+
try {
|
|
186
|
+
return window.localStorage.getItem(key);
|
|
187
|
+
} catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function writeLocalStorage(key, value) {
|
|
192
|
+
try {
|
|
193
|
+
window.localStorage.setItem(key, value);
|
|
194
|
+
} catch {
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function getVisitorId() {
|
|
198
|
+
if (typeof window === "undefined")
|
|
199
|
+
return safeUuid();
|
|
200
|
+
const existing = readLocalStorage(VISITOR_STORAGE_KEY);
|
|
201
|
+
if (existing && existing.length > 0)
|
|
202
|
+
return existing;
|
|
203
|
+
const fresh = safeUuid();
|
|
204
|
+
writeLocalStorage(VISITOR_STORAGE_KEY, fresh);
|
|
205
|
+
return fresh;
|
|
206
|
+
}
|
|
207
|
+
function getOrRotateSessionId(now = Date.now()) {
|
|
208
|
+
if (typeof window === "undefined")
|
|
209
|
+
return safeUuid();
|
|
210
|
+
const raw = readLocalStorage(SESSION_STORAGE_KEY);
|
|
211
|
+
if (raw) {
|
|
212
|
+
try {
|
|
213
|
+
const parsed = JSON.parse(raw);
|
|
214
|
+
if (typeof parsed.id === "string" && typeof parsed.last_event_at === "number") {
|
|
215
|
+
if (now - parsed.last_event_at <= SESSION_IDLE_MS) {
|
|
216
|
+
const refreshed = { id: parsed.id, last_event_at: now };
|
|
217
|
+
writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(refreshed));
|
|
218
|
+
return parsed.id;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const fresh = { id: safeUuid(), last_event_at: now };
|
|
225
|
+
writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(fresh));
|
|
226
|
+
return fresh.id;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ../tracking-core/src/events/page-view.ts
|
|
230
|
+
import { z } from "zod";
|
|
231
|
+
var pageViewMetadataSchema = z.object({
|
|
232
|
+
page: z.object({
|
|
233
|
+
title: z.string().nullable(),
|
|
234
|
+
path: z.string(),
|
|
235
|
+
search: z.string(),
|
|
236
|
+
hash: z.string()
|
|
237
|
+
}),
|
|
238
|
+
referrer: z.string().nullable(),
|
|
239
|
+
// `.nullable().optional()` — absent (undefined) OR explicit null OR a
|
|
240
|
+
// real viewport object. Mirrors Pydantic's `_Viewport | None = None`
|
|
241
|
+
// on the backend side so the drift test stays clean.
|
|
242
|
+
viewport: z.object({
|
|
243
|
+
w: z.number(),
|
|
244
|
+
h: z.number()
|
|
245
|
+
}).nullable().optional()
|
|
246
|
+
}).strict();
|
|
247
|
+
var pageViewConfigSchema = z.object({}).strict();
|
|
248
|
+
|
|
249
|
+
// ../tracking-core/src/page-view.ts
|
|
250
|
+
var LAST_FIRED_URL_STORAGE_KEY = "aranova_tracking_last_fired_url";
|
|
251
|
+
var lastFiredUrl = null;
|
|
252
|
+
var lastFiredUrlHydrated = false;
|
|
253
|
+
function readSessionStorage(key) {
|
|
254
|
+
try {
|
|
255
|
+
if (typeof window === "undefined") return null;
|
|
256
|
+
return window.sessionStorage.getItem(key);
|
|
257
|
+
} catch {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function writeSessionStorage(key, value) {
|
|
262
|
+
try {
|
|
263
|
+
if (typeof window === "undefined") return;
|
|
264
|
+
window.sessionStorage.setItem(key, value);
|
|
265
|
+
} catch {
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function getLastFiredUrl() {
|
|
269
|
+
if (!lastFiredUrlHydrated) {
|
|
270
|
+
lastFiredUrlHydrated = true;
|
|
271
|
+
const stored = readSessionStorage(LAST_FIRED_URL_STORAGE_KEY);
|
|
272
|
+
if (stored !== null) lastFiredUrl = stored;
|
|
273
|
+
}
|
|
274
|
+
return lastFiredUrl;
|
|
275
|
+
}
|
|
276
|
+
function setLastFiredUrl(url) {
|
|
277
|
+
lastFiredUrl = url;
|
|
278
|
+
lastFiredUrlHydrated = true;
|
|
279
|
+
writeSessionStorage(LAST_FIRED_URL_STORAGE_KEY, url);
|
|
280
|
+
}
|
|
281
|
+
function buildPageViewMetadata(referrerOverride) {
|
|
282
|
+
if (typeof window === "undefined" || typeof document === "undefined")
|
|
283
|
+
return null;
|
|
284
|
+
return pageViewMetadataSchema.parse({
|
|
285
|
+
page: {
|
|
286
|
+
title: document.title || null,
|
|
287
|
+
path: window.location.pathname,
|
|
288
|
+
search: window.location.search,
|
|
289
|
+
hash: window.location.hash
|
|
290
|
+
},
|
|
291
|
+
referrer: referrerOverride !== void 0 ? referrerOverride : document.referrer || null,
|
|
292
|
+
viewport: { w: window.innerWidth, h: window.innerHeight }
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function fireManualPageView(client) {
|
|
296
|
+
if (typeof window === "undefined")
|
|
297
|
+
return;
|
|
298
|
+
const currentHref = window.location.href;
|
|
299
|
+
const previousFiredUrl = getLastFiredUrl();
|
|
300
|
+
const internalReferrer = previousFiredUrl !== null && previousFiredUrl !== currentHref ? previousFiredUrl : null;
|
|
301
|
+
const externalReferrer = typeof document !== "undefined" ? document.referrer || null : null;
|
|
302
|
+
const referrer = internalReferrer ?? externalReferrer;
|
|
303
|
+
const metadata = buildPageViewMetadata(referrer);
|
|
304
|
+
client.trackEvent({
|
|
305
|
+
eventType: "page_view",
|
|
306
|
+
pageUrl: currentHref,
|
|
307
|
+
metadata
|
|
308
|
+
});
|
|
309
|
+
if (currentHref !== previousFiredUrl) {
|
|
310
|
+
setLastFiredUrl(currentHref);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function attachBfcacheRestore(client) {
|
|
314
|
+
if (typeof window === "undefined") return () => {
|
|
315
|
+
};
|
|
316
|
+
function handlePageShow(event) {
|
|
317
|
+
if (!event.persisted) return;
|
|
318
|
+
fireManualPageView(client);
|
|
319
|
+
}
|
|
320
|
+
window.addEventListener("pageshow", handlePageShow);
|
|
321
|
+
return () => {
|
|
322
|
+
window.removeEventListener("pageshow", handlePageShow);
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ../tracking-core/src/ingest.ts
|
|
327
|
+
var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
|
|
328
|
+
var DEFAULT_MAX_QUEUE_SIZE = 10;
|
|
329
|
+
var HARD_MAX_BATCH = 50;
|
|
330
|
+
var API_KEY_HEADER = "X-Aranova-Api-Key";
|
|
331
|
+
function buildContext(surface, sdkVersion, packageName) {
|
|
332
|
+
return {
|
|
333
|
+
surface,
|
|
334
|
+
sdk_version: sdkVersion,
|
|
335
|
+
package_name: packageName,
|
|
336
|
+
site_origin: typeof window === "undefined" ? null : window.location.origin,
|
|
337
|
+
page_title: typeof document === "undefined" ? null : document.title || null,
|
|
338
|
+
referrer: typeof document === "undefined" ? null : document.referrer || null
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
function readTrackingParams() {
|
|
342
|
+
if (typeof window === "undefined")
|
|
343
|
+
return createEmptyTrackingParams();
|
|
344
|
+
try {
|
|
345
|
+
captureTrackingParamsFromLocation();
|
|
346
|
+
} catch {
|
|
347
|
+
}
|
|
348
|
+
return getTrackingParamsFromCookieReader(getCookieValueFromDocument);
|
|
349
|
+
}
|
|
350
|
+
function consentSnapshot() {
|
|
351
|
+
try {
|
|
352
|
+
return { state: getConsentState() };
|
|
353
|
+
} catch {
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function postWithFetch(url, body, apiKey, keepalive) {
|
|
358
|
+
if (typeof fetch !== "function")
|
|
359
|
+
return;
|
|
360
|
+
try {
|
|
361
|
+
await fetch(url, {
|
|
362
|
+
method: "POST",
|
|
363
|
+
headers: {
|
|
364
|
+
"Content-Type": "application/json",
|
|
365
|
+
[API_KEY_HEADER]: apiKey
|
|
366
|
+
},
|
|
367
|
+
body,
|
|
368
|
+
keepalive,
|
|
369
|
+
// CORS is open on the tracking endpoint; never send cookies.
|
|
370
|
+
credentials: "omit",
|
|
371
|
+
mode: "cors"
|
|
372
|
+
});
|
|
373
|
+
} catch {
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
var globalClient = null;
|
|
377
|
+
var globalClientKey = null;
|
|
378
|
+
function clientConfigKey(config) {
|
|
379
|
+
return `${config.apiKey}@${config.endpoint}#${config.surface}`;
|
|
380
|
+
}
|
|
381
|
+
function getOrCreateTrackingClient(config) {
|
|
382
|
+
const key = clientConfigKey(config);
|
|
383
|
+
if (globalClient !== null && globalClientKey === key) {
|
|
384
|
+
return globalClient;
|
|
385
|
+
}
|
|
386
|
+
if (globalClient !== null) {
|
|
387
|
+
globalClient.destroy();
|
|
388
|
+
}
|
|
389
|
+
globalClient = createTrackingClient(config);
|
|
390
|
+
globalClientKey = key;
|
|
391
|
+
return globalClient;
|
|
392
|
+
}
|
|
393
|
+
function createTrackingClient(config) {
|
|
394
|
+
const flushIntervalMs = config.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS;
|
|
395
|
+
const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
|
|
396
|
+
const sdkVersion = config.sdkVersion ?? null;
|
|
397
|
+
const packageName = config.packageName ?? null;
|
|
398
|
+
const endpointBase = config.endpoint.replace(/\/$/, "");
|
|
399
|
+
const eventsUrl = `${endpointBase}/events`;
|
|
400
|
+
let queue = [];
|
|
401
|
+
let flushTimer = null;
|
|
402
|
+
let firstPage = null;
|
|
403
|
+
let destroyed = false;
|
|
404
|
+
const visitorId = getVisitorId();
|
|
405
|
+
let sessionId = getOrRotateSessionId();
|
|
406
|
+
if (typeof window !== "undefined")
|
|
407
|
+
firstPage = window.location.href;
|
|
408
|
+
function buildSessionPayload() {
|
|
409
|
+
sessionId = getOrRotateSessionId();
|
|
410
|
+
const params = readTrackingParams();
|
|
411
|
+
const context = buildContext(config.surface, sdkVersion, packageName);
|
|
412
|
+
return {
|
|
413
|
+
session_id: sessionId,
|
|
414
|
+
visitor_id: visitorId,
|
|
415
|
+
gclid: params.gclid,
|
|
416
|
+
fbclid: params.fbclid,
|
|
417
|
+
utm_source: params.utm_source,
|
|
418
|
+
utm_medium: params.utm_medium,
|
|
419
|
+
utm_campaign: params.utm_campaign,
|
|
420
|
+
utm_term: params.utm_term,
|
|
421
|
+
utm_content: params.utm_content,
|
|
422
|
+
first_page: firstPage,
|
|
423
|
+
consent_state: consentSnapshot(),
|
|
424
|
+
context
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function scheduleFlush() {
|
|
428
|
+
if (flushTimer !== null || destroyed)
|
|
429
|
+
return;
|
|
430
|
+
flushTimer = setTimeout(() => {
|
|
431
|
+
flushTimer = null;
|
|
432
|
+
void flush();
|
|
433
|
+
}, flushIntervalMs);
|
|
434
|
+
}
|
|
435
|
+
function clearScheduledFlush() {
|
|
436
|
+
if (flushTimer !== null) {
|
|
437
|
+
clearTimeout(flushTimer);
|
|
438
|
+
flushTimer = null;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
async function flush() {
|
|
442
|
+
if (queue.length === 0)
|
|
443
|
+
return;
|
|
444
|
+
const events = queue.slice(0, HARD_MAX_BATCH);
|
|
445
|
+
queue = queue.slice(events.length);
|
|
446
|
+
clearScheduledFlush();
|
|
447
|
+
const body = {
|
|
448
|
+
session: buildSessionPayload(),
|
|
449
|
+
events
|
|
450
|
+
};
|
|
451
|
+
const serialized = JSON.stringify(body);
|
|
452
|
+
await postWithFetch(eventsUrl, serialized, config.apiKey, false);
|
|
453
|
+
}
|
|
454
|
+
function trackEvent(input) {
|
|
455
|
+
if (destroyed)
|
|
456
|
+
return;
|
|
457
|
+
if (!input || typeof input.eventType !== "string" || input.eventType.length === 0)
|
|
458
|
+
return;
|
|
459
|
+
const occurredAt = input.occurredAt instanceof Date ? input.occurredAt.toISOString() : typeof input.occurredAt === "string" ? input.occurredAt : (/* @__PURE__ */ new Date()).toISOString();
|
|
460
|
+
queue.push({
|
|
461
|
+
event_type: input.eventType,
|
|
462
|
+
page_url: input.pageUrl ?? (typeof window === "undefined" ? null : window.location.href),
|
|
463
|
+
metadata: input.metadata ?? null,
|
|
464
|
+
occurred_at: occurredAt
|
|
465
|
+
});
|
|
466
|
+
if (queue.length >= maxQueueSize) {
|
|
467
|
+
void flush();
|
|
468
|
+
} else {
|
|
469
|
+
scheduleFlush();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function flushOnUnload() {
|
|
473
|
+
if (queue.length === 0)
|
|
474
|
+
return;
|
|
475
|
+
const events = queue.slice(0, HARD_MAX_BATCH);
|
|
476
|
+
queue = queue.slice(events.length);
|
|
477
|
+
clearScheduledFlush();
|
|
478
|
+
const body = {
|
|
479
|
+
session: buildSessionPayload(),
|
|
480
|
+
events
|
|
481
|
+
};
|
|
482
|
+
const serialized = JSON.stringify(body);
|
|
483
|
+
void postWithFetch(eventsUrl, serialized, config.apiKey, true);
|
|
484
|
+
}
|
|
485
|
+
if (typeof window !== "undefined") {
|
|
486
|
+
window.addEventListener("pagehide", flushOnUnload);
|
|
487
|
+
window.addEventListener("visibilitychange", () => {
|
|
488
|
+
if (document.visibilityState === "hidden") flushOnUnload();
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
return {
|
|
492
|
+
trackEvent,
|
|
493
|
+
flush,
|
|
494
|
+
getSessionId: () => sessionId,
|
|
495
|
+
getVisitorId: () => visitorId,
|
|
496
|
+
destroy: () => {
|
|
497
|
+
destroyed = true;
|
|
498
|
+
if (queue.length > 0) {
|
|
499
|
+
flushOnUnload();
|
|
500
|
+
}
|
|
501
|
+
clearScheduledFlush();
|
|
502
|
+
queue = [];
|
|
503
|
+
if (typeof window !== "undefined") {
|
|
504
|
+
window.removeEventListener("pagehide", flushOnUnload);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// ../tracking-core/src/events/contact-page-visit.ts
|
|
511
|
+
import { z as z2 } from "zod";
|
|
512
|
+
var contactPageVisitMetadataSchema = z2.object({
|
|
513
|
+
page: z2.object({
|
|
514
|
+
path: z2.string()
|
|
515
|
+
})
|
|
516
|
+
}).strict();
|
|
517
|
+
var contactPageVisitConfigSchema = z2.object({
|
|
518
|
+
// RegExp is runtime-only so we wrap it via z.custom. Consumers pass a
|
|
519
|
+
// real regex at createTracking() time; the factory validates with this
|
|
520
|
+
// schema and then keeps the live RegExp reference for runtime matching.
|
|
521
|
+
pathPattern: z2.custom(
|
|
522
|
+
(value) => value instanceof RegExp,
|
|
523
|
+
{ message: "pathPattern must be a RegExp" }
|
|
524
|
+
)
|
|
525
|
+
}).strict();
|
|
526
|
+
|
|
527
|
+
// ../tracking-core/src/events/form-submit.ts
|
|
528
|
+
import { z as z3 } from "zod";
|
|
529
|
+
var formSubmitMetadataSchema = z3.object({
|
|
530
|
+
form: z3.object({
|
|
531
|
+
id: z3.string(),
|
|
532
|
+
action: z3.string().nullable()
|
|
533
|
+
}),
|
|
534
|
+
page: z3.object({
|
|
535
|
+
path: z3.string()
|
|
536
|
+
})
|
|
537
|
+
}).strict();
|
|
538
|
+
var formSubmitConfigSchema = z3.object({}).strict();
|
|
539
|
+
|
|
540
|
+
// ../tracking-core/src/events/phone-click.ts
|
|
541
|
+
import { z as z4 } from "zod";
|
|
542
|
+
var phoneClickMetadataSchema = z4.object({
|
|
543
|
+
element: z4.object({
|
|
544
|
+
tag: z4.string(),
|
|
545
|
+
text: z4.string().nullable(),
|
|
546
|
+
href: z4.string()
|
|
547
|
+
}),
|
|
548
|
+
page: z4.object({
|
|
549
|
+
path: z4.string()
|
|
550
|
+
})
|
|
551
|
+
}).strict();
|
|
552
|
+
var phoneClickConfigSchema = z4.object({}).strict();
|
|
553
|
+
|
|
554
|
+
// ../tracking-core/src/events/time-on-site.ts
|
|
555
|
+
import { z as z5 } from "zod";
|
|
556
|
+
var timeOnSiteMetadataSchema = z5.object({
|
|
557
|
+
duration_ms: z5.number().int().nonnegative(),
|
|
558
|
+
page: z5.object({
|
|
559
|
+
path: z5.string()
|
|
560
|
+
})
|
|
561
|
+
}).strict();
|
|
562
|
+
var timeOnSiteConfigSchema = z5.object({
|
|
563
|
+
thresholdSeconds: z5.number().int().positive()
|
|
564
|
+
}).strict();
|
|
565
|
+
|
|
566
|
+
// ../tracking-core/src/events/registry.ts
|
|
567
|
+
var EVENT_REGISTRY = {
|
|
568
|
+
// --- automatic triggers ---
|
|
569
|
+
page_view: {
|
|
570
|
+
kind: "automatic",
|
|
571
|
+
metadataSchema: pageViewMetadataSchema,
|
|
572
|
+
configSchema: pageViewConfigSchema
|
|
573
|
+
},
|
|
574
|
+
time_on_site: {
|
|
575
|
+
kind: "automatic",
|
|
576
|
+
metadataSchema: timeOnSiteMetadataSchema,
|
|
577
|
+
configSchema: timeOnSiteConfigSchema
|
|
578
|
+
},
|
|
579
|
+
contact_page_visit: {
|
|
580
|
+
kind: "automatic",
|
|
581
|
+
metadataSchema: contactPageVisitMetadataSchema,
|
|
582
|
+
configSchema: contactPageVisitConfigSchema
|
|
583
|
+
},
|
|
584
|
+
// --- manual triggers ---
|
|
585
|
+
form_submit: {
|
|
586
|
+
kind: "manual",
|
|
587
|
+
metadataSchema: formSubmitMetadataSchema,
|
|
588
|
+
configSchema: formSubmitConfigSchema
|
|
589
|
+
},
|
|
590
|
+
phone_click: {
|
|
591
|
+
kind: "manual",
|
|
592
|
+
metadataSchema: phoneClickMetadataSchema,
|
|
593
|
+
configSchema: phoneClickConfigSchema
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
var ALL_AUTOMATIC_EVENT_NAMES = Object.entries(EVENT_REGISTRY).filter(([, def]) => def.kind === "automatic").map(([name]) => name);
|
|
597
|
+
var ALL_MANUAL_EVENT_NAMES = Object.entries(EVENT_REGISTRY).filter(([, def]) => def.kind === "manual").map(([name]) => name);
|
|
598
|
+
function getEventDefinition(name) {
|
|
599
|
+
return EVENT_REGISTRY[name];
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// ../tracking-core/src/ingest-typed.ts
|
|
603
|
+
function createTypedClient(raw, _registry, options = {}) {
|
|
604
|
+
const debug = options.debug ?? false;
|
|
605
|
+
return {
|
|
606
|
+
trackEvent(eventType, metadata, opts) {
|
|
607
|
+
if (debug) {
|
|
608
|
+
const def = getEventDefinition(eventType);
|
|
609
|
+
def.metadataSchema.parse(metadata);
|
|
610
|
+
}
|
|
611
|
+
raw.trackEvent({
|
|
612
|
+
eventType,
|
|
613
|
+
metadata,
|
|
614
|
+
pageUrl: opts?.pageUrl ?? null,
|
|
615
|
+
occurredAt: opts?.occurredAt ?? null
|
|
616
|
+
});
|
|
617
|
+
},
|
|
618
|
+
flush: raw.flush.bind(raw),
|
|
619
|
+
getSessionId: raw.getSessionId.bind(raw),
|
|
620
|
+
getVisitorId: raw.getVisitorId.bind(raw)
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// ../tracking-core/src/triggers/time-on-site.ts
|
|
625
|
+
function attachTimeOnSite(client, config) {
|
|
626
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
627
|
+
return () => {
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
const thresholdMs = config.thresholdSeconds * 1e3;
|
|
631
|
+
let accumulatedMs = 0;
|
|
632
|
+
let activeSince = document.visibilityState === "visible" ? Date.now() : null;
|
|
633
|
+
let timer = null;
|
|
634
|
+
let fired = false;
|
|
635
|
+
function fire() {
|
|
636
|
+
if (fired) return;
|
|
637
|
+
fired = true;
|
|
638
|
+
client.trackEvent({
|
|
639
|
+
eventType: "time_on_site",
|
|
640
|
+
metadata: {
|
|
641
|
+
duration_ms: thresholdMs,
|
|
642
|
+
page: { path: window.location.pathname }
|
|
643
|
+
},
|
|
644
|
+
pageUrl: window.location.href,
|
|
645
|
+
occurredAt: null
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
function scheduleNext() {
|
|
649
|
+
if (fired || activeSince === null) return;
|
|
650
|
+
const remaining = thresholdMs - accumulatedMs;
|
|
651
|
+
if (remaining <= 0) {
|
|
652
|
+
fire();
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
timer = setTimeout(fire, remaining);
|
|
656
|
+
}
|
|
657
|
+
function clearTimer() {
|
|
658
|
+
if (timer !== null) {
|
|
659
|
+
clearTimeout(timer);
|
|
660
|
+
timer = null;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
function onVisibilityChange() {
|
|
664
|
+
if (fired) return;
|
|
665
|
+
if (document.visibilityState === "hidden") {
|
|
666
|
+
if (activeSince !== null) {
|
|
667
|
+
accumulatedMs += Date.now() - activeSince;
|
|
668
|
+
activeSince = null;
|
|
669
|
+
}
|
|
670
|
+
clearTimer();
|
|
671
|
+
} else {
|
|
672
|
+
activeSince = Date.now();
|
|
673
|
+
scheduleNext();
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
677
|
+
scheduleNext();
|
|
678
|
+
return () => {
|
|
679
|
+
clearTimer();
|
|
680
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
|
|
174
684
|
// src/ConsentBanner.tsx
|
|
175
685
|
import { useEffect, useState } from "react";
|
|
176
686
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -266,45 +776,114 @@ function GoogleAdsTracking({ gtagId }) {
|
|
|
266
776
|
] });
|
|
267
777
|
}
|
|
268
778
|
|
|
269
|
-
// src/
|
|
270
|
-
import "
|
|
271
|
-
import {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
779
|
+
// src/factory.tsx
|
|
780
|
+
import { usePathname, useSearchParams } from "next/navigation";
|
|
781
|
+
import {
|
|
782
|
+
createContext,
|
|
783
|
+
useContext,
|
|
784
|
+
useEffect as useEffect3,
|
|
785
|
+
useMemo,
|
|
786
|
+
useRef
|
|
787
|
+
} from "react";
|
|
788
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
789
|
+
function createTracking(options) {
|
|
790
|
+
const { apiKey, endpoint, triggers, debug } = options;
|
|
791
|
+
if (!apiKey) throw new Error("createTracking: apiKey is required");
|
|
792
|
+
if (!endpoint) throw new Error("createTracking: endpoint is required");
|
|
793
|
+
const TrackingContext = createContext(null);
|
|
794
|
+
const contactPageVisitConfig = triggers.automatic.contact_page_visit ?? null;
|
|
795
|
+
const timeOnSiteConfig = triggers.automatic.time_on_site ?? null;
|
|
796
|
+
function TrackingProvider({ children }) {
|
|
797
|
+
const pathname = usePathname();
|
|
798
|
+
const searchParams = useSearchParams();
|
|
799
|
+
const client = useMemo(
|
|
800
|
+
() => createTypedClient(
|
|
801
|
+
getOrCreateTrackingClient({
|
|
802
|
+
apiKey,
|
|
803
|
+
endpoint,
|
|
804
|
+
surface: "next",
|
|
805
|
+
packageName: "@aranova/tracking-next",
|
|
806
|
+
debug
|
|
807
|
+
}),
|
|
808
|
+
triggers,
|
|
809
|
+
{ debug }
|
|
810
|
+
),
|
|
811
|
+
[]
|
|
812
|
+
);
|
|
813
|
+
useEffect3(() => {
|
|
814
|
+
const rawClient = getOrCreateTrackingClient({
|
|
815
|
+
apiKey,
|
|
816
|
+
endpoint,
|
|
817
|
+
surface: "next",
|
|
818
|
+
packageName: "@aranova/tracking-next",
|
|
819
|
+
debug
|
|
281
820
|
});
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
821
|
+
fireManualPageView(rawClient);
|
|
822
|
+
}, [pathname, searchParams]);
|
|
823
|
+
const lastContactPathRef = useRef(null);
|
|
824
|
+
useEffect3(() => {
|
|
825
|
+
if (!contactPageVisitConfig || pathname === null) return;
|
|
826
|
+
if (!contactPageVisitConfig.pathPattern.test(pathname)) return;
|
|
827
|
+
if (lastContactPathRef.current === pathname) return;
|
|
828
|
+
lastContactPathRef.current = pathname;
|
|
829
|
+
const rawClient = getOrCreateTrackingClient({
|
|
830
|
+
apiKey,
|
|
831
|
+
endpoint,
|
|
832
|
+
surface: "next",
|
|
833
|
+
packageName: "@aranova/tracking-next",
|
|
834
|
+
debug
|
|
835
|
+
});
|
|
836
|
+
rawClient.trackEvent({
|
|
837
|
+
eventType: "contact_page_visit",
|
|
838
|
+
metadata: { page: { path: pathname } },
|
|
839
|
+
pageUrl: null,
|
|
840
|
+
occurredAt: null
|
|
841
|
+
});
|
|
842
|
+
}, [pathname]);
|
|
843
|
+
useEffect3(() => {
|
|
844
|
+
const rawClient = getOrCreateTrackingClient({
|
|
845
|
+
apiKey,
|
|
846
|
+
endpoint,
|
|
847
|
+
surface: "next",
|
|
848
|
+
packageName: "@aranova/tracking-next",
|
|
849
|
+
debug
|
|
850
|
+
});
|
|
851
|
+
return attachBfcacheRestore(rawClient);
|
|
852
|
+
}, []);
|
|
853
|
+
useEffect3(() => {
|
|
854
|
+
if (!timeOnSiteConfig) return;
|
|
855
|
+
const rawClient = getOrCreateTrackingClient({
|
|
856
|
+
apiKey,
|
|
857
|
+
endpoint,
|
|
858
|
+
surface: "next",
|
|
859
|
+
packageName: "@aranova/tracking-next",
|
|
860
|
+
debug
|
|
861
|
+
});
|
|
862
|
+
return attachTimeOnSite(rawClient, timeOnSiteConfig);
|
|
863
|
+
}, []);
|
|
864
|
+
return /* @__PURE__ */ jsx3(TrackingContext.Provider, { value: client, children });
|
|
865
|
+
}
|
|
866
|
+
function useTracking() {
|
|
867
|
+
const client = useContext(TrackingContext);
|
|
868
|
+
if (client === null) {
|
|
869
|
+
throw new Error(
|
|
870
|
+
"useTracking must be called inside a <TrackingProvider> returned by createTracking()"
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
return client;
|
|
874
|
+
}
|
|
875
|
+
return { TrackingProvider, useTracking };
|
|
296
876
|
}
|
|
297
877
|
export {
|
|
298
878
|
ConsentBanner,
|
|
299
879
|
GoogleAdsTracking,
|
|
300
880
|
TRACKING_PARAM_KEYS,
|
|
301
881
|
captureTrackingParamsFromLocation,
|
|
882
|
+
createTracking,
|
|
302
883
|
createTrackingClientContext,
|
|
303
884
|
createTrackingEventCreatePayload,
|
|
304
|
-
createTrackingMiddleware,
|
|
305
885
|
createTrackingSessionUpsertPayload,
|
|
306
886
|
getConsentState,
|
|
307
|
-
getTrackingParamsServer,
|
|
308
887
|
setConsentState,
|
|
309
888
|
useConsentState,
|
|
310
889
|
useGclid,
|