Skip to content

📈 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!

Backend for funnel tooltip enrichment

Blue Mouse043f3c18ff51
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index 88b7fa1e9..62336fa09 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -442,6 +442,10 @@ export class AnalyticsController {
let funnel: IFunnel[] = []
let totalPageviews: number = 0
+ let stepDetails: {
+ countries: Record<number, Record<string, number>>
+ sources: Record<number, Record<string, number>>
+ } = { countries: {}, sources: {} }
const promises = [
(async () => {
@@ -454,10 +458,21 @@ export class AnalyticsController {
groupTo,
)
})(),
+ (async () => {
+ stepDetails = await this.analyticsService.getFunnelStepDetails(
+ pagesArr,
+ params,
+ )
+ })(),
]
await Promise.all(promises)
+ for (let i = 0; i < funnel.length; i++) {
+ funnel[i].topCountries = stepDetails.countries[i + 1] || {}
+ funnel[i].topSources = stepDetails.sources[i + 1] || {}
+ }
+
return { funnel, totalPageviews }
}
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 7f2b10e1b..388851bf7 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -1400,6 +1400,8 @@ export class AnalyticsService {
eventsPercStep,
dropoff,
dropoffPercStep,
+ topCountries: {} as Record<string, number>,
+ topSources: {} as Record<string, number>,
}
})
@@ -1453,6 +1455,8 @@ export class AnalyticsService {
eventsPercStep: 0,
dropoff: 0,
dropoffPercStep: 0,
+ topCountries: {},
+ topSources: {},
}))
}
@@ -1518,6 +1522,116 @@ export class AnalyticsService {
return this.formatFunnel(_reverse(this.backfillFunnel(data, pages)), pages)
}
+ async getFunnelStepDetails(
+ pages: string[],
+ params: any,
+ ): Promise<{
+ countries: Record<number, Record<string, number>>
+ sources: Record<number, Record<string, number>>
+ }> {
+ const pageParams: Record<string, string> = {}
+
+ const pagesStr = _join(
+ _map(pages, (value, index) => {
+ pageParams[`v${index}`] = value
+ return `value={v${index}:String}`
+ }),
+ ',',
+ )
+
+ const query = `
+ WITH funnel_sessions AS (
+ SELECT
+ psid,
+ windowFunnel(86400)(created, ${pagesStr}) AS level
+ FROM (
+ SELECT psid, pg AS value, created
+ FROM analytics
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ UNION ALL
+ SELECT psid, ev AS value, created
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ )
+ GROUP BY psid
+ HAVING level > 0
+ ),
+ expanded AS (
+ SELECT psid, arrayJoin(range(1, toUInt64(level + 1))) AS step
+ FROM funnel_sessions
+ ),
+ session_info AS (
+ SELECT
+ psid,
+ any(cc) AS cc,
+ any(if(domain(ref) != '', domain(ref), 'Direct / None')) AS source
+ FROM (
+ SELECT psid, cc, ref
+ FROM analytics
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ UNION ALL
+ SELECT psid, cc, ref
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ )
+ GROUP BY psid
+ )
+ SELECT step, type, val, cnt FROM (
+ SELECT e.step AS step, 'cc' AS type, si.cc AS val, count() AS cnt
+ FROM expanded e
+ INNER JOIN session_info si ON e.psid = si.psid
+ WHERE si.cc != ''
+ GROUP BY e.step, si.cc
+
+ UNION ALL
+
+ SELECT e.step AS step, 'so' AS type, si.source AS val, count() AS cnt
+ FROM expanded e
+ INNER JOIN session_info si ON e.psid = si.psid
+ GROUP BY e.step, si.source
+ )
+ ORDER BY step, type, cnt DESC
+ `
+
+ const { data } = await clickhouse
+ .query({
+ query,
+ query_params: { ...params, ...pageParams },
+ })
+ .then((resultSet) =>
+ resultSet.json<{
+ step: number
+ type: string
+ val: string
+ cnt: number
+ }>(),
+ )
+
+ const countries: Record<number, Record<string, number>> = {}
+ const sources: Record<number, Record<string, number>> = {}
+
+ for (const row of data) {
+ const { step } = row
+ if (row.type === 'cc') {
+ if (!countries[step]) countries[step] = {}
+ if (Object.keys(countries[step]).length < 3) {
+ countries[step][row.val] = row.cnt
+ }
+ } else if (row.type === 'so') {
+ if (!sources[step]) sources[step] = {}
+ if (Object.keys(sources[step]).length < 3) {
+ sources[step][row.val] = row.cnt
+ }
+ }
+ }
+
+ return { countries, sources }
+ }
+
async getTotalPageviews(
pid: string,
groupFrom: string,
diff --git a/backend/apps/cloud/src/analytics/interfaces/index.ts b/backend/apps/cloud/src/analytics/interfaces/index.ts
index 432b148eb..f0e6203ad 100644
--- a/backend/apps/cloud/src/analytics/interfaces/index.ts
+++ b/backend/apps/cloud/src/analytics/interfaces/index.ts
@@ -125,6 +125,8 @@ export interface IFunnel {
eventsPercStep: number
dropoff: number
dropoffPercStep: number
+ topCountries: Record<string, number>
+ topSources: Record<string, number>
}
export interface IGetFunnel {
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index 11c8bdd21..8de4bab52 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -430,6 +430,10 @@ export class AnalyticsController {
let funnel: IFunnel[] = []
let totalPageviews: number = 0
+ let stepDetails: {
+ countries: Record<number, Record<string, number>>
+ sources: Record<number, Record<string, number>>
+ } = { countries: {}, sources: {} }
const promises = [
(async () => {
@@ -442,10 +446,21 @@ export class AnalyticsController {
groupTo,
)
})(),
+ (async () => {
+ stepDetails = await this.analyticsService.getFunnelStepDetails(
+ pagesArr,
+ params,
+ )
+ })(),
]
await Promise.all(promises)
+ for (let i = 0; i < funnel.length; i++) {
+ funnel[i].topCountries = stepDetails.countries[i + 1] || {}
+ funnel[i].topSources = stepDetails.sources[i + 1] || {}
+ }
+
return { funnel, totalPageviews }
}
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 6fe1074c8..aa4ee7882 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -1361,6 +1361,8 @@ export class AnalyticsService {
eventsPercStep,
dropoff,
dropoffPercStep,
+ topCountries: {} as Record<string, number>,
+ topSources: {} as Record<string, number>,
}
})
@@ -1414,6 +1416,8 @@ export class AnalyticsService {
eventsPercStep: 0,
dropoff: 0,
dropoffPercStep: 0,
+ topCountries: {},
+ topSources: {},
}))
}
@@ -1479,6 +1483,116 @@ export class AnalyticsService {
return this.formatFunnel(_reverse(this.backfillFunnel(data, pages)), pages)
}
+ async getFunnelStepDetails(
+ pages: string[],
+ params: any,
+ ): Promise<{
+ countries: Record<number, Record<string, number>>
+ sources: Record<number, Record<string, number>>
+ }> {
+ const pageParams: Record<string, string> = {}
+
+ const pagesStr = _join(
+ _map(pages, (value, index) => {
+ pageParams[`v${index}`] = value
+ return `value={v${index}:String}`
+ }),
+ ',',
+ )
+
+ const query = `
+ WITH funnel_sessions AS (
+ SELECT
+ psid,
+ windowFunnel(86400)(created, ${pagesStr}) AS level
+ FROM (
+ SELECT psid, pg AS value, created
+ FROM analytics
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ UNION ALL
+ SELECT psid, ev AS value, created
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ )
+ GROUP BY psid
+ HAVING level > 0
+ ),
+ expanded AS (
+ SELECT psid, arrayJoin(range(1, toUInt64(level + 1))) AS step
+ FROM funnel_sessions
+ ),
+ session_info AS (
+ SELECT
+ psid,
+ any(cc) AS cc,
+ any(if(domain(ref) != '', domain(ref), 'Direct / None')) AS source
+ FROM (
+ SELECT psid, cc, ref
+ FROM analytics
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ UNION ALL
+ SELECT psid, cc, ref
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)} AND psid != 0
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ )
+ GROUP BY psid
+ )
+ SELECT step, type, val, cnt FROM (
+ SELECT e.step AS step, 'cc' AS type, si.cc AS val, count() AS cnt
+ FROM expanded e
+ INNER JOIN session_info si ON e.psid = si.psid
+ WHERE si.cc != ''
+ GROUP BY e.step, si.cc
+
+ UNION ALL
+
+ SELECT e.step AS step, 'so' AS type, si.source AS val, count() AS cnt
+ FROM expanded e
+ INNER JOIN session_info si ON e.psid = si.psid
+ GROUP BY e.step, si.source
+ )
+ ORDER BY step, type, cnt DESC
+ `
+
+ const { data } = await clickhouse
+ .query({
+ query,
+ query_params: { ...params, ...pageParams },
+ })
+ .then((resultSet) =>
+ resultSet.json<{
+ step: number
+ type: string
+ val: string
+ cnt: number
+ }>(),
+ )
+
+ const countries: Record<number, Record<string, number>> = {}
+ const sources: Record<number, Record<string, number>> = {}
+
+ for (const row of data) {
+ const { step } = row
+ if (row.type === 'cc') {
+ if (!countries[step]) countries[step] = {}
+ if (Object.keys(countries[step]).length < 3) {
+ countries[step][row.val] = row.cnt
+ }
+ } else if (row.type === 'so') {
+ if (!sources[step]) sources[step] = {}
+ if (Object.keys(sources[step]).length < 3) {
+ sources[step][row.val] = row.cnt
+ }
+ }
+ }
+
+ return { countries, sources }
+ }
+
async getTotalPageviews(
pid: string,
groupFrom: string,
diff --git a/backend/apps/community/src/analytics/interfaces/index.ts b/backend/apps/community/src/analytics/interfaces/index.ts
index fedd7e54f..84da70732 100644
--- a/backend/apps/community/src/analytics/interfaces/index.ts
+++ b/backend/apps/community/src/analytics/interfaces/index.ts
@@ -118,6 +118,8 @@ export interface IFunnel {
eventsPercStep: number
dropoff: number
dropoffPercStep: number
+ topCountries: Record<string, number>
+ topSources: Record<string, number>
}
export interface IGetFunnel {