@aranova/tracking-next 0.2.2 → 0.3.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 +41 -5
- package/dist/index.d.ts +41 -5
- package/dist/index.js +297 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +301 -29
- package/dist/index.mjs.map +1 -1
- package/dist/middleware.js +3 -0
- package/dist/middleware.js.map +1 -1
- package/dist/middleware.mjs +3 -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 +3 -0
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +3 -0
- package/dist/server.mjs.map +1 -1
- package/dist/{server-BMTa7njW.d.mts → types-CFWz-qi9.d.mts} +8 -3
- package/dist/{server-BMTa7njW.d.ts → types-CFWz-qi9.d.ts} +8 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { C as ConsentState, T as TrackingInstallSurface, a as TrackingClientContext, b as TrackingParams, c as TrackingEventCreatePayload, d as TrackingSessionUpsertPayload } from './
|
|
2
|
-
export { e as TrackingInitConfig
|
|
1
|
+
import { C as ConsentState, T as TrackingInstallSurface, a as TrackingClientContext, b as TrackingParams, c as TrackingEventCreatePayload, d as TrackingSessionUpsertPayload } from './types-CFWz-qi9.mjs';
|
|
2
|
+
export { e as TrackingInitConfig } from './types-CFWz-qi9.mjs';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
-
|
|
5
|
-
import 'next/server';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
6
5
|
|
|
7
6
|
type GtagConsentValue = 'granted' | 'denied';
|
|
8
7
|
declare function getConsentState(): ConsentState;
|
|
@@ -25,6 +24,7 @@ interface TrackingSessionInput {
|
|
|
25
24
|
consentState?: Record<string, unknown> | null;
|
|
26
25
|
firstPage?: string | null;
|
|
27
26
|
sessionId: string;
|
|
27
|
+
visitorId?: string | null;
|
|
28
28
|
}
|
|
29
29
|
declare function createTrackingClientContext(surface: TrackingInstallSurface, input?: TrackingContextInput): TrackingClientContext;
|
|
30
30
|
declare function createTrackingSessionUpsertPayload(trackingParams: TrackingParams, input: TrackingSessionInput, context: TrackingClientContext): TrackingSessionUpsertPayload;
|
|
@@ -33,6 +33,20 @@ declare function createTrackingEventCreatePayload(trackingParams: TrackingParams
|
|
|
33
33
|
declare const TRACKING_PARAM_KEYS: readonly ["gclid", "fbclid", "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
|
|
34
34
|
declare function captureTrackingParamsFromLocation(url?: string, maxAgeSeconds?: number): TrackingParams;
|
|
35
35
|
|
|
36
|
+
interface TrackEventInput {
|
|
37
|
+
eventType: string;
|
|
38
|
+
pageUrl?: string | null;
|
|
39
|
+
metadata?: Record<string, unknown> | null;
|
|
40
|
+
occurredAt?: Date | string | null;
|
|
41
|
+
}
|
|
42
|
+
interface TrackingClient {
|
|
43
|
+
trackEvent: (input: TrackEventInput) => void;
|
|
44
|
+
flush: () => Promise<void>;
|
|
45
|
+
getSessionId: () => string;
|
|
46
|
+
getVisitorId: () => string;
|
|
47
|
+
destroy: () => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
declare function ConsentBanner(): react_jsx_runtime.JSX.Element | null;
|
|
37
51
|
|
|
38
52
|
declare function useGclid(): string | null;
|
|
@@ -44,4 +58,26 @@ interface GoogleAdsTrackingProps {
|
|
|
44
58
|
}
|
|
45
59
|
declare function GoogleAdsTracking({ gtagId }: GoogleAdsTrackingProps): react_jsx_runtime.JSX.Element;
|
|
46
60
|
|
|
47
|
-
|
|
61
|
+
interface TrackingProviderProps {
|
|
62
|
+
/** Public tracking API key issued for this business. Required. */
|
|
63
|
+
apiKey: string;
|
|
64
|
+
/** Full ingest endpoint base URL, e.g. https://api.aranova.io/tracking. Required. */
|
|
65
|
+
endpoint: string;
|
|
66
|
+
/** Disable automatic page_view emission (manual tracking only). */
|
|
67
|
+
disableAutoPageView?: boolean;
|
|
68
|
+
/** Optional debug flag — currently a no-op, reserved for verbose logging. */
|
|
69
|
+
debug?: boolean;
|
|
70
|
+
children: ReactNode;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Provider for App Router projects. Uses `usePathname` for SPA route detection
|
|
74
|
+
* instead of patching `history.pushState`, because Next's router does not
|
|
75
|
+
* always go through the History API for transitions.
|
|
76
|
+
*
|
|
77
|
+
* `gtagId` lives on the existing <GoogleAdsTracking> component — keep them
|
|
78
|
+
* separate so a client without paid Google Ads can install just this provider.
|
|
79
|
+
*/
|
|
80
|
+
declare function TrackingProvider({ apiKey, endpoint, disableAutoPageView, debug, children, }: TrackingProviderProps): react_jsx_runtime.JSX.Element;
|
|
81
|
+
declare function useTracking(): TrackingClient;
|
|
82
|
+
|
|
83
|
+
export { ConsentBanner, ConsentState, GoogleAdsTracking, type GoogleAdsTrackingProps, TRACKING_PARAM_KEYS, type TrackingClient, TrackingClientContext, TrackingEventCreatePayload, TrackingInstallSurface, TrackingParams, TrackingProvider, type TrackingProviderProps, TrackingSessionUpsertPayload, captureTrackingParamsFromLocation, createTrackingClientContext, createTrackingEventCreatePayload, createTrackingSessionUpsertPayload, getConsentState, setConsentState, useConsentState, useGclid, useTracking, useTrackingParams };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { C as ConsentState, T as TrackingInstallSurface, a as TrackingClientContext, b as TrackingParams, c as TrackingEventCreatePayload, d as TrackingSessionUpsertPayload } from './
|
|
2
|
-
export { e as TrackingInitConfig
|
|
1
|
+
import { C as ConsentState, T as TrackingInstallSurface, a as TrackingClientContext, b as TrackingParams, c as TrackingEventCreatePayload, d as TrackingSessionUpsertPayload } from './types-CFWz-qi9.js';
|
|
2
|
+
export { e as TrackingInitConfig } from './types-CFWz-qi9.js';
|
|
3
3
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
-
|
|
5
|
-
import 'next/server';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
6
5
|
|
|
7
6
|
type GtagConsentValue = 'granted' | 'denied';
|
|
8
7
|
declare function getConsentState(): ConsentState;
|
|
@@ -25,6 +24,7 @@ interface TrackingSessionInput {
|
|
|
25
24
|
consentState?: Record<string, unknown> | null;
|
|
26
25
|
firstPage?: string | null;
|
|
27
26
|
sessionId: string;
|
|
27
|
+
visitorId?: string | null;
|
|
28
28
|
}
|
|
29
29
|
declare function createTrackingClientContext(surface: TrackingInstallSurface, input?: TrackingContextInput): TrackingClientContext;
|
|
30
30
|
declare function createTrackingSessionUpsertPayload(trackingParams: TrackingParams, input: TrackingSessionInput, context: TrackingClientContext): TrackingSessionUpsertPayload;
|
|
@@ -33,6 +33,20 @@ declare function createTrackingEventCreatePayload(trackingParams: TrackingParams
|
|
|
33
33
|
declare const TRACKING_PARAM_KEYS: readonly ["gclid", "fbclid", "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
|
|
34
34
|
declare function captureTrackingParamsFromLocation(url?: string, maxAgeSeconds?: number): TrackingParams;
|
|
35
35
|
|
|
36
|
+
interface TrackEventInput {
|
|
37
|
+
eventType: string;
|
|
38
|
+
pageUrl?: string | null;
|
|
39
|
+
metadata?: Record<string, unknown> | null;
|
|
40
|
+
occurredAt?: Date | string | null;
|
|
41
|
+
}
|
|
42
|
+
interface TrackingClient {
|
|
43
|
+
trackEvent: (input: TrackEventInput) => void;
|
|
44
|
+
flush: () => Promise<void>;
|
|
45
|
+
getSessionId: () => string;
|
|
46
|
+
getVisitorId: () => string;
|
|
47
|
+
destroy: () => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
declare function ConsentBanner(): react_jsx_runtime.JSX.Element | null;
|
|
37
51
|
|
|
38
52
|
declare function useGclid(): string | null;
|
|
@@ -44,4 +58,26 @@ interface GoogleAdsTrackingProps {
|
|
|
44
58
|
}
|
|
45
59
|
declare function GoogleAdsTracking({ gtagId }: GoogleAdsTrackingProps): react_jsx_runtime.JSX.Element;
|
|
46
60
|
|
|
47
|
-
|
|
61
|
+
interface TrackingProviderProps {
|
|
62
|
+
/** Public tracking API key issued for this business. Required. */
|
|
63
|
+
apiKey: string;
|
|
64
|
+
/** Full ingest endpoint base URL, e.g. https://api.aranova.io/tracking. Required. */
|
|
65
|
+
endpoint: string;
|
|
66
|
+
/** Disable automatic page_view emission (manual tracking only). */
|
|
67
|
+
disableAutoPageView?: boolean;
|
|
68
|
+
/** Optional debug flag — currently a no-op, reserved for verbose logging. */
|
|
69
|
+
debug?: boolean;
|
|
70
|
+
children: ReactNode;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Provider for App Router projects. Uses `usePathname` for SPA route detection
|
|
74
|
+
* instead of patching `history.pushState`, because Next's router does not
|
|
75
|
+
* always go through the History API for transitions.
|
|
76
|
+
*
|
|
77
|
+
* `gtagId` lives on the existing <GoogleAdsTracking> component — keep them
|
|
78
|
+
* separate so a client without paid Google Ads can install just this provider.
|
|
79
|
+
*/
|
|
80
|
+
declare function TrackingProvider({ apiKey, endpoint, disableAutoPageView, debug, children, }: TrackingProviderProps): react_jsx_runtime.JSX.Element;
|
|
81
|
+
declare function useTracking(): TrackingClient;
|
|
82
|
+
|
|
83
|
+
export { ConsentBanner, ConsentState, GoogleAdsTracking, type GoogleAdsTrackingProps, TRACKING_PARAM_KEYS, type TrackingClient, TrackingClientContext, TrackingEventCreatePayload, TrackingInstallSurface, TrackingParams, TrackingProvider, type TrackingProviderProps, TrackingSessionUpsertPayload, captureTrackingParamsFromLocation, createTrackingClientContext, createTrackingEventCreatePayload, createTrackingSessionUpsertPayload, getConsentState, setConsentState, useConsentState, useGclid, useTracking, useTrackingParams };
|
package/dist/index.js
CHANGED
|
@@ -33,16 +33,16 @@ __export(src_exports, {
|
|
|
33
33
|
ConsentBanner: () => ConsentBanner,
|
|
34
34
|
GoogleAdsTracking: () => GoogleAdsTracking,
|
|
35
35
|
TRACKING_PARAM_KEYS: () => TRACKING_PARAM_KEYS,
|
|
36
|
+
TrackingProvider: () => TrackingProvider,
|
|
36
37
|
captureTrackingParamsFromLocation: () => captureTrackingParamsFromLocation,
|
|
37
38
|
createTrackingClientContext: () => createTrackingClientContext,
|
|
38
39
|
createTrackingEventCreatePayload: () => createTrackingEventCreatePayload,
|
|
39
|
-
createTrackingMiddleware: () => createTrackingMiddleware,
|
|
40
40
|
createTrackingSessionUpsertPayload: () => createTrackingSessionUpsertPayload,
|
|
41
41
|
getConsentState: () => getConsentState,
|
|
42
|
-
getTrackingParamsServer: () => getTrackingParamsServer,
|
|
43
42
|
setConsentState: () => setConsentState,
|
|
44
43
|
useConsentState: () => useConsentState,
|
|
45
44
|
useGclid: () => useGclid,
|
|
45
|
+
useTracking: () => useTracking,
|
|
46
46
|
useTrackingParams: () => useTrackingParams
|
|
47
47
|
});
|
|
48
48
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -89,6 +89,7 @@ function createTrackingClientContext(surface, input = {}) {
|
|
|
89
89
|
function createTrackingSessionUpsertPayload(trackingParams, input, context) {
|
|
90
90
|
return {
|
|
91
91
|
session_id: input.sessionId,
|
|
92
|
+
visitor_id: input.visitorId ?? null,
|
|
92
93
|
gclid: trackingParams.gclid,
|
|
93
94
|
fbclid: trackingParams.fbclid,
|
|
94
95
|
utm_source: trackingParams.utm_source,
|
|
@@ -194,8 +195,8 @@ function getTrackingQueryValues(searchParams) {
|
|
|
194
195
|
function getCookieValueFromDocument(key) {
|
|
195
196
|
if (typeof document === "undefined")
|
|
196
197
|
return null;
|
|
197
|
-
const
|
|
198
|
-
const match =
|
|
198
|
+
const cookies = document.cookie ? document.cookie.split("; ") : [];
|
|
199
|
+
const match = cookies.find((cookie) => cookie.startsWith(`${key}=`));
|
|
199
200
|
if (!match)
|
|
200
201
|
return null;
|
|
201
202
|
const [, rawValue = ""] = match.split("=");
|
|
@@ -220,6 +221,248 @@ function captureTrackingParamsFromLocation(url = typeof window === "undefined" ?
|
|
|
220
221
|
return getTrackingParamsFromCookieReader(getCookieValueFromDocument);
|
|
221
222
|
}
|
|
222
223
|
|
|
224
|
+
// ../tracking-core/src/session.ts
|
|
225
|
+
var VISITOR_STORAGE_KEY = "aranova_tracking_visitor";
|
|
226
|
+
var SESSION_STORAGE_KEY = "aranova_tracking_session";
|
|
227
|
+
var SESSION_IDLE_MS = 30 * 60 * 1e3;
|
|
228
|
+
function safeUuid() {
|
|
229
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function")
|
|
230
|
+
return crypto.randomUUID();
|
|
231
|
+
return `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}-${Math.random().toString(16).slice(2)}`;
|
|
232
|
+
}
|
|
233
|
+
function readLocalStorage(key) {
|
|
234
|
+
try {
|
|
235
|
+
return window.localStorage.getItem(key);
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function writeLocalStorage(key, value) {
|
|
241
|
+
try {
|
|
242
|
+
window.localStorage.setItem(key, value);
|
|
243
|
+
} catch {
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function getVisitorId() {
|
|
247
|
+
if (typeof window === "undefined")
|
|
248
|
+
return safeUuid();
|
|
249
|
+
const existing = readLocalStorage(VISITOR_STORAGE_KEY);
|
|
250
|
+
if (existing && existing.length > 0)
|
|
251
|
+
return existing;
|
|
252
|
+
const fresh = safeUuid();
|
|
253
|
+
writeLocalStorage(VISITOR_STORAGE_KEY, fresh);
|
|
254
|
+
return fresh;
|
|
255
|
+
}
|
|
256
|
+
function getOrRotateSessionId(now = Date.now()) {
|
|
257
|
+
if (typeof window === "undefined")
|
|
258
|
+
return safeUuid();
|
|
259
|
+
const raw = readLocalStorage(SESSION_STORAGE_KEY);
|
|
260
|
+
if (raw) {
|
|
261
|
+
try {
|
|
262
|
+
const parsed = JSON.parse(raw);
|
|
263
|
+
if (typeof parsed.id === "string" && typeof parsed.last_event_at === "number") {
|
|
264
|
+
if (now - parsed.last_event_at <= SESSION_IDLE_MS) {
|
|
265
|
+
const refreshed = { id: parsed.id, last_event_at: now };
|
|
266
|
+
writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(refreshed));
|
|
267
|
+
return parsed.id;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
} catch {
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const fresh = { id: safeUuid(), last_event_at: now };
|
|
274
|
+
writeLocalStorage(SESSION_STORAGE_KEY, JSON.stringify(fresh));
|
|
275
|
+
return fresh.id;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ../tracking-core/src/ingest.ts
|
|
279
|
+
var DEFAULT_FLUSH_INTERVAL_MS = 2e3;
|
|
280
|
+
var DEFAULT_MAX_QUEUE_SIZE = 10;
|
|
281
|
+
var HARD_MAX_BATCH = 50;
|
|
282
|
+
var API_KEY_HEADER = "X-Aranova-Api-Key";
|
|
283
|
+
function buildContext(surface, sdkVersion, packageName) {
|
|
284
|
+
return {
|
|
285
|
+
surface,
|
|
286
|
+
sdk_version: sdkVersion,
|
|
287
|
+
package_name: packageName,
|
|
288
|
+
site_origin: typeof window === "undefined" ? null : window.location.origin,
|
|
289
|
+
page_title: typeof document === "undefined" ? null : document.title || null,
|
|
290
|
+
referrer: typeof document === "undefined" ? null : document.referrer || null
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function readTrackingParams() {
|
|
294
|
+
if (typeof window === "undefined")
|
|
295
|
+
return createEmptyTrackingParams();
|
|
296
|
+
try {
|
|
297
|
+
captureTrackingParamsFromLocation();
|
|
298
|
+
} catch {
|
|
299
|
+
}
|
|
300
|
+
return getTrackingParamsFromCookieReader(getCookieValueFromDocument);
|
|
301
|
+
}
|
|
302
|
+
function consentSnapshot() {
|
|
303
|
+
try {
|
|
304
|
+
return { state: getConsentState() };
|
|
305
|
+
} catch {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async function postWithFetch(url, body, apiKey, keepalive) {
|
|
310
|
+
if (typeof fetch !== "function")
|
|
311
|
+
return;
|
|
312
|
+
try {
|
|
313
|
+
await fetch(url, {
|
|
314
|
+
method: "POST",
|
|
315
|
+
headers: {
|
|
316
|
+
"Content-Type": "application/json",
|
|
317
|
+
[API_KEY_HEADER]: apiKey
|
|
318
|
+
},
|
|
319
|
+
body,
|
|
320
|
+
keepalive,
|
|
321
|
+
// CORS is open on the tracking endpoint; never send cookies.
|
|
322
|
+
credentials: "omit",
|
|
323
|
+
mode: "cors"
|
|
324
|
+
});
|
|
325
|
+
} catch {
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function createTrackingClient(config) {
|
|
329
|
+
const flushIntervalMs = config.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS;
|
|
330
|
+
const maxQueueSize = Math.min(config.maxQueueSize ?? DEFAULT_MAX_QUEUE_SIZE, HARD_MAX_BATCH);
|
|
331
|
+
const sdkVersion = config.sdkVersion ?? null;
|
|
332
|
+
const packageName = config.packageName ?? null;
|
|
333
|
+
const endpointBase = config.endpoint.replace(/\/$/, "");
|
|
334
|
+
const eventsUrl = `${endpointBase}/events`;
|
|
335
|
+
let queue = [];
|
|
336
|
+
let flushTimer = null;
|
|
337
|
+
let firstPage = null;
|
|
338
|
+
let destroyed = false;
|
|
339
|
+
const visitorId = getVisitorId();
|
|
340
|
+
let sessionId = getOrRotateSessionId();
|
|
341
|
+
if (typeof window !== "undefined")
|
|
342
|
+
firstPage = window.location.href;
|
|
343
|
+
function buildSessionPayload() {
|
|
344
|
+
sessionId = getOrRotateSessionId();
|
|
345
|
+
const params = readTrackingParams();
|
|
346
|
+
const context = buildContext(config.surface, sdkVersion, packageName);
|
|
347
|
+
return {
|
|
348
|
+
session_id: sessionId,
|
|
349
|
+
visitor_id: visitorId,
|
|
350
|
+
gclid: params.gclid,
|
|
351
|
+
fbclid: params.fbclid,
|
|
352
|
+
utm_source: params.utm_source,
|
|
353
|
+
utm_medium: params.utm_medium,
|
|
354
|
+
utm_campaign: params.utm_campaign,
|
|
355
|
+
utm_term: params.utm_term,
|
|
356
|
+
utm_content: params.utm_content,
|
|
357
|
+
first_page: firstPage,
|
|
358
|
+
consent_state: consentSnapshot(),
|
|
359
|
+
context
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
function scheduleFlush() {
|
|
363
|
+
if (flushTimer !== null || destroyed)
|
|
364
|
+
return;
|
|
365
|
+
flushTimer = setTimeout(() => {
|
|
366
|
+
flushTimer = null;
|
|
367
|
+
void flush();
|
|
368
|
+
}, flushIntervalMs);
|
|
369
|
+
}
|
|
370
|
+
function clearScheduledFlush() {
|
|
371
|
+
if (flushTimer !== null) {
|
|
372
|
+
clearTimeout(flushTimer);
|
|
373
|
+
flushTimer = null;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
async function flush() {
|
|
377
|
+
if (queue.length === 0)
|
|
378
|
+
return;
|
|
379
|
+
const events = queue.slice(0, HARD_MAX_BATCH);
|
|
380
|
+
queue = queue.slice(events.length);
|
|
381
|
+
clearScheduledFlush();
|
|
382
|
+
const body = {
|
|
383
|
+
session: buildSessionPayload(),
|
|
384
|
+
events
|
|
385
|
+
};
|
|
386
|
+
const serialized = JSON.stringify(body);
|
|
387
|
+
await postWithFetch(eventsUrl, serialized, config.apiKey, false);
|
|
388
|
+
}
|
|
389
|
+
function trackEvent(input) {
|
|
390
|
+
if (destroyed)
|
|
391
|
+
return;
|
|
392
|
+
if (!input || typeof input.eventType !== "string" || input.eventType.length === 0)
|
|
393
|
+
return;
|
|
394
|
+
const occurredAt = input.occurredAt instanceof Date ? input.occurredAt.toISOString() : typeof input.occurredAt === "string" ? input.occurredAt : (/* @__PURE__ */ new Date()).toISOString();
|
|
395
|
+
queue.push({
|
|
396
|
+
event_type: input.eventType,
|
|
397
|
+
page_url: input.pageUrl ?? (typeof window === "undefined" ? null : window.location.href),
|
|
398
|
+
metadata: input.metadata ?? null,
|
|
399
|
+
occurred_at: occurredAt
|
|
400
|
+
});
|
|
401
|
+
if (queue.length >= maxQueueSize) {
|
|
402
|
+
void flush();
|
|
403
|
+
} else {
|
|
404
|
+
scheduleFlush();
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function flushOnUnload() {
|
|
408
|
+
if (queue.length === 0)
|
|
409
|
+
return;
|
|
410
|
+
const events = queue.slice(0, HARD_MAX_BATCH);
|
|
411
|
+
queue = queue.slice(events.length);
|
|
412
|
+
clearScheduledFlush();
|
|
413
|
+
const body = {
|
|
414
|
+
session: buildSessionPayload(),
|
|
415
|
+
events
|
|
416
|
+
};
|
|
417
|
+
const serialized = JSON.stringify(body);
|
|
418
|
+
void postWithFetch(eventsUrl, serialized, config.apiKey, true);
|
|
419
|
+
}
|
|
420
|
+
if (typeof window !== "undefined") {
|
|
421
|
+
window.addEventListener("pagehide", flushOnUnload);
|
|
422
|
+
window.addEventListener("visibilitychange", () => {
|
|
423
|
+
if (document.visibilityState === "hidden") flushOnUnload();
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
return {
|
|
427
|
+
trackEvent,
|
|
428
|
+
flush,
|
|
429
|
+
getSessionId: () => sessionId,
|
|
430
|
+
getVisitorId: () => visitorId,
|
|
431
|
+
destroy: () => {
|
|
432
|
+
destroyed = true;
|
|
433
|
+
clearScheduledFlush();
|
|
434
|
+
queue = [];
|
|
435
|
+
if (typeof window !== "undefined") {
|
|
436
|
+
window.removeEventListener("pagehide", flushOnUnload);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// ../tracking-core/src/page-view.ts
|
|
443
|
+
function buildPageViewMetadata() {
|
|
444
|
+
if (typeof window === "undefined" || typeof document === "undefined")
|
|
445
|
+
return null;
|
|
446
|
+
return {
|
|
447
|
+
page: {
|
|
448
|
+
title: document.title || null,
|
|
449
|
+
path: window.location.pathname,
|
|
450
|
+
search: window.location.search,
|
|
451
|
+
hash: window.location.hash
|
|
452
|
+
},
|
|
453
|
+
referrer: document.referrer || null,
|
|
454
|
+
viewport: { w: window.innerWidth, h: window.innerHeight }
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function fireManualPageView(client) {
|
|
458
|
+
const metadata = buildPageViewMetadata();
|
|
459
|
+
client.trackEvent({
|
|
460
|
+
eventType: "page_view",
|
|
461
|
+
pageUrl: typeof window === "undefined" ? null : window.location.href,
|
|
462
|
+
metadata
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
|
|
223
466
|
// src/ConsentBanner.tsx
|
|
224
467
|
var import_react = require("react");
|
|
225
468
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
@@ -315,49 +558,72 @@ function GoogleAdsTracking({ gtagId }) {
|
|
|
315
558
|
] });
|
|
316
559
|
}
|
|
317
560
|
|
|
318
|
-
// src/
|
|
319
|
-
var
|
|
320
|
-
var
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
561
|
+
// src/TrackingProvider.tsx
|
|
562
|
+
var import_navigation = require("next/navigation");
|
|
563
|
+
var import_react3 = require("react");
|
|
564
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
565
|
+
var TrackingContext = (0, import_react3.createContext)(null);
|
|
566
|
+
function TrackingProvider({
|
|
567
|
+
apiKey,
|
|
568
|
+
endpoint,
|
|
569
|
+
disableAutoPageView,
|
|
570
|
+
debug,
|
|
571
|
+
children
|
|
572
|
+
}) {
|
|
573
|
+
if (!apiKey)
|
|
574
|
+
throw new Error("TrackingProvider: apiKey is required");
|
|
575
|
+
if (!endpoint)
|
|
576
|
+
throw new Error("TrackingProvider: endpoint is required");
|
|
577
|
+
const pathname = (0, import_navigation.usePathname)();
|
|
578
|
+
const searchParams = (0, import_navigation.useSearchParams)();
|
|
579
|
+
const clientRef = (0, import_react3.useRef)(null);
|
|
580
|
+
if (clientRef.current === null) {
|
|
581
|
+
clientRef.current = createTrackingClient({
|
|
582
|
+
apiKey,
|
|
583
|
+
endpoint,
|
|
584
|
+
surface: "next",
|
|
585
|
+
packageName: "@aranova/tracking-next",
|
|
586
|
+
debug
|
|
331
587
|
});
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
588
|
+
}
|
|
589
|
+
(0, import_react3.useEffect)(() => {
|
|
590
|
+
if (disableAutoPageView)
|
|
591
|
+
return;
|
|
592
|
+
const client = clientRef.current;
|
|
593
|
+
if (client === null)
|
|
594
|
+
return;
|
|
595
|
+
fireManualPageView(client);
|
|
596
|
+
}, [pathname, searchParams, disableAutoPageView]);
|
|
597
|
+
(0, import_react3.useEffect)(() => {
|
|
598
|
+
return () => {
|
|
599
|
+
clientRef.current?.destroy();
|
|
600
|
+
clientRef.current = null;
|
|
601
|
+
};
|
|
602
|
+
}, []);
|
|
603
|
+
const contextValue = (0, import_react3.useMemo)(() => clientRef.current, []);
|
|
604
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(TrackingContext.Provider, { value: contextValue, children });
|
|
341
605
|
}
|
|
342
|
-
function
|
|
343
|
-
const
|
|
344
|
-
|
|
606
|
+
function useTracking() {
|
|
607
|
+
const client = (0, import_react3.useContext)(TrackingContext);
|
|
608
|
+
if (client === null)
|
|
609
|
+
throw new Error("useTracking must be called inside a <TrackingProvider>");
|
|
610
|
+
return client;
|
|
345
611
|
}
|
|
346
612
|
// Annotate the CommonJS export names for ESM import in node:
|
|
347
613
|
0 && (module.exports = {
|
|
348
614
|
ConsentBanner,
|
|
349
615
|
GoogleAdsTracking,
|
|
350
616
|
TRACKING_PARAM_KEYS,
|
|
617
|
+
TrackingProvider,
|
|
351
618
|
captureTrackingParamsFromLocation,
|
|
352
619
|
createTrackingClientContext,
|
|
353
620
|
createTrackingEventCreatePayload,
|
|
354
|
-
createTrackingMiddleware,
|
|
355
621
|
createTrackingSessionUpsertPayload,
|
|
356
622
|
getConsentState,
|
|
357
|
-
getTrackingParamsServer,
|
|
358
623
|
setConsentState,
|
|
359
624
|
useConsentState,
|
|
360
625
|
useGclid,
|
|
626
|
+
useTracking,
|
|
361
627
|
useTrackingParams
|
|
362
628
|
});
|
|
363
629
|
//# sourceMappingURL=index.js.map
|