-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathwith-analytics-cookies.ts
More file actions
53 lines (42 loc) · 1.74 KB
/
Copy pathwith-analytics-cookies.ts
File metadata and controls
53 lines (42 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { validate as isUuid, v4 as uuidv4 } from 'uuid';
import {
getVisitIdCookie,
getVisitorIdCookie,
setVisitIdCookie,
setVisitorIdCookie,
} from '~/lib/analytics/bigcommerce';
import { sendVisitStartedEvent } from '~/lib/analytics/bigcommerce/data-events';
import { MiddlewareFactory } from './compose-middlewares';
export const withAnalyticsCookies: MiddlewareFactory = (next) => {
return async (request, event) => {
const existingVisitorId = await getVisitorIdCookie();
const existingVisitId = await getVisitIdCookie();
const isPrefetch = request.headers.get('Next-Router-Prefetch') === '1';
const isRSC = request.headers.get('RSC') === '1';
const visitorId = existingVisitorId && isUuid(existingVisitorId) ? existingVisitorId : uuidv4();
await setVisitorIdCookie(visitorId);
const hasValidVisit = existingVisitId != null && isUuid(existingVisitId);
if (hasValidVisit) {
// Sliding window: refresh the TTL on every request
await setVisitIdCookie(existingVisitId);
} else if (!isPrefetch && !isRSC) {
// New visit on a real navigation: create cookie and fire event
const visitId = uuidv4();
await setVisitIdCookie(visitId);
event.waitUntil(recordNewVisit(request, visitorId, visitId));
}
// Prefetch/RSC with no valid visit: skip entirely so the
// subsequent real navigation properly detects a new visit.
return next(request, event);
};
};
async function recordNewVisit(request: Request, visitorId: string, visitId: string) {
await sendVisitStartedEvent({
initiator: { visitId, visitorId },
request: {
url: request.url,
refererUrl: request.headers.get('referer') || '',
userAgent: request.headers.get('user-agent') || '',
},
});
}