Skip to content

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

feat(tracker): expand domain and source categorization for modern platforms

Bill Yange16f986f71d4
diff --git a/server/src/tracker/channelExamples.ts b/server/src/tracker/channelExamples.ts
new file mode 100644
index 000000000..40b2a10c1
--- /dev/null
+++ b/server/src/tracker/channelExamples.ts
@@ -0,0 +1,175 @@
+import { getChannel, getChannelDetails } from "./getChannel.js";
+import { getSourceType, getMediumType, isPaidTraffic } from "./const.js";
+
+/**
+ * Examples of how to use the enhanced channel detection functions
+ */
+
+// Example 1: Basic channel detection
+export function exampleBasicChannelDetection() {
+ const examples = [
+ {
+ referrer: "https://google.com/search?q=analytics",
+ querystring: "",
+ expected: "Organic Search",
+ },
+ {
+ referrer: "https://facebook.com",
+ querystring:
+ "utm_source=facebook&utm_medium=social&utm_campaign=awareness",
+ expected: "Organic Social",
+ },
+ {
+ referrer: "",
+ querystring:
+ "utm_source=google&utm_medium=cpc&utm_campaign=brand&gclid=123",
+ expected: "Paid Search",
+ },
+ {
+ referrer: "https://instagram.com",
+ querystring:
+ "utm_source=instagram&utm_medium=influencer&utm_campaign=creator_collab",
+ expected: "Influencer",
+ },
+ ];
+
+ examples.forEach(({ referrer, querystring, expected }) => {
+ const channel = getChannel(referrer, querystring);
+ console.log(`Channel: ${channel} (Expected: ${expected})`);
+ });
+}
+
+// Example 2: Detailed channel analysis
+export function exampleDetailedAnalysis() {
+ const referrer = "https://youtube.com/watch?v=abc123";
+ const querystring =
+ "utm_source=youtube&utm_medium=video&utm_campaign=product_demo";
+
+ const details = getChannelDetails(referrer, querystring);
+
+ console.log("Detailed Analysis:", {
+ channel: details.channel, // "Organic Video"
+ sourceType: details.sourceType, // "video"
+ mediumType: details.mediumType, // "video"
+ isPaid: details.isPaid, // false
+ isMobile: details.isMobile, // false
+ source: details.details.source, // "youtube"
+ medium: details.details.medium, // "video"
+ campaign: details.details.campaign, // "product_demo"
+ });
+}
+
+// Example 3: Mobile app tracking
+export function exampleMobileAppTracking() {
+ const mobileExamples = [
+ {
+ referrer: "",
+ querystring: "utm_source=com.instagram.android&utm_medium=social",
+ description: "Instagram Android app",
+ },
+ {
+ referrer: "",
+ querystring: "utm_source=com.google.android.youtube&utm_medium=video",
+ description: "YouTube Android app",
+ },
+ {
+ referrer: "",
+ querystring: "utm_source=com.nytimes.android&utm_medium=news",
+ description: "NY Times Android app",
+ },
+ ];
+
+ mobileExamples.forEach(({ referrer, querystring, description }) => {
+ const details = getChannelDetails(referrer, querystring);
+ console.log(`${description}:`, {
+ channel: details.channel,
+ isMobile: details.isMobile,
+ sourceType: details.sourceType,
+ });
+ });
+}
+
+// Example 4: Paid vs Organic detection
+export function examplePaidVsOrganic() {
+ const trafficExamples = [
+ "utm_medium=cpc",
+ "utm_medium=social",
+ "utm_medium=influencer&utm_source=sponsored_post",
+ "utm_medium=email",
+ "gclid=abc123",
+ "utm_medium=display&utm_campaign=retargeting",
+ ];
+
+ trafficExamples.forEach((querystring) => {
+ const details = getChannelDetails("", querystring);
+ console.log(
+ `${querystring} -> ${details.channel} (Paid: ${details.isPaid})`
+ );
+ });
+}
+
+// Example 5: Using utility functions directly
+export function exampleUtilityFunctions() {
+ // Source type detection
+ console.log("Source Types:");
+ console.log("google.com ->", getSourceType("google.com")); // "search"
+ console.log("facebook.com ->", getSourceType("facebook.com")); // "social"
+ console.log("youtube.com ->", getSourceType("youtube.com")); // "video"
+ console.log("amazon.com ->", getSourceType("amazon.com")); // "shopping"
+
+ // Medium type detection
+ console.log("\nMedium Types:");
+ console.log("cpc ->", getMediumType("cpc")); // "cpc"
+ console.log("social ->", getMediumType("social")); // "social"
+ console.log("influencer ->", getMediumType("influencer")); // "influencer"
+ console.log("email ->", getMediumType("email")); // "email"
+
+ // Paid traffic detection
+ console.log("\nPaid Traffic Detection:");
+ console.log("cpc + google ->", isPaidTraffic("cpc", "google")); // true
+ console.log("social + facebook ->", isPaidTraffic("social", "facebook")); // false
+ console.log(
+ "sponsored + instagram ->",
+ isPaidTraffic("sponsored", "instagram")
+ ); // true
+}
+
+// Example 6: Advanced campaign analysis
+export function exampleCampaignAnalysis() {
+ const campaigns = [
+ {
+ name: "Google Ads Campaign",
+ referrer: "",
+ querystring:
+ "utm_source=google&utm_medium=cpc&utm_campaign=brand_keywords&gclid=123",
+ },
+ {
+ name: "Influencer Partnership",
+ referrer: "https://instagram.com",
+ querystring:
+ "utm_source=instagram&utm_medium=influencer&utm_campaign=summer_collection&utm_content=creator_jane",
+ },
+ {
+ name: "Content Marketing",
+ referrer: "https://techcrunch.com",
+ querystring:
+ "utm_source=techcrunch&utm_medium=content&utm_campaign=thought_leadership",
+ },
+ {
+ name: "Event Traffic",
+ referrer: "",
+ querystring:
+ "utm_source=conference&utm_medium=event&utm_campaign=tech_summit_2024",
+ },
+ ];
+
+ campaigns.forEach(({ name, referrer, querystring }) => {
+ const details = getChannelDetails(referrer, querystring);
+ console.log(`\n${name}:`);
+ console.log(` Channel: ${details.channel}`);
+ console.log(` Source Type: ${details.sourceType}`);
+ console.log(` Medium Type: ${details.mediumType}`);
+ console.log(` Is Paid: ${details.isPaid}`);
+ console.log(` Campaign: ${details.details.campaign}`);
+ });
+}
diff --git a/server/src/tracker/const.ts b/server/src/tracker/const.ts
index 61824cc91..5d0a5a97a 100644
--- a/server/src/tracker/const.ts
+++ b/server/src/tracker/const.ts
@@ -54,6 +54,14 @@ export const searchDomains = [
"sogou.com",
"goo.ne.jp",
"rambler.ru",
+
+ // AI-powered search engines
+ "perplexity.ai",
+ "you.com",
+ "phind.com",
+ "andi.com",
+ "neeva.com",
+ "kagi.com",
];
export const socialDomains = [
@@ -106,6 +114,33 @@ export const socialDomains = [
"news.ycombinator.com",
"hn.algolia.com",
+ // Newer/emerging platforms
+ "bluesky.app",
+ "bsky.app",
+ "post.news",
+ "truth.social",
+ "truthsocial.com",
+ "gettr.com",
+ "parler.com",
+ "gab.com",
+ "minds.com",
+ "diaspora.",
+ "lemmy.",
+ "kbin.social",
+ "bereal.com",
+ "vsco.co",
+ "flickr.com",
+ "flic.kr",
+ "500px.com",
+ "deviantart.com",
+ "behance.net",
+ "dribbble.com",
+
+ // Professional networks
+ "xing.com",
+ "viadeo.com",
+ "meetup.com",
+
// Regional social platforms
"wechat.com",
"weixin.qq.com",
@@ -114,6 +149,10 @@ export const socialDomains = [
"weibo.com",
"weibo.cn",
"line.me",
+ "kakaotalk.com",
+ "viber.com",
+ "ok.ru",
+ "odnoklassniki.ru",
];
export const videoDomains = [
@@ -129,6 +168,7 @@ export const videoDomains = [
"disneyplus.com",
"hulu.com",
"hbomax.com",
+ "max.com",
"peacocktv.com",
"primevideo.com",
"amazon.com/prime-video",
@@ -138,6 +178,8 @@ export const videoDomains = [
"funimation.com",
"curiositystream.com",
"mubi.com",
+ "appletv.com",
+ "tv.apple.com",
// Video sharing
"tiktok.com",
@@ -166,6 +208,7 @@ export const videoDomains = [
"youtube.com/live",
"kick.com",
"caffeine.tv",
+ "mixer.com",
// Regional platforms
"bilibili.com",
@@ -174,6 +217,12 @@ export const videoDomains = [
"youku.com",
"tudou.com",
"iqiyi.com",
+
+ // Short-form video
+ "vine.co",
+ "byte.co",
+ "triller.co",
+ "likee.video",
];
export const shoppingDomains = [
@@ -201,6 +250,11 @@ export const shoppingDomains = [
"net-a-porter.com",
"farfetch.com",
"fashionnova.com",
+ "shein.com",
+ "romwe.com",
+ "zaful.com",
+ "boohoo.com",
+ "prettylittlething.com",
// Electronics
"bestbuy.com",
@@ -208,6 +262,8 @@ export const shoppingDomains = [
"bhphotovideo.com",
"apple.com/shop",
"samsung.com/shop",
+ "microcenter.com",
+ "tigerdirect.com",
// Home and furniture
"wayfair.com",
@@ -215,6 +271,9 @@ export const shoppingDomains = [
"homedepot.com",
"lowes.com",
"overstock.com",
+ "cb2.com",
+ "crateandbarrel.com",
+ "westelm.com",
// Regional marketplaces
"taobao.com",
@@ -227,6 +286,9 @@ export const shoppingDomains = [
"allegro.pl",
"coupang.com",
"gmarket.co.kr",
+ "shopee.com",
+ "tokopedia.com",
+ "bukalapak.com",
// Grocery and food
"instacart.com",
@@ -237,6 +299,9 @@ export const shoppingDomains = [
"ubereats.com",
"grubhub.com",
"deliveroo.com",
+ "postmates.com",
+ "seamless.com",
+ "foodpanda.com",
];
// Define sources types
@@ -275,7 +340,16 @@ export const searchSources = [
"sogou",
"goo",
"rambler",
+
+ // AI-powered search engines
+ "perplexity",
+ "you",
+ "phind",
+ "andi",
+ "neeva",
+ "kagi",
];
+
export const socialSources = [
// Major social platforms
"facebook",
@@ -313,6 +387,31 @@ export const socialSources = [
"tw",
"yt",
+ // Newer/emerging platforms
+ "bluesky",
+ "bsky",
+ "post",
+ "truth social",
+ "gettr",
+ "parler",
+ "gab",
+ "minds",
+ "diaspora",
+ "lemmy",
+ "kbin",
+ "bereal",
+ "vsco",
+ "flickr",
+ "500px",
+ "deviantart",
+ "behance",
+ "dribbble",
+
+ // Professional networks
+ "xing",
+ "viadeo",
+ "meetup",
+
// Regional platforms
"wechat",
"weixin",
@@ -321,7 +420,12 @@ export const socialSources = [
"weibo",
"line",
"kakao",
+ "kakaotalk",
+ "viber",
+ "ok",
+ "odnoklassniki",
];
+
export const videoSources = [
// Major video platforms
"youtube",
@@ -335,6 +439,7 @@ export const videoSources = [
"netflix",
"hulu",
"hbomax",
+ "max",
"peacock",
"prime video",
"paramount+",
@@ -343,6 +448,8 @@ export const videoSources = [
"funimation",
"curiositystream",
"mubi",
+ "apple tv",
+ "appletv",
// Video sharing
"vevo",
@@ -366,6 +473,7 @@ export const videoSources = [
// Live streaming
"kick",
"caffeine",
+ "mixer",
// Regional platforms
"bilibili",
@@ -374,7 +482,14 @@ export const videoSources = [
"youku",
"tudou",
"iqiyi",
+
+ // Short-form video
+ "vine",
+ "byte",
+ "triller",
+ "likee",
];
+
export const shoppingSources = [
// Major marketplaces
"amazon",
@@ -400,6 +515,11 @@ export const shoppingSources = [
"net-a-porter",
"farfetch",
"fashionnova",
+ "shein",
+ "romwe",
+ "zaful",
+ "boohoo",
+ "prettylittlething",
// Electronics
"bestbuy",
@@ -407,6 +527,8 @@ export const shoppingSources = [
"bhphotovideo",
"apple",
"samsung",
+ "microcenter",
+ "tigerdirect",
// Home and furniture
"wayfair",
@@ -414,6 +536,9 @@ export const shoppingSources = [
"homedepot",
"lowes",
"overstock",
+ "cb2",
+ "crateandbarrel",
+ "westelm",
// Regional marketplaces
"taobao",
@@ -426,6 +551,9 @@ export const shoppingSources = [
"allegro",
"coupang",
"gmarket",
+ "shopee",
+ "tokopedia",
+ "bukalapak",
// Grocery and food
"instacart",
@@ -436,7 +564,11 @@ export const shoppingSources = [
"ubereats",
"grubhub",
"deliveroo",
+ "postmates",
+ "seamless",
+ "foodpanda",
];
+
export const emailSources = [
"email",
"e_mail",
@@ -466,6 +598,7 @@ export const emailSources = [
"blast",
"campaign",
];
+
export const smsSources = [
"sms",
"text",
@@ -627,6 +760,80 @@ export const audioMediums = [
"commercial",
];
+// New medium categories for modern marketing
+export const influencerMediums = [
+ "influencer",
+ "creator",
+ "sponsored",
+ "collaboration",
+ "brand-ambassador",
+ "micro-influencer",
+ "macro-influencer",
+ "nano-influencer",
+ "ugc-creator",
+ "content-creator",
+ "partnership",
+ "endorsement",
+];
+
+export const cpcMediums = [
+ "cpc",
+ "ppc",
+ "paid-search",
+ "search-ads",
+ "google-ads",
+ "bing-ads",
+ "yahoo-ads",
+ "adwords",
+ "sem",
+ "paid-click",
+];
+
+export const cpmMediums = [
+ "cpm",
+ "display-ads",
+ "banner-ads",
+ "impression",
+ "programmatic",
+ "rtb",
+ "dsp",
+ "demand-side",
+ "supply-side",
+ "ad-exchange",
+];
+
+export const contentMediums = [
+ "content",
+ "blog",
+ "article",
+ "guest-post",
+ "editorial",
+ "pr",
+ "press-release",
+ "news",
+ "publication",
+ "magazine",
+ "journal",
+ "whitepaper",
+ "case-study",
+ "resource",
+];
+
+export const eventMediums = [
+ "event",
+ "conference",
+ "webinar",
+ "workshop",
+ "seminar",
+ "meetup",
+ "trade-show",
+ "expo",
+ "summit",
+ "networking",
+ "live-event",
+ "virtual-event",
+];
+
// Mobile App IDs (reverse DNS format)
export const socialAppIds = [
// Facebook family
@@ -680,7 +887,7 @@ export const socialAppIds = [
// WhatsApp
"com.whatsapp",
- // Thread
+ // Threads
"com.instagram.barcelona",
"com.threads",
@@ -690,9 +897,17 @@ export const socialAppIds = [
// Mastodon
"org.joinmastodon.android",
+ // BeReal
+ "com.bereal.ft",
+
+ // VSCO
+ "com.vsco.cam",
+
// Other social
"com.slack",
"im.vector.app",
+ "com.nextdoor",
+ "com.clubhouse.app",
];
export const videoAppIds = [
@@ -811,12 +1026,67 @@ export const shoppingAppIds = [
// AliExpress
"com.alibaba.aliexpresshd",
+ // Shein
+ "com.zzkko",
+
// Other shopping
"com.wayfair.wayfair",
"com.newegg.app",
"com.bestbuy.android",
"com.ikea.app",
"com.homedepot",
+ "com.lowes.android",
+ "com.overstock",
+];
+
+// News and content app IDs
+export const newsAppIds = [
+ // Major news apps
+ "com.cnn.mobile.android.phone",
+ "com.foxnews.android",
+ "com.nytimes.android",
+ "com.washingtonpost.rainbow",
+ "com.wsj.reader",
+ "com.usatoday.android.news",
+ "com.bbc.news",
+ "com.reuters.android",
+ "com.ap.mobile",
+ "com.nbcuni.nbc",
+
+ // Tech news
+ "com.aol.mobile.techcrunch",
+ "com.theverge.verge",
+ "com.wired.android",
+ "com.arstechnica.app",
+ "com.engadget.android",
+
+ // Aggregators
+ "com.google.android.apps.magazines",
+ "flipboard.app",
+ "com.apple.news",
+ "com.microsoft.amp.apps.bingnews",
+];
+
+// Productivity app IDs
+export const productivityAppIds = [
+ // Microsoft Office
+ "com.microsoft.office.word",
+ "com.microsoft.office.excel",
+ "com.microsoft.office.powerpoint",
+ "com.microsoft.teams",
+
+ // Google Workspace
+ "com.google.android.apps.docs",
+ "com.google.android.apps.docs.editors.sheets",
+ "com.google.android.apps.docs.editors.slides",
+ "com.google.android.apps.meetings",
+
+ // Note-taking
+ "com.evernote",
+ "us.zoom.videomeetings",
+ "com.notion.id",
+ "md.obsidian",
+ "com.dropbox.android",
];
// Categorize mobile apps by their bundle ID/package name - helper function
@@ -824,3 +1094,99 @@ export function isMobileAppId(source: string): boolean {
// Check for common app identifier patterns (com.company.app, etc.)
return /^[a-z0-9_]+(\.([a-z0-9_]+))+$/.test(source);
}
+
+// Helper function to categorize traffic source type
+export function getSourceType(source: string): string {
+ const lowerSource = source.toLowerCase();
+
+ // Check domains first
+ if (searchDomains.some((domain) => lowerSource.includes(domain)))
+ return "search";
+ if (socialDomains.some((domain) => lowerSource.includes(domain)))
+ return "social";
+ if (videoDomains.some((domain) => lowerSource.includes(domain)))
+ return "video";
+ if (shoppingDomains.some((domain) => lowerSource.includes(domain)))
+ return "shopping";
+
+ // Check source names
+ if (searchSources.includes(lowerSource)) return "search";
+ if (socialSources.includes(lowerSource)) return "social";
+ if (videoSources.includes(lowerSource)) return "video";
+ if (shoppingSources.includes(lowerSource)) return "shopping";
+ if (emailSources.includes(lowerSource)) return "email";
+ if (smsSources.includes(lowerSource)) return "sms";
+
+ // Check mobile app IDs
+ if (isMobileAppId(source)) {
+ if (socialAppIds.includes(source)) return "social";
+ if (videoAppIds.includes(source)) return "video";
+ if (searchAppIds.includes(source)) return "search";
+ if (emailAppIds.includes(source)) return "email";
+ if (shoppingAppIds.includes(source)) return "shopping";
+ if (newsAppIds.includes(source)) return "news";
+ if (productivityAppIds.includes(source)) return "productivity";
+ return "mobile-app";
+ }
+
+ return "direct";
+}
+
+// Helper function to categorize medium type
+export function getMediumType(medium: string): string {
+ const lowerMedium = medium.toLowerCase();
+
+ if (socialMediums.includes(lowerMedium)) return "social";
+ if (videoMediums.includes(lowerMedium)) return "video";
+ if (displayMediums.includes(lowerMedium)) return "display";
+ if (affiliateMediums.includes(lowerMedium)) return "affiliate";
+ if (referralMediums.includes(lowerMedium)) return "referral";
+ if (emailMediums.includes(lowerMedium)) return "email";
+ if (pushMediums.includes(lowerMedium)) return "push";
+ if (audioMediums.includes(lowerMedium)) return "audio";
+ if (influencerMediums.includes(lowerMedium)) return "influencer";
+ if (cpcMediums.includes(lowerMedium)) return "cpc";
+ if (cpmMediums.includes(lowerMedium)) return "cpm";
+ if (contentMediums.includes(lowerMedium)) return "content";
+ if (eventMediums.includes(lowerMedium)) return "event";
+
+ return "organic";
+}
+
+// Helper function to check if traffic is paid
+export function isPaidTraffic(medium: string, source: string): boolean {
+ const lowerMedium = medium.toLowerCase();
+ const lowerSource = source.toLowerCase();
+
+ // Paid medium indicators
+ const paidMediums = [
+ ...cpcMediums,
+ ...cpmMediums,
+ ...displayMediums,
+ "paid",
+ "ad",
+ "ads",
+ "advertising",
+ "sponsored",
+ "promotion",
+ ];
+
+ // Paid source indicators
+ const paidSources = [
+ "google ads",
+ "googleads",
+ "bing ads",
+ "facebook ads",
+ "instagram ads",
+ "twitter ads",
+ "linkedin ads",
+ "tiktok ads",
+ "youtube ads",
+ "pinterest ads",
+ ];
+
+ return (
+ paidMediums.some((pm) => lowerMedium.includes(pm)) ||
+ paidSources.some((ps) => lowerSource.includes(ps))
+ );
+}
diff --git a/server/src/tracker/getChannel.ts b/server/src/tracker/getChannel.ts
index a436b0c23..8a64b1edd 100644
--- a/server/src/tracker/getChannel.ts
+++ b/server/src/tracker/getChannel.ts
@@ -1,28 +1,16 @@
import {
- affiliateMediums,
- audioMediums,
- displayMediums,
- emailMediums,
emailAppIds,
- emailSources,
+ getMediumType,
+ getSourceType,
isMobileAppId,
- pushMediums,
- referralMediums,
+ isPaidTraffic,
+ // New imports for expanded categories
+ newsAppIds,
+ productivityAppIds,
searchAppIds,
- searchDomains,
- searchSources,
shoppingAppIds,
- shoppingDomains,
- shoppingSources,
- smsSources,
socialAppIds,
- socialDomains,
- socialMediums,
- socialSources,
videoAppIds,
- videoDomains,
- videoMediums,
- videoSources,
} from "./const.js";
import { getUTMParams } from "./trackingUtils.js";
@@ -53,6 +41,14 @@ function getMobileAppCategory(
return { type: "Organic Shopping", isPaid: false };
}
+ if (newsAppIds.some((id) => appIdLower.includes(id))) {
+ return { type: "News", isPaid: false };
+ }
+
+ if (productivityAppIds.some((id) => appIdLower.includes(id))) {
+ return { type: "Productivity", isPaid: false };
+ }
+
return null;
}
@@ -82,6 +78,74 @@ function isSelfReferral(referringDomain: string, hostname: string): boolean {
return refDomain === currentDomain || refDomain.endsWith("." + currentDomain);
}
+/**
+ * Get detailed channel analysis including source type, medium type, and paid status
+ *
+ * @example
+ * ```typescript
+ * const details = getChannelDetails(
+ * "https://google.com/search?q=example",
+ * "utm_source=google&utm_medium=cpc&utm_campaign=brand"
+ * );
+ * // Returns:
+ * // {
+ * // channel: "Paid Search",
+ * // sourceType: "search",
+ * // mediumType: "cpc",
+ * // isPaid: true,
+ * // isMobile: false,
+ * // details: { source: "google", medium: "cpc", campaign: "brand", referringDomain: "google.com" }
+ * // }
+ * ```
+ */
+export function getChannelDetails(
+ referrer: string,
+ querystring: string,
+ hostname?: string
+): {
+ channel: string;
+ sourceType: string;
+ mediumType: string;
+ isPaid: boolean;
+ isMobile: boolean;
+ details: {
+ source: string;
+ medium: string;
+ campaign: string;
+ referringDomain: string;
+ };
+} {
+ const utmParams = getUTMParams(querystring);
+ const referringDomain = getDomainFromReferrer(referrer);
+
+ const utmSource = utmParams["utm_source"] || "";
+ const utmMedium = utmParams["utm_medium"] || "";
+ const utmCampaign = utmParams["utm_campaign"] || "";
+
+ const channel = getChannel(referrer, querystring, hostname);
+ const sourceType = getSourceType(utmSource || referringDomain);
+ const mediumType = getMediumType(utmMedium);
+ const isPaid =
+ isPaidTraffic(utmMedium, utmSource) ||
+ utmParams["gclid"] !== undefined ||
+ utmParams["gad_source"] !== undefined;
+ const isMobile = utmSource ? isMobileAppId(utmSource) : false;
+
+ return {
+ channel,
+ sourceType,
+ mediumType,
+ isPaid,
+ isMobile,
+ details: {
+ source: utmSource || referringDomain,
+ medium: utmMedium,
+ campaign: utmCampaign,
+ referringDomain,
+ },
+ };
+}
+
export function getChannel(
referrer: string,
querystring: string,
@@ -124,67 +188,18 @@ export function getChannel(
return selfReferral ? "Internal" : "Direct";
}
- // Check if traffic is paid
+ // Use utility functions for better categorization
+ const sourceType = getSourceType(utmSource || referringDomain);
+ const mediumType = getMediumType(utmMedium);
const isPaid =
- ["cpc", "cpm", "cpv", "cpa", "ppc", "retargeting"].includes(utmMedium) ||
- utmMedium.startsWith("paid") ||
- gclid !== "" ||
- gadSource !== "";
-
- // Check domain type - improved to handle subdomains better
- const isDomainSearch = searchDomains.some(
- (domain) =>
- referringDomain === domain ||
- referringDomain.endsWith("." + domain) ||
- (domain.endsWith(".") && referringDomain.startsWith(domain))
- );
- const isDomainSocial = socialDomains.some(
- (domain) =>
- referringDomain === domain ||
- referringDomain.endsWith("." + domain) ||
- (domain.endsWith(".") && referringDomain.startsWith(domain))
- );
- const isDomainVideo = videoDomains.some(
- (domain) =>
- referringDomain === domain ||
- referringDomain.endsWith("." + domain) ||
- (domain.endsWith(".") && referringDomain.startsWith(domain))
- );
- const isDomainShopping = shoppingDomains.some(
- (domain) =>
- referringDomain === domain ||
- referringDomain.endsWith("." + domain) ||
- (domain.endsWith(".") && referringDomain.startsWith(domain))
- );
-
- // Check source type
- const isSourceSearch = searchSources.includes(utmSource);
- const isSourceSocial = socialSources.includes(utmSource);
- const isSourceVideo = videoSources.includes(utmSource);
- const isSourceShopping = shoppingSources.includes(utmSource);
- const isSourceEmail = emailSources.includes(utmSource);
- const isSourceSMS = smsSources.includes(utmSource);
+ isPaidTraffic(utmMedium, utmSource) || gclid !== "" || gadSource !== "";
// Apply channel detection logic (in order of precedence)
// Cross Network
if (utmCampaign === "cross-network") return "Cross-Network";
- // Paid channels
- if (isPaid) {
- if (isSourceSearch || isDomainSearch) return "Paid Search";
- if (isSourceSocial || isDomainSocial) return "Paid Social";
- if (isSourceVideo || isDomainVideo) return "Paid Video";
- if (isSourceShopping || isDomainShopping) return "Paid Shopping";
- if (socialMediums.includes(utmMedium)) return "Paid Social";
- if (videoMediums.includes(utmMedium)) return "Paid Video";
- if (displayMediums.includes(utmMedium)) return "Display";
- if (gadSource === "1") return "Paid Search";
- if (/video/.test(utmCampaign)) return "Paid Video";
- return "Paid Unknown";
- }
-
- // Direct
+ // Direct traffic
if (
(referringDomain === "$direct" || (!referrer && !selfReferral)) &&
!utmMedium &&
@@ -193,34 +208,92 @@ export function getChannel(
return "Direct";
}
- // Organic channels - with referrer domain check prioritized for more accurate detection
- // This is the key part that handles organic search from Google clicks without UTM params
- if (isDomainSearch) return "Organic Search";
- if (isDomainSocial) return "Organic Social";
- if (isDomainVideo) return "Organic Video";
- if (isDomainShopping) return "Organic Shopping";
-
- // Then check UTM parameters if referrer didn't match
- if (isSourceSearch) return "Organic Search";
- if (isSourceSocial) return "Organic Social";
- if (isSourceVideo) return "Organic Video";
- if (isSourceShopping) return "Organic Shopping";
- if (isSourceEmail || emailMediums.includes(utmMedium)) return "Email";
- if (isSourceSMS) return "SMS";
-
- // Medium-based detection
- if (socialMediums.includes(utmMedium)) return "Organic Social";
- if (videoMediums.includes(utmMedium)) return "Organic Video";
- if (affiliateMediums.includes(utmMedium)) return "Affiliate";
- if (referralMediums.includes(utmMedium)) return "Referral";
- if (displayMediums.includes(utmMedium)) return "Display";
- if (audioMediums.includes(utmMedium)) return "Audio";
- if (pushMediums.includes(utmMedium) || utmMedium.endsWith("push"))
- return "Push";
-
- // Campaign-based detection
+ // Paid channels - use intelligent categorization
+ if (isPaid) {
+ switch (sourceType) {
+ case "search":
+ return "Paid Search";
+ case "social":
+ return "Paid Social";
+ case "video":
+ return "Paid Video";
+ case "shopping":
+ return "Paid Shopping";
+ default:
+ // Fall back to medium-based detection for paid traffic
+ switch (mediumType) {
+ case "social":
+ return "Paid Social";
+ case "video":
+ return "Paid Video";
+ case "display":
+ case "cpm":
+ return "Display";
+ case "cpc":
+ return "Paid Search";
+ case "influencer":
+ return "Paid Influencer";
+ case "audio":
+ return "Paid Audio";
+ default:
+ return "Paid Unknown";
+ }
+ }
+ }
+
+ // Organic channels - prioritize source type detection
+ switch (sourceType) {
+ case "search":
+ return "Organic Search";
+ case "social":
+ return "Organic Social";
+ case "video":
+ return "Organic Video";
+ case "shopping":
+ return "Organic Shopping";
+ case "email":
+ return "Email";
+ case "sms":
+ return "SMS";
+ case "news":
+ return "News";
+ case "productivity":
+ return "Productivity";
+ }
+
+ // Medium-based detection for organic traffic
+ switch (mediumType) {
+ case "social":
+ return "Organic Social";
+ case "video":
+ return "Organic Video";
+ case "affiliate":
+ return "Affiliate";
+ case "referral":
+ return "Referral";
+ case "display":
+ return "Display";
+ case "audio":
+ return "Audio";
+ case "push":
+ return "Push";
+ case "influencer":
+ return "Influencer";
+ case "content":
+ return "Content";
+ case "event":
+ return "Event";
+ case "email":
+ return "Email";
+ }
+
+ // Campaign-based detection as fallback
if (/video/.test(utmCampaign)) return "Organic Video";
if (/shop|shopping/.test(utmCampaign)) return "Organic Shopping";
+ if (/influencer|creator|sponsored/.test(utmCampaign)) return "Influencer";
+ if (/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
diff --git a/server/src/tracker/pageviewQueue.ts b/server/src/tracker/pageviewQueue.ts
index ec5dd54e2..2717c0d6a 100644
--- a/server/src/tracker/pageviewQueue.ts
+++ b/server/src/tracker/pageviewQueue.ts
@@ -29,7 +29,7 @@ const getParsedProperties = (properties: string | undefined) => {
class PageviewQueue {
private queue: TotalPayload[] = [];
private batchSize = 5000;
- private interval = 10000; // 10 seconds
+ private interval = 10000;
private processing = false;
constructor() {