🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
fix(tracker): fall back custom UTM traffic without HTTP referrer to Referral instead of Unknown (#1102)
Ganapathy S9597d068905d
diff --git a/server/src/services/tracker/getChannel.test.ts b/server/src/services/tracker/getChannel.test.tsnew file mode 100644index 000000000..12e611a19--- /dev/null+++ b/server/src/services/tracker/getChannel.test.ts@@ -0,0 +1,22 @@+import { describe, expect, it } from "vitest";+import { getChannel } from "./getChannel.js";++describe("getChannel - UTM parameter fallback", () => {+ it("classifies traffic with custom UTM parameters as Referral when no HTTP referrer is present", () => {+ expect(getChannel("", "utm_source=my_app&utm_medium=custom")).toBe("Referral");+ expect(getChannel("", "utm_source=gsuite_extension")).toBe("Referral");+ expect(getChannel("", "utm_medium=custom_link")).toBe("Referral");+ expect(getChannel("", "utm_campaign=custom_campaign")).toBe("Referral");+ });++ it("retains Direct classification when no referrer and no UTM parameters exist", () => {+ expect(getChannel("", "")).toBe("Direct");+ });++ it("retains Referral classification for external referring domains", () => {+ expect(getChannel("https://external-site.com/blog", "")).toBe("Referral");+ expect(+ getChannel("https://example.com/page", "utm_source=custom_source", "example.com"),+ ).not.toBe("Referral");+ });+});diff --git a/server/src/services/tracker/getChannel.ts b/server/src/services/tracker/getChannel.tsindex 46c0bee74..1e3990e7a 100644--- a/server/src/services/tracker/getChannel.ts+++ b/server/src/services/tracker/getChannel.ts@@ -187,6 +187,7 @@ export function getChannel(referrer: string, querystring: string, hostname?: strif ((referringDomain === "$direct" || (!referrer && !selfReferral)) &&!utmMedium &&+ !utmCampaign &&(!utmSource || utmSource === "direct" || utmSource === "(direct)")) {return "Direct";@@ -287,9 +288,11 @@ export function getChannel(referrer: string, querystring: string, hostname?: strif (/event|conference|webinar/.test(utmCampaign)) return "Event";if (/social|facebook|twitter|instagram|linkedin/.test(utmCampaign)) return "Organic Social";- // If referring domain exists but we couldn't categorize it- // Don't mark as referral if it's a self-referral- if (referringDomain && referringDomain !== "$direct" && !selfReferral) return "Referral";+ // If referring domain or UTM parameters exist but couldn't be categorized into a specific channel,+ // classify as Referral (unless it's a self-referral)+ if ((utmSource || utmMedium || utmCampaign || (referringDomain && referringDomain !== "$direct")) && !selfReferral) {+ return "Referral";+ }// Default fallbackreturn "Unknown";