Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-analytics-visit-inflation.md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added a changeset and rebased the PR.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Fix analytics visit count inflation by implementing a sliding window for the visit cookie TTL, guarding against prefetch/RSC requests creating spurious visits, and reordering middleware so analytics cookies survive locale redirects.
2 changes: 1 addition & 1 deletion core/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { withRoutes } from './middlewares/with-routes';

export const middleware = composeMiddlewares(
withAuth,
withIntl,
withAnalyticsCookies,
withIntl,
withChannelId,
withRoutes,
);
Expand Down
26 changes: 17 additions & 9 deletions core/middlewares/with-analytics-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ import { MiddlewareFactory } from './compose-middlewares';

export const withAnalyticsCookies: MiddlewareFactory = (next) => {
return async (request, event) => {
let visitorId = await getVisitorIdCookie();
let visitId = await getVisitIdCookie();
const existingVisitorId = await getVisitorIdCookie();
const existingVisitId = await getVisitIdCookie();

if (!visitorId || !isUuid(visitorId)) {
visitorId = uuidv4();
}
const isPrefetch = request.headers.get('Next-Router-Prefetch') === '1';
const isRSC = request.headers.get('RSC') === '1';

const visitorId = existingVisitorId && isUuid(existingVisitorId) ? existingVisitorId : uuidv4();

// Update the visitorId cookie every time
await setVisitorIdCookie(visitorId);

if (!visitId || !isUuid(visitId)) {
visitId = uuidv4();
await setVisitIdCookie(visitId);
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);
};
Expand Down
7 changes: 6 additions & 1 deletion core/middlewares/with-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,12 @@ export const withRoutes: MiddlewareFactory = () => {
case 'Product': {
url = `/${locale}/product/${node.entityId}`;

event.waitUntil(recordProductVisit(request, node.entityId));
const isPrefetch = request.headers.get('Next-Router-Prefetch') === '1';
const isRSC = request.headers.get('RSC') === '1';

if (!isPrefetch && !isRSC) {
event.waitUntil(recordProductVisit(request, node.entityId));
}

break;
}
Expand Down
Loading