fix(home): preserve legacy visitor totals
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
} from "@/lib/visitor/visitor-identifiers";
|
||||
|
||||
const VISITOR_SET_KEY = "erika:unique_visitors:v2";
|
||||
const LEGACY_VISITOR_SET_KEY = "erika:unique_visitors";
|
||||
|
||||
export async function trackUniqueVisitor(headers: Headers) {
|
||||
const client = await getRedisClient();
|
||||
@@ -15,8 +16,23 @@ export async function trackUniqueVisitor(headers: Headers) {
|
||||
|
||||
if (address) {
|
||||
const { secret } = getVisitorTrackingConfig();
|
||||
await client.sadd(VISITOR_SET_KEY, hashClientAddress(address, secret));
|
||||
const identifier = hashClientAddress(address, secret);
|
||||
const [isLegacyVisitor, isPrivateVisitor] = await Promise.all([
|
||||
client.sismember(LEGACY_VISITOR_SET_KEY, address),
|
||||
client.sismember(VISITOR_SET_KEY, identifier),
|
||||
]);
|
||||
|
||||
if (isLegacyVisitor) {
|
||||
// Keep the legacy visitor in the old total without counting a revisit twice.
|
||||
if (isPrivateVisitor) await client.srem(VISITOR_SET_KEY, identifier);
|
||||
} else {
|
||||
await client.sadd(VISITOR_SET_KEY, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
return client.scard(VISITOR_SET_KEY);
|
||||
const [legacyCount, privateCount] = await Promise.all([
|
||||
client.scard(LEGACY_VISITOR_SET_KEY),
|
||||
client.scard(VISITOR_SET_KEY),
|
||||
]);
|
||||
return legacyCount + privateCount;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user