From 5750e6320cd7bd5c925bf52e503ca37f33da0491 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 18 Sep 2025 09:12:33 +0530 Subject: [PATCH] only track required params --- src/lib/actions/analytics.ts | 4 ++-- src/lib/helpers/utm.ts | 43 +++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/lib/actions/analytics.ts b/src/lib/actions/analytics.ts index 0db556971..31e6dd708 100644 --- a/src/lib/actions/analytics.ts +++ b/src/lib/actions/analytics.ts @@ -6,7 +6,7 @@ import { user } from '$lib/stores/user'; import { ENV, MODE, VARS, isCloud } from '$lib/system'; import { AppwriteException } from '@appwrite.io/console'; import { browser } from '$app/environment'; -import { getReferrerAndUtmSource, getAllQueryParams } from '$lib/helpers/utm'; +import { getReferrerAndUtmSource, getTrackedQueryParams } from '$lib/helpers/utm'; function plausible(domain: string): AnalyticsPlugin { if (!browser) return { name: 'analytics-plugin-plausible' }; @@ -65,7 +65,7 @@ export function trackEvent(name: string, data: object = null): void { }; } - data = { ...data, ...getReferrerAndUtmSource(), params: getAllQueryParams() }; + data = { ...data, ...getReferrerAndUtmSource(), ...getTrackedQueryParams() }; if (ENV.DEV || ENV.PREVIEW) { console.debug(`[Analytics] Event ${name} ${path}`, data); diff --git a/src/lib/helpers/utm.ts b/src/lib/helpers/utm.ts index 89eef85a9..d37abd88c 100644 --- a/src/lib/helpers/utm.ts +++ b/src/lib/helpers/utm.ts @@ -1,3 +1,17 @@ +const LINKEDIN_TRACKING_ID = 'li_fat_id'; +const GOOGLE_TRACKING_ID = 'gclid'; +const TWITTER_TRACKING_ID = 'twclid'; +const REDDIT_TRACKING_ID = 'rdt_cid'; + +const TRACKED_QUERY_PARAMS = [ + LINKEDIN_TRACKING_ID, + GOOGLE_TRACKING_ID, + TWITTER_TRACKING_ID, + REDDIT_TRACKING_ID +] as const; + +type TrackedParams = Record<(typeof TRACKED_QUERY_PARAMS)[number], string>; + export function getReferrerAndUtmSource() { if (sessionStorage) { let values = {}; @@ -16,26 +30,25 @@ export function getReferrerAndUtmSource() { return {}; } -export function getAllQueryParams() { - if (typeof window !== 'undefined' && window.location) { +export function getTrackedQueryParams(): Partial { + if (typeof window === 'undefined' || !window.location) { + return {}; + } + + try { const urlParams = new URLSearchParams(window.location.search); - const params = {}; + const params: Partial = {}; - const utmParams = new Set([ - 'utm_source', - 'utm_medium', - 'utm_campaign', - 'utm_term', - 'utm_content' - ]); - - for (const [key, value] of urlParams.entries()) { - if (!utmParams.has(key.toLowerCase())) { - params[key] = value; + for (const paramName of TRACKED_QUERY_PARAMS) { + const value = urlParams.get(paramName); + if (value) { + params[paramName] = value; } } return params; + } catch (error) { + console.warn('Failed to parse URL search parameters:', error); + return {}; } - return {}; }