23 lines
620 B
TypeScript
23 lines
620 B
TypeScript
import "server-only";
|
|
|
|
import { getVisitorTrackingConfig } from "@/lib/config/server";
|
|
import { getRedisClient } from "@/lib/redis";
|
|
import {
|
|
getClientAddress,
|
|
hashClientAddress,
|
|
} from "@/lib/visitor/visitor-identifiers";
|
|
|
|
const VISITOR_SET_KEY = "erika:unique_visitors:v2";
|
|
|
|
export async function trackUniqueVisitor(headers: Headers) {
|
|
const client = await getRedisClient();
|
|
const address = getClientAddress(headers);
|
|
|
|
if (address) {
|
|
const { secret } = getVisitorTrackingConfig();
|
|
await client.sadd(VISITOR_SET_KEY, hashClientAddress(address, secret));
|
|
}
|
|
|
|
return client.scard(VISITOR_SET_KEY);
|
|
}
|