Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more reliable fingerprint #1721

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 13 additions & 15 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import crypto from 'node:crypto';

import { posthogServerClient } from '$lib/experiments';
import { getAllChangelogEntries } from './changelog/utils';

export const trailingSlash = 'never';

const generateDistinctId = (fingerprintData: Record<string, string>) => {
return crypto.createHash('sha256').update(JSON.stringify(fingerprintData)).digest('hex');
const generateDistinctId = (request: Request, clientAddress?: string) => {
const headers = Object.fromEntries(request.headers);

const identifiers = [
clientAddress || 'unknown-ip',
Copy link
Member

Choose a reason for hiding this comment

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

what about x-forwarded-for from headers?

headers['user-agent'] || 'unknown-ua',

!clientAddress ? Math.random().toString() : '',
request.headers.get('cookie')?.match(/sessionid=([^;]+)/)?.[1] || ''
].join('|');

return crypto.createHash('sha256').update(identifiers).digest('hex');
};

export const load = async ({ request, getClientAddress }) => {
const clientAddress = getClientAddress();
const headers = Object.fromEntries(request.headers);
const fingerprintData = {
ip: clientAddress,
userAgent: headers['user-agent'],
acceptLanguage: headers['accept-language'],
platform: headers['sec-ch-ua-platform'],
mobile: headers['sec-ch-ua-mobile'],
browserBrand: headers['sec-ch-ua']
};

const distinctId = generateDistinctId(fingerprintData);
const distinctId = generateDistinctId(request, getClientAddress());
const ctaVariant = await posthogServerClient?.getFeatureFlag('cta-copy_ab-test', distinctId);

const ctaCopy =
Expand Down