📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
security improvements
Blue Mouseee90662b972a
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex afc4a8029..67f3e151e 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -98,7 +98,6 @@ import { GetHeartbeatStatsDto } from './dto/get-heartbeat-stats'import { GetKeywordsDto } from './dto/get-keywords.dto'import { GSCService } from '../project/gsc.service'import { GetProfileIdDto, GetSessionIdDto } from './dto/get-id.dto'-import { RevenueService } from '../revenue/revenue.service'dayjs.extend(utc)dayjs.extend(dayjsTimezone)@@ -206,7 +205,6 @@ export class AnalyticsController {private readonly analyticsService: AnalyticsService,private readonly logger: AppLoggerService,private readonly gscService: GSCService,- private readonly revenueService: RevenueService,) {}@ApiBearerAuth()diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex a04bd00fd..cfa736033 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -4341,25 +4341,39 @@ export class AnalyticsService {UNION ALLSELECT- revenue.type AS type,- COALESCE(toString(revenue.product_name), toString(revenue.product_id), if(revenue.type = 'refund', 'Refund', 'Sale')) AS value,- toTimeZone(revenue.created, {timezone:String}) AS created,+ type,+ COALESCE(toString(product_name), toString(product_id), if(type = 'refund', 'Refund', 'Sale')) AS value,+ toTimeZone(created, {timezone:String}) AS created,pid,- toString(revenue.session_id) AS psid,+ toString(session_id) AS psid,[- tuple('amount', toString(revenue.amount)),- tuple('currency', toString(revenue.currency)),- tuple('transaction_id', toString(revenue.transaction_id)),- tuple('status', toString(revenue.status)),- tuple('provider', toString(revenue.provider))+ tuple('amount', toString(amount)),+ tuple('currency', toString(currency)),+ tuple('transaction_id', toString(transaction_id)),+ tuple('status', toString(status)),+ tuple('provider', toString(provider))] AS metadata- FROM revenue- WHERE- pid = {pid:FixedString(12)}- AND revenue.session_id IS NOT NULL- AND toString(revenue.session_id) = {psid:String}- AND revenue.type IN ('sale', 'refund')- GROUP BY type, value, created, pid, psid, revenue.product_name, revenue.product_id, revenue.amount, revenue.currency, revenue.transaction_id, revenue.status, revenue.provider+ FROM (+ SELECT+ argMax(type, synced_at) AS type,+ argMax(product_name, synced_at) AS product_name,+ argMax(product_id, synced_at) AS product_id,+ argMax(created, synced_at) AS created,+ pid,+ session_id,+ argMax(amount, synced_at) AS amount,+ argMax(currency, synced_at) AS currency,+ transaction_id,+ argMax(status, synced_at) AS status,+ argMax(provider, synced_at) AS provider+ FROM revenue+ WHERE+ pid = {pid:FixedString(12)}+ AND session_id IS NOT NULL+ AND toString(session_id) = {psid:String}+ AND type IN ('sale', 'refund')+ GROUP BY pid, session_id, transaction_id+ ))SELECT@@ -4588,14 +4602,22 @@ export class AnalyticsService {),revenue_totals AS (SELECT- CAST(session_id, 'String') AS psidCasted,+ psidCasted,pid,sum(CASE WHEN type = 'sale' THEN amount ELSE 0 END) - sum(CASE WHEN type = 'refund' THEN abs(amount) ELSE 0 END) as revenue,sum(CASE WHEN type = 'refund' THEN abs(amount) ELSE 0 END) as refunds- FROM revenue- WHERE pid = {pid:FixedString(12)} AND session_id IS NOT NULL- AND created BETWEEN {groupFrom:String} AND {groupTo:String}- AND type IN ('sale', 'refund')+ FROM (+ SELECT+ CAST(session_id, 'String') AS psidCasted,+ pid,+ argMax(type, synced_at) AS type,+ argMax(amount, synced_at) AS amount+ FROM revenue+ WHERE pid = {pid:FixedString(12)} AND session_id IS NOT NULL+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ AND type IN ('sale', 'refund')+ GROUP BY psidCasted, pid, transaction_id+ )GROUP BY psidCasted, pid),session_duration_agg AS (@@ -4822,11 +4844,17 @@ export class AnalyticsService {SELECTsum(amount) AS totalRevenue,any(currency) AS revenueCurrency- FROM revenue- WHERE pid = {pid:FixedString(12)}- AND profile_id = {profileId:String}- AND type = 'sale'- AND status = 'completed'+ FROM (+ SELECT+ argMax(amount, synced_at) AS amount,+ argMax(currency, synced_at) AS currency+ FROM revenue+ WHERE pid = {pid:FixedString(12)}+ AND profile_id = {profileId:String}+ AND type = 'sale'+ AND status = 'completed'+ GROUP BY transaction_id+ )`const params = { pid, profileId }diff --git a/backend/apps/cloud/src/revenue/adapters/paddle.adapter.ts b/backend/apps/cloud/src/revenue/adapters/paddle.adapter.tsindex caa75fab6..8c514eeea 100644--- a/backend/apps/cloud/src/revenue/adapters/paddle.adapter.ts+++ b/backend/apps/cloud/src/revenue/adapters/paddle.adapter.ts@@ -77,8 +77,7 @@ interface PaddleListResponse<T> {@Injectable()export class PaddleAdapter {- // TODO: Change to api.paddle.com when merging to main- private readonly baseUrl = 'https://sandbox-api.paddle.com'+ private readonly baseUrl = 'https://api.paddle.com'constructor(private readonly logger: AppLoggerService,diff --git a/backend/apps/cloud/src/revenue/revenue.controller.ts b/backend/apps/cloud/src/revenue/revenue.controller.tsindex 56af60a1f..5bc169562 100644--- a/backend/apps/cloud/src/revenue/revenue.controller.ts+++ b/backend/apps/cloud/src/revenue/revenue.controller.ts@@ -189,6 +189,36 @@ export class RevenueController {await this.revenueService.updateCurrency(pid, dto.currency)+ // Trigger immediate re-sync+ try {+ if (project.paddleApiKeyEnc) {+ const apiKey = this.revenueService.getPaddleApiKey(project)+ if (apiKey) {+ await this.paddleAdapter.syncTransactions(+ pid,+ apiKey,+ dto.currency,+ undefined, // Full re-sync+ )+ await this.revenueService.updateLastSyncAt(pid)+ }+ } else if (project.stripeApiKeyEnc) {+ const apiKey = this.revenueService.getStripeApiKey(project)+ if (apiKey) {+ await this.stripeAdapter.syncTransactions(+ pid,+ apiKey,+ dto.currency,+ undefined, // Full re-sync+ )+ await this.revenueService.updateLastSyncAt(pid)+ }+ }+ } catch (error) {+ this.logger.error({ error, pid }, 'Failed to trigger re-sync after currency update')+ // Don't throw here, as currency was updated successfully+ }+return { success: true }}diff --git a/backend/apps/cloud/src/revenue/revenue.service.ts b/backend/apps/cloud/src/revenue/revenue.service.tsindex 201d36032..1c52e963b 100644--- a/backend/apps/cloud/src/revenue/revenue.service.ts+++ b/backend/apps/cloud/src/revenue/revenue.service.ts@@ -160,6 +160,7 @@ export class RevenueService {async updateCurrency(projectId: string, currency: string): Promise<void> {await this.projectRepository.update(projectId, {revenueCurrency: currency,+ revenueLastSyncAt: null, // Reset sync so all transactions are re-converted})}@@ -563,11 +564,91 @@ export class RevenueService {resultSet.json<{ name: string; revenue: number; count: number }>(),)- // For source and country, we'd need to join with analytics table- // For now, return empty arrays - these will be populated when we have attribution+ // By source+ const bySourceQuery = `+ SELECT+ COALESCE(a.so, 'Direct') as name,+ sum(r.amount) as revenue,+ count(r.amount) as count+ FROM (+ SELECT+ session_id,+ argMax(amount, synced_at) as amount+ FROM revenue+ WHERE pid = {pid:FixedString(12)}+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ AND type = 'sale'+ AND session_id IS NOT NULL+ GROUP BY session_id, transaction_id+ ) AS r+ JOIN (+ SELECT+ psid,+ argMin(so, created) as so+ FROM analytics+ WHERE pid = {pid:FixedString(12)}+ AND psid IS NOT NULL+ GROUP BY psid+ ) AS a ON r.session_id = toString(a.psid)+ GROUP BY name+ ORDER BY revenue DESC+ LIMIT 10+ `++ const { data: bySourceData } = await clickhouse+ .query({ query: bySourceQuery, query_params: params })+ .then(resultSet =>+ resultSet.json<{ name: string; revenue: number; count: number }>(),+ )++ // By country+ const byCountryQuery = `+ SELECT+ COALESCE(a.cc, 'Unknown') as name,+ sum(r.amount) as revenue,+ count(r.amount) as count+ FROM (+ SELECT+ session_id,+ argMax(amount, synced_at) as amount+ FROM revenue+ WHERE pid = {pid:FixedString(12)}+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ AND type = 'sale'+ AND session_id IS NOT NULL+ GROUP BY session_id, transaction_id+ ) AS r+ JOIN (+ SELECT+ psid,+ argMin(cc, created) as cc+ FROM analytics+ WHERE pid = {pid:FixedString(12)}+ AND psid IS NOT NULL+ GROUP BY psid+ ) AS a ON r.session_id = toString(a.psid)+ GROUP BY name+ ORDER BY revenue DESC+ LIMIT 10+ `++ const { data: byCountryData } = await clickhouse+ .query({ query: byCountryQuery, query_params: params })+ .then(resultSet =>+ resultSet.json<{ name: string; revenue: number; count: number }>(),+ )+return {- bySource: [],- byCountry: [],+ bySource: bySourceData.map(row => ({+ name: row.name,+ revenue: _round(row.revenue, 2),+ count: row.count,+ })),+ byCountry: byCountryData.map(row => ({+ name: row.name,+ revenue: _round(row.revenue, 2),+ count: row.count,+ })),byProduct: byProductData.map(row => ({name: row.name,revenue: _round(row.revenue, 2),diff --git a/backend/apps/cloud/src/task-manager/task-manager.service.ts b/backend/apps/cloud/src/task-manager/task-manager.service.tsindex 6a2ff7c51..53494f47e 100644--- a/backend/apps/cloud/src/task-manager/task-manager.service.ts+++ b/backend/apps/cloud/src/task-manager/task-manager.service.ts@@ -61,6 +61,7 @@ import { DiscordService } from '../integrations/discord/discord.service'import { SlackService } from '../integrations/slack/slack.service'import { RevenueService } from '../revenue/revenue.service'import { PaddleAdapter } from '../revenue/adapters/paddle.adapter'+import { StripeAdapter } from '../revenue/adapters/stripe.adapter'dayjs.extend(utc)@@ -327,6 +328,7 @@ export class TaskManagerService {private readonly goalService: GoalService,private readonly revenueService: RevenueService,private readonly paddleAdapter: PaddleAdapter,+ private readonly stripeAdapter: StripeAdapter,) {}/**@@ -1083,15 +1085,24 @@ export class TaskManagerService {// Sync revenue data from payment providers every 30 minutes@Cron(CronExpression.EVERY_30_MINUTES)async syncRevenueData() {- // Find all projects with Paddle connected+ // Find all projects with Paddle or Stripe connectedconst projects = await this.projectService.find({- where: {- paddleApiKeyEnc: Not(IsNull()),- admin: {- planCode: Not(PlanCode.none),- dashboardBlockReason: IsNull(),+ where: [+ {+ paddleApiKeyEnc: Not(IsNull()),+ admin: {+ planCode: Not(PlanCode.none),+ dashboardBlockReason: IsNull(),+ },},- },+ {+ stripeApiKeyEnc: Not(IsNull()),+ admin: {+ planCode: Not(PlanCode.none),+ dashboardBlockReason: IsNull(),+ },+ },+ ],relations: ['admin'],})@@ -1101,20 +1112,37 @@ export class TaskManagerService {const promises = _map(projects, async project => {try {- const apiKey = this.revenueService.getPaddleApiKey(project)- if (!apiKey) {- this.logger.warn(- `[CRON WORKER](syncRevenueData) Failed to decrypt Paddle API key for project ${project.id}`,+ if (project.paddleApiKeyEnc) {+ const apiKey = this.revenueService.getPaddleApiKey(project)+ if (!apiKey) {+ this.logger.warn(+ `[CRON WORKER](syncRevenueData) Failed to decrypt Paddle API key for project ${project.id}`,+ )+ return+ }++ await this.paddleAdapter.syncTransactions(+ project.id,+ apiKey,+ project.revenueCurrency || 'USD',+ project.revenueLastSyncAt || undefined,)- return- }+ } else if (project.stripeApiKeyEnc) {+ const apiKey = this.revenueService.getStripeApiKey(project)+ if (!apiKey) {+ this.logger.warn(+ `[CRON WORKER](syncRevenueData) Failed to decrypt Stripe API key for project ${project.id}`,+ )+ return+ }- await this.paddleAdapter.syncTransactions(- project.id,- apiKey,- project.revenueCurrency || 'USD',- project.revenueLastSyncAt || undefined,- )+ await this.stripeAdapter.syncTransactions(+ project.id,+ apiKey,+ project.revenueCurrency || 'USD',+ project.revenueLastSyncAt || undefined,+ )+ }await this.revenueService.updateLastSyncAt(project.id)} catch (error) {@@ -1893,22 +1921,29 @@ export class TaskManagerService {// a bit more than 2 months agoconst nineWeeksAgo = dayjs.utc().subtract(9, 'w').format('YYYY-MM-DD')- const promises = _map(users, async user => {+ await mapLimit(users, REPORTS_USERS_CONCURRENCY, async user => {const { id, projects } = userif (_isEmpty(projects) || _isNull(projects)) {return}- const pidsStringified = _map(projects, p => `'${p.id}'`).join(',')+ const pids = _map(projects, p => p.id)+ const queryParams = {+ pids,+ nineWeeksAgo,+ now,+ }+// No need to check for performance activity because it's not tracked without tracking analytics- const queryAnalytics = `SELECT count() FROM analytics WHERE pid IN (${pidsStringified}) AND created BETWEEN '${nineWeeksAgo}' AND '${now}'`- const queryCaptcha = `SELECT count() FROM captcha WHERE pid IN (${pidsStringified}) AND created BETWEEN '${nineWeeksAgo}' AND '${now}'`- const queryCustomEvents = `SELECT count() FROM customEV WHERE pid IN (${pidsStringified}) AND created BETWEEN '${nineWeeksAgo}' AND '${now}'`+ const queryAnalytics = `SELECT count() FROM analytics WHERE pid IN ({pids:Array(FixedString(12))}) AND created BETWEEN {nineWeeksAgo:String} AND {now:String}`+ const queryCaptcha = `SELECT count() FROM captcha WHERE pid IN ({pids:Array(FixedString(12))}) AND created BETWEEN {nineWeeksAgo:String} AND {now:String}`+ const queryCustomEvents = `SELECT count() FROM customEV WHERE pid IN ({pids:Array(FixedString(12))}) AND created BETWEEN {nineWeeksAgo:String} AND {now:String}`const { data: analyticsResult } = await clickhouse.query({query: queryAnalytics,+ query_params: queryParams,}).then(resultSet => resultSet.json<{ 'count()': number }>())@@ -1919,6 +1954,7 @@ export class TaskManagerService {const { data: captchaResult } = await clickhouse.query({query: queryCaptcha,+ query_params: queryParams,}).then(resultSet => resultSet.json<{ 'count()': number }>())@@ -1929,6 +1965,7 @@ export class TaskManagerService {const { data: customEventsResult } = await clickhouse.query({query: queryCustomEvents,+ query_params: queryParams,}).then(resultSet => resultSet.json<{ 'count()': number }>())