🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor usageService, r2StorageService, trackingUtils, and getChannel for improved readability and consistency
Bill Yang23cad30eaa6c
diff --git a/server/src/services/replay/trackingUtils.ts b/server/src/services/replay/trackingUtils.tsindex 909384fad..5ab6cfd0f 100644--- a/server/src/services/replay/trackingUtils.ts+++ b/server/src/services/replay/trackingUtils.ts@@ -80,39 +80,3 @@ export async function parseTrackingData(referrer: referrer || "",};}--/**- * Extract browser info from user agent- */-export function getBrowserInfo(userAgent: string): {- name: string;- version: string;-} {- const ua = UAParser(userAgent);- return {- name: ua.browser.name || "Unknown",- version: ua.browser.major || "",- };-}--/**- * Extract OS info from user agent- */-export function getOSInfo(userAgent: string): {- name: string;- version: string;-} {- const ua = UAParser(userAgent);- return {- name: ua.os.name || "Unknown",- version: ua.os.version || "",- };-}--/**- * Get device type from user agent and screen dimensions- */-export function getDeviceTypeFromUA(userAgent: string, screenWidth: number, screenHeight: number): string {- const ua = UAParser(userAgent);- return getDeviceType(screenWidth, screenHeight, ua);-}diff --git a/server/src/services/storage/r2StorageService.ts b/server/src/services/storage/r2StorageService.tsindex 4bebebdf4..ae236a696 100644--- a/server/src/services/storage/r2StorageService.ts+++ b/server/src/services/storage/r2StorageService.ts@@ -4,7 +4,7 @@ import { gunzipSync } from "zlib";import { compress as zstdCompress, decompress as zstdDecompress } from "@mongodb-js/zstd";import { IS_CLOUD } from "../../lib/const.js";-export class R2StorageService {+class R2StorageService {private client: S3Client | null = null;private bucketName: string = "";private enabled: boolean = false;diff --git a/server/src/services/tracker/getChannel.ts b/server/src/services/tracker/getChannel.tsindex 547a2f65a..4897dccde 100644--- a/server/src/services/tracker/getChannel.ts+++ b/server/src/services/tracker/getChannel.ts@@ -15,9 +15,7 @@ import {import { getUTMParams } from "./utils.js";// Categorize mobile app by its bundle ID/package name-function getMobileAppCategory(- appId: string-): { type: string; isPaid: boolean } | null {+function getMobileAppCategory(appId: string): { type: string; isPaid: boolean } | null {const appIdLower = appId.toLowerCase();// Check against our defined app ID lists@@ -98,10 +96,10 @@ function isSelfReferral(referringDomain: string, hostname: string): boolean {* // }* ```*/-export function getChannelDetails(+function getChannelDetails(referrer: string,querystring: string,- hostname?: string+ hostname?: string,): {channel: string;sourceType: string;@@ -126,9 +124,7 @@ export function getChannelDetails(const sourceType = getSourceType(utmSource || referringDomain);const mediumType = getMediumType(utmMedium);const isPaid =- isPaidTraffic(utmMedium, utmSource) ||- utmParams["gclid"] !== undefined ||- utmParams["gad_source"] !== undefined;+ isPaidTraffic(utmMedium, utmSource) || utmParams["gclid"] !== undefined || utmParams["gad_source"] !== undefined;const isMobile = utmSource ? isMobileAppId(utmSource) : false;return {@@ -146,18 +142,12 @@ export function getChannelDetails(};}-export function getChannel(- referrer: string,- querystring: string,- hostname?: string-): string {+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;+ const selfReferral = hostname ? isSelfReferral(referringDomain, hostname) : false;// UTM parametersconst utmSource = utmParams["utm_source"] || "";@@ -170,29 +160,19 @@ export function getChannel(if (utmSource && isMobileAppId(utmSource)) {const appCategory = getMobileAppCategory(utmSource);if (appCategory) {- return appCategory.isPaid- ? "Paid " + appCategory.type.split(" ")[1]- : appCategory.type;+ return appCategory.isPaid ? "Paid " + appCategory.type.split(" ")[1] : appCategory.type;}}// If it's a self-referral and has no UTM parameters, treat it as internal traffic- if (- !referrer &&- !utmSource &&- !utmMedium &&- !utmCampaign &&- !gclid &&- !gadSource- ) {+ if (!referrer && !utmSource && !utmMedium && !utmCampaign && !gclid && !gadSource) {return selfReferral ? "Internal" : "Direct";}// Use utility functions for better categorizationconst sourceType = getSourceType(utmSource || referringDomain);const mediumType = getMediumType(utmMedium);- const isPaid =- isPaidTraffic(utmMedium, utmSource) || gclid !== "" || gadSource !== "";+ const isPaid = isPaidTraffic(utmMedium, utmSource) || gclid !== "" || gadSource !== "";// Apply channel detection logic (in order of precedence)@@ -292,13 +272,11 @@ export function getChannel(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 (/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 (referringDomain && referringDomain !== "$direct" && !selfReferral) return "Referral";// Default fallbackreturn "Unknown";diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.tsindex bf42dd3b3..87ffae494 100644--- a/server/src/services/usageService.ts+++ b/server/src/services/usageService.ts@@ -6,15 +6,10 @@ import { processResults } from "../api/analytics/utils.js";import { clickhouse } from "../db/clickhouse/clickhouse.js";import { db } from "../db/postgres/postgres.js";import { organization, sites } from "../db/postgres/schema.js";-import {- getStripePrices,- StripePlan,- DEFAULT_EVENT_LIMIT,- IS_CLOUD,-} from "../lib/const.js";+import { getStripePrices, StripePlan, DEFAULT_EVENT_LIMIT, IS_CLOUD } from "../lib/const.js";import { stripe } from "../lib/stripe.js";-export class UsageService {+class UsageService {private sitesOverLimit = new Set<number>();private usageCheckTask: cron.ScheduledTask | null = null;@@ -32,19 +27,14 @@ export class UsageService {try {await this.updateOrganizationsMonthlyUsage();} catch (error) {- console.error(- "[UsageCheckerService] Error during usage check:",- error- );+ console.error("[UsageCheckerService] Error during usage check:", error);}});// Run immediately on startupthis.updateOrganizationsMonthlyUsage();- console.log(- "[UsageCheckerService] Monthly usage check cron initialized (runs every 30 minutes)"- );+ console.log("[UsageCheckerService] Monthly usage check cron initialized (runs every 30 minutes)");}}@@ -65,9 +55,7 @@ export class UsageService {/*** Gets all site IDs for an organization*/- private async getSiteIdsForOrganization(- organizationId: string- ): Promise<number[]> {+ private async getSiteIdsForOrganization(organizationId: string): Promise<number[]> {try {const siteRecords = await db.select({ siteId: sites.siteId })@@ -76,10 +64,7 @@ export class UsageService {return siteRecords.map((record) => record.siteId);} catch (error) {- console.error(- `Error getting sites for organization ${organizationId}:`,- error- );+ console.error(`Error getting sites for organization ${organizationId}:`, error);return [];}}@@ -121,17 +106,14 @@ export class UsageService {const priceId = subscriptionItem.price.id;if (!priceId) {- console.error(- `Subscription item price ID not found for organization ${orgData.id}, sub ${subscription.id}`- );+ console.error(`Subscription item price ID not found for organization ${orgData.id}, sub ${subscription.id}`);return [DEFAULT_EVENT_LIMIT, this.getStartOfMonth()];}// Find corresponding plan details from constantsconst planDetails = getStripePrices().find((plan: StripePlan) =>- plan.priceId === priceId ||- (plan.annualDiscountPriceId && plan.annualDiscountPriceId === priceId)+ plan.priceId === priceId || (plan.annualDiscountPriceId && plan.annualDiscountPriceId === priceId),);// Get the event limit from the plan@@ -145,9 +127,7 @@ export class UsageService {if (subscriptionItem.current_period_start) {// Convert subscription start timestamp to DateTime- const subscriptionStartDate = DateTime.fromSeconds(- subscriptionItem.current_period_start- );+ const subscriptionStartDate = DateTime.fromSeconds(subscriptionItem.current_period_start);const currentMonth = DateTime.now().startOf("month");// If the subscription started within the current month, use that as the start date@@ -155,27 +135,22 @@ export class UsageService {if (subscriptionStartDate >= currentMonth) {periodStart = subscriptionStartDate.toISODate() as string;console.log(- `[Monthly Usage Checker] Organization ${orgData.name} subscribed during current month on ${periodStart}. Using subscription start date for counting.`+ `[Monthly Usage Checker] Organization ${orgData.name} subscribed during current month on ${periodStart}. Using subscription start date for counting.`,);} else {console.log(- `[Monthly Usage Checker] Organization ${orgData.name} subscription started before current month. Using month start for counting.`+ `[Monthly Usage Checker] Organization ${orgData.name} subscription started before current month. Using month start for counting.`,);}}// Include subscription info for logging purposesconst interval = subscriptionItem.price.recurring?.interval || "unknown";- console.log(- `[Monthly Usage Checker] Organization ${orgData.name} has a ${interval} subscription.`- );+ console.log(`[Monthly Usage Checker] Organization ${orgData.name} has a ${interval} subscription.`);return [eventLimit, periodStart];} catch (error: any) {- console.error(- `Error fetching Stripe subscription info for organization ${orgData.name}:`,- error- );+ console.error(`Error fetching Stripe subscription info for organization ${orgData.name}:`, error);// Fallback to default limit and current month start on Stripe API errorreturn [DEFAULT_EVENT_LIMIT, this.getStartOfMonth()];}@@ -184,10 +159,7 @@ export class UsageService {/*** Gets monthly pageview count from ClickHouse for the given site IDs*/- private async getMonthlyPageviews(- siteIds: number[],- startDate: string | null- ): Promise<number> {+ private async getMonthlyPageviews(siteIds: number[], startDate: string | null): Promise<number> {if (!siteIds.length) {return 0;}@@ -208,10 +180,7 @@ export class UsageService {const rows = await processResults<{ count: string }>(result);return parseInt(rows[0].count, 10);} catch (error) {- console.error(- `Error querying ClickHouse for pageviews for sites ${siteIds}:`,- error- );+ console.error(`Error querying ClickHouse for pageviews for sites ${siteIds}:`, error);return 0;}}@@ -220,9 +189,7 @@ export class UsageService {* Updates monthly event usage for all organizations*/public async updateOrganizationsMonthlyUsage(): Promise<void> {- console.log(- "[Monthly Usage Checker] Starting check of monthly event usage for organizations..."- );+ console.log("[Monthly Usage Checker] Starting check of monthly event usage for organizations...");try {// Get all organizations (both with and without Stripe customer IDs)@@ -246,14 +213,10 @@ export class UsageService {}// Get organization's subscription information (limit and period start)- const [eventLimit, periodStart] =- await this.getOrganizationSubscriptionInfo(orgData);+ const [eventLimit, periodStart] = await this.getOrganizationSubscriptionInfo(orgData);// Get monthly pageview count from ClickHouse using the billing period start date- const pageviewCount = await this.getMonthlyPageviews(- siteIds,- periodStart- );+ const pageviewCount = await this.getMonthlyPageviews(siteIds, periodStart);// Check if over limit and update global setconst isOverLimit = pageviewCount > eventLimit;@@ -273,7 +236,7 @@ export class UsageService {this.sitesOverLimit.add(siteId);}console.log(- `[Monthly Usage Checker] Organization ${orgData.name} is over limit. Added ${siteIds.length} sites to blocked list.`+ `[Monthly Usage Checker] Organization ${orgData.name} is over limit. Added ${siteIds.length} sites to blocked list.`,);} else {for (const siteId of siteIds) {@@ -282,31 +245,23 @@ export class UsageService {}// Format additional date info for logging if available- const periodInfo = periodStart- ? `period started ${periodStart}`- : "this month";+ const periodInfo = periodStart ? `period started ${periodStart}` : "this month";console.log(`[Monthly Usage Checker] Updated organization ${orgData.name- }: ${pageviewCount.toLocaleString()} events, limit ${eventLimit.toLocaleString()}, ${periodInfo}`+ }: ${pageviewCount.toLocaleString()} events, limit ${eventLimit.toLocaleString()}, ${periodInfo}`,);} catch (error) {- console.error(- `[Monthly Usage Checker] Error processing organization ${orgData.id}:`,- error- );+ console.error(`[Monthly Usage Checker] Error processing organization ${orgData.id}:`, error);}}console.log(- `[Monthly Usage Checker] Completed monthly event usage check. ${this.sitesOverLimit.size} sites are over their limit.`+ `[Monthly Usage Checker] Completed monthly event usage check. ${this.sitesOverLimit.size} sites are over their limit.`,);} catch (error) {- console.error(- "[Monthly Usage Checker] Error updating monthly usage:",- error- );+ console.error("[Monthly Usage Checker] Error updating monthly usage:", error);}}