Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

sdk api: avoid getting referrer is same domain

Carl-Gerhard Lindesvärd034be63ac0d4
diff --git a/apps/sdk-api/src/controllers/event.controller.ts b/apps/sdk-api/src/controllers/event.controller.ts
index 1198b29cb..1ca35148b 100644
--- a/apps/sdk-api/src/controllers/event.controller.ts
+++ b/apps/sdk-api/src/controllers/event.controller.ts
@@ -49,6 +49,17 @@ function parsePath(path?: string): {
}
}
+function isSameDomain(url1: string | undefined, url2: string | undefined) {
+ if (!url1 || !url2) {
+ return false;
+ }
+ try {
+ return new URL(url1).hostname === new URL(url2).hostname;
+ } catch (e) {
+ return false;
+ }
+}
+
export async function postEvent(
request: FastifyRequest<{
Body: PostEventPayload;
@@ -59,8 +70,11 @@ export async function postEvent(
const projectId = request.projectId;
const body = request.body;
const createdAt = new Date(body.timestamp);
- const { path, hash, query } = parsePath(body.properties?.path);
- const referrer = parseReferrer(body.properties?.referrer);
+ const url = body.properties?.path;
+ const { path, hash, query } = parsePath(url);
+ const referrer = isSameDomain(body.properties?.referrer, url)
+ ? null
+ : parseReferrer(body.properties?.referrer);
const utmReferrer = getReferrerWithQuery(query);
const ip = getClientIp(request)!;
const origin = request.headers.origin!;
@@ -85,8 +99,6 @@ export async function postEvent(
eventsQueue.getJobs(['delayed']),
]);
- console.log('----->', projectId, ip, geo);
-
// find session_end job
const sessionEndJobCurrentProfileId = findJobByPrefix(
eventsJobs,
@@ -151,9 +163,9 @@ export async function postEvent(
model: uaInfo.model,
duration: 0,
path: path,
- referrer: referrer.url,
- referrerName: referrer.name ?? utmReferrer?.name ?? '',
- referrerType: referrer.type ?? utmReferrer?.type ?? '',
+ referrer: referrer?.url,
+ referrerName: referrer?.name ?? utmReferrer?.name ?? '',
+ referrerType: referrer?.type ?? utmReferrer?.type ?? '',
};
const job = findJobByPrefix(eventsJobs, `event:${projectId}:${profileId}:`);
diff --git a/apps/sdk-api/src/utils/parseReferrer.ts b/apps/sdk-api/src/utils/parseReferrer.ts
index ca9cf398f..5e481de69 100644
--- a/apps/sdk-api/src/utils/parseReferrer.ts
+++ b/apps/sdk-api/src/utils/parseReferrer.ts
@@ -15,9 +15,6 @@ function getHostname(url: string | undefined) {
export function parseReferrer(url: string | undefined) {
const match = referrers[getHostname(url)];
- console.log('Parsing referrer', url);
- console.log('Match', match);
-
return {
name: match?.name ?? '',
type: match?.type ?? 'unknown',