sdk api: avoid getting referrer is same domain
This commit is contained in:
@@ -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(
|
export async function postEvent(
|
||||||
request: FastifyRequest<{
|
request: FastifyRequest<{
|
||||||
Body: PostEventPayload;
|
Body: PostEventPayload;
|
||||||
@@ -59,8 +70,11 @@ export async function postEvent(
|
|||||||
const projectId = request.projectId;
|
const projectId = request.projectId;
|
||||||
const body = request.body;
|
const body = request.body;
|
||||||
const createdAt = new Date(body.timestamp);
|
const createdAt = new Date(body.timestamp);
|
||||||
const { path, hash, query } = parsePath(body.properties?.path);
|
const url = body.properties?.path;
|
||||||
const referrer = parseReferrer(body.properties?.referrer);
|
const { path, hash, query } = parsePath(url);
|
||||||
|
const referrer = isSameDomain(body.properties?.referrer, url)
|
||||||
|
? null
|
||||||
|
: parseReferrer(body.properties?.referrer);
|
||||||
const utmReferrer = getReferrerWithQuery(query);
|
const utmReferrer = getReferrerWithQuery(query);
|
||||||
const ip = getClientIp(request)!;
|
const ip = getClientIp(request)!;
|
||||||
const origin = request.headers.origin!;
|
const origin = request.headers.origin!;
|
||||||
@@ -85,8 +99,6 @@ export async function postEvent(
|
|||||||
eventsQueue.getJobs(['delayed']),
|
eventsQueue.getJobs(['delayed']),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
console.log('----->', projectId, ip, geo);
|
|
||||||
|
|
||||||
// find session_end job
|
// find session_end job
|
||||||
const sessionEndJobCurrentProfileId = findJobByPrefix(
|
const sessionEndJobCurrentProfileId = findJobByPrefix(
|
||||||
eventsJobs,
|
eventsJobs,
|
||||||
@@ -151,9 +163,9 @@ export async function postEvent(
|
|||||||
model: uaInfo.model,
|
model: uaInfo.model,
|
||||||
duration: 0,
|
duration: 0,
|
||||||
path: path,
|
path: path,
|
||||||
referrer: referrer.url,
|
referrer: referrer?.url,
|
||||||
referrerName: referrer.name ?? utmReferrer?.name ?? '',
|
referrerName: referrer?.name ?? utmReferrer?.name ?? '',
|
||||||
referrerType: referrer.type ?? utmReferrer?.type ?? '',
|
referrerType: referrer?.type ?? utmReferrer?.type ?? '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const job = findJobByPrefix(eventsJobs, `event:${projectId}:${profileId}:`);
|
const job = findJobByPrefix(eventsJobs, `event:${projectId}:${profileId}:`);
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ function getHostname(url: string | undefined) {
|
|||||||
export function parseReferrer(url: string | undefined) {
|
export function parseReferrer(url: string | undefined) {
|
||||||
const match = referrers[getHostname(url)];
|
const match = referrers[getHostname(url)];
|
||||||
|
|
||||||
console.log('Parsing referrer', url);
|
|
||||||
console.log('Match', match);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: match?.name ?? '',
|
name: match?.name ?? '',
|
||||||
type: match?.type ?? 'unknown',
|
type: match?.type ?? 'unknown',
|
||||||
|
|||||||
Reference in New Issue
Block a user