Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Enhance channel detection and self-referral handling in tracking

Bill Yangecf69fb82894
diff --git a/server/src/tracker/getChannel.ts b/server/src/tracker/getChannel.ts
index 747e3c01f..a6888b949 100644
--- a/server/src/tracker/getChannel.ts
+++ b/server/src/tracker/getChannel.ts
@@ -53,10 +53,25 @@ function getDomainFromReferrer(referrer: string): string {
}
}
-export function getChannel(referrer: string, querystring: string): string {
+// Checks if the referrer is from the same site as the current hostname
+function isSelfReferral(referringDomain: string, hostname: string): boolean {
+ if (!referringDomain || !hostname) return false;
+ return referringDomain === hostname;
+}
+
+export function getChannel(
+ referrer: string,
+ querystring: string,
+ hostname?: string
+): string {
const utmParams = getUTMParams(querystring);
const referringDomain = getDomainFromReferrer(referrer);
+ // Check if this is a self-referral (internal navigation)
+ const selfReferral = hostname
+ ? isSelfReferral(referringDomain, hostname)
+ : false;
+
// UTM parameters
const utmSource = utmParams["utm_source"] || "";
const utmMedium = utmParams["utm_medium"] || "";
@@ -64,6 +79,18 @@ export function getChannel(referrer: string, querystring: string): string {
const gclid = utmParams["gclid"] || "";
const gadSource = utmParams["gad_source"] || "";
+ // If it's a self-referral and has no UTM parameters, treat it as internal traffic
+ if (
+ (selfReferral || !referrer) &&
+ !utmSource &&
+ !utmMedium &&
+ !utmCampaign &&
+ !gclid &&
+ !gadSource
+ ) {
+ return "Internal";
+ }
+
// Check if traffic is paid
const isPaid =
["cpc", "cpm", "cpv", "cpa", "ppc", "retargeting"].includes(utmMedium) ||
@@ -116,7 +143,7 @@ export function getChannel(referrer: string, querystring: string): string {
// Direct
if (
- referringDomain === "$direct" &&
+ (referringDomain === "$direct" || (!referrer && !selfReferral)) &&
!utmMedium &&
(!utmSource || utmSource === "direct" || utmSource === "(direct)")
) {
@@ -153,7 +180,9 @@ export function getChannel(referrer: string, querystring: string): string {
if (/shop|shopping/.test(utmCampaign)) return "Organic Shopping";
// If referring domain exists but we couldn't categorize it
- if (referringDomain && referringDomain !== "$direct") return "Referral";
+ // Don't mark as referral if it's a self-referral
+ if (referringDomain && referringDomain !== "$direct" && !selfReferral)
+ return "Referral";
// Default fallback
return "Unknown";
diff --git a/server/src/tracker/pageviewQueue.ts b/server/src/tracker/pageviewQueue.ts
index be297ab19..2cce30653 100644
--- a/server/src/tracker/pageviewQueue.ts
+++ b/server/src/tracker/pageviewQueue.ts
@@ -3,6 +3,7 @@ import clickhouse from "../db/clickhouse/clickhouse.js";
import { TrackingPayload } from "../types.js";
import { getDeviceType } from "../utils.js";
import { getChannel } from "./getChannel.js";
+import { clearSelfReferrer } from "./trackingUtils.js";
type TotalPayload = TrackingPayload & {
userId: string;
@@ -67,6 +68,9 @@ class PageviewQueue {
const longitude = dataForIp?.data?.longitude || 0;
const city = dataForIp?.data?.city || "";
+ // Check if referrer is from the same domain and clear it if so
+ let referrer = clearSelfReferrer(pv.referrer || "", pv.hostname || "");
+
return {
site_id: pv.site_id,
timestamp: DateTime.fromISO(pv.timestamp).toFormat(
@@ -78,8 +82,8 @@ class PageviewQueue {
pathname: pv.pathname || "",
querystring: pv.querystring || "",
page_title: pv.page_title || "",
- referrer: pv.referrer || "",
- channel: getChannel(pv.referrer, pv.querystring),
+ referrer: referrer,
+ channel: getChannel(referrer, pv.querystring, pv.hostname),
browser: pv.ua.browser.name || "",
browser_version: pv.ua.browser.major || "",
operating_system: pv.ua.os.name || "",
diff --git a/server/src/tracker/trackingUtils.ts b/server/src/tracker/trackingUtils.ts
index 951bce481..0642990b0 100644
--- a/server/src/tracker/trackingUtils.ts
+++ b/server/src/tracker/trackingUtils.ts
@@ -45,6 +45,23 @@ export type ActiveSession = {
referrer: string | null;
};
+// Clear referrer if it's from the same domain
+export function clearSelfReferrer(referrer: string, hostname: string): string {
+ if (!referrer || !hostname) return referrer;
+
+ try {
+ const referrerUrl = new URL(referrer);
+ if (referrerUrl.hostname === hostname) {
+ // Internal navigation, clear the referrer
+ return "";
+ }
+ } catch (e) {
+ // Invalid URL, return original referrer
+ }
+
+ return referrer;
+}
+
// Check if site is over the monthly limit
export function isSiteOverLimit(siteId: number | string): boolean {
return sitesOverLimit.has(Number(siteId));
@@ -128,7 +145,9 @@ export async function updateSession(
browser: payload.ua.browser.name || null,
operatingSystem: payload.ua.os.name || null,
language: payload.language || null,
- referrer: payload.referrer || null,
+ referrer: payload.hostname
+ ? clearSelfReferrer(payload.referrer || "", payload.hostname)
+ : payload.referrer || null,
};
await db.insert(activeSessions).values(insertData);