Updated tracker to send full url.

This commit is contained in:
Mike Cao 2025-02-07 23:10:27 -08:00
parent 5c2cb97070
commit a2a39b7ecd
2 changed files with 33 additions and 29 deletions

View file

@ -1,7 +1,6 @@
import { z } from 'zod';
import { isbot } from 'isbot';
import { createToken, parseToken } from '@/lib/jwt';
import { safeDecodeURI } from '@/lib/url';
import clickhouse from '@/lib/clickhouse';
import { parseRequest } from '@/lib/request';
import { badRequest, json, forbidden, serverError } from '@/lib/response';
@ -129,26 +128,32 @@ export async function POST(request: Request) {
}
if (type === COLLECTION_TYPE.event) {
// eslint-disable-next-line prefer-const
let [urlPath, urlQuery] = safeDecodeURI(url)?.split('?') || [];
let [referrerPath, referrerQuery] = safeDecodeURI(referrer)?.split('?') || [];
let referrerDomain = '';
const base = hostname ? `http://${hostname}` : 'http://localhost';
const currentUrl = new URL(url, base);
if (!urlPath) {
urlPath = '/';
}
if (/^[\w-]+:\/\/\w+/.test(referrerPath)) {
const refUrl = new URL(referrer);
referrerPath = refUrl.pathname;
referrerQuery = refUrl.search.substring(1);
referrerDomain = refUrl.hostname.replace(/www\./, '');
}
let urlPath = currentUrl.pathname;
const urlQuery = currentUrl.search.substring(1);
const urlDomain = currentUrl.hostname.replace(/^www\./, '');
if (process.env.REMOVE_TRAILING_SLASH) {
urlPath = urlPath.replace(/(.+)\/$/, '$1');
}
let referrerPath: string;
let referrerQuery: string;
let referrerDomain: string;
if (referrer) {
const referrerUrl = new URL(referrer, base);
referrerPath = referrerUrl.pathname;
referrerQuery = referrerUrl.search.substring(1);
if (referrerUrl.hostname !== 'localhost') {
referrerDomain = referrerUrl.hostname.replace(/^www\./, '');
}
}
await saveEvent({
websiteId,
sessionId,
@ -161,7 +166,7 @@ export async function POST(request: Request) {
pageTitle: title,
eventName: name,
eventData: data,
hostname,
hostname: hostname || urlDomain,
browser,
os,
device,

View file

@ -35,19 +35,8 @@
/* Helper functions */
const parseURL = url => {
try {
const { pathname, search, hash } = new URL(url, location.href);
return pathname + (excludeSearch ? '' : search) + (excludeHash ? '' : hash);
} catch (e) {
return url;
}
};
const getPayload = () => ({
website,
hostname,
screen,
language,
title,
@ -62,7 +51,17 @@
if (!url) return;
currentRef = currentUrl;
currentUrl = parseURL(url.toString());
currentUrl = new URL(url, location.href);
if (excludeSearch) {
currentUrl.search = '';
}
if (excludeHash) {
currentUrl.hash = '';
}
currentUrl = currentUrl.toString();
if (currentUrl !== currentRef) {
setTimeout(track, delayDuration);
@ -250,7 +249,7 @@
};
}
let currentUrl = parseURL(href);
let currentUrl = href;
let currentRef = referrer.startsWith(origin) ? '' : referrer;
let title = document.title;
let cache;