diff --git a/src/app/api/send/route.ts b/src/app/api/send/route.ts index 4b1db017..415b5a8e 100644 --- a/src/app/api/send/route.ts +++ b/src/app/api/send/route.ts @@ -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, diff --git a/src/tracker/index.js b/src/tracker/index.js index 6e3b65f1..aaf05a25 100644 --- a/src/tracker/index.js +++ b/src/tracker/index.js @@ -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;