📈 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
some improvements
Blue Mouseaa5d7a876a30
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.tsindex ba5048eb6..4c5660481 100644--- a/apps/production/src/analytics/analytics.controller.ts+++ b/apps/production/src/analytics/analytics.controller.ts@@ -654,6 +654,53 @@ export class AnalyticsController {}}+ @Get('funnel')+ @Auth([], true, true)+ async getFunnel(+ @Query() data: GetUserFlowDTO,+ @CurrentUserId() uid: string,+ @Headers() headers: { 'x-password'?: string },+ ): Promise<IUserFlow | { appliedFilters: any[] }> {+ const { pid, period, from, to, timezone = DEFAULT_TIMEZONE, filters } = data+ this.analyticsService.validatePID(pid)++ if (!_isEmpty(period)) {+ this.analyticsService.validatePeriod(period)+ }++ await this.analyticsService.checkProjectAccess(+ pid,+ uid,+ headers['x-password'],+ )++ const safeTimezone = this.analyticsService.getSafeTimezone(timezone)+ const { groupFrom, groupTo } = this.analyticsService.getGroupFromTo(+ from,+ to,+ null,+ period,+ safeTimezone,+ )++ const [filtersQuery, filtersParams, appliedFilters] =+ this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)++ const params = {+ pid,+ groupFrom,+ groupTo,+ ...filtersParams,+ }++ const flow = await this.analyticsService.getUserFlow(params, filtersQuery)++ return {+ ...flow,+ appliedFilters,+ }+ }+@Get('birdseye')@Auth([], true, true)// returns overall short statistics per projectdiff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.tsindex 155072f6d..8c7036acd 100644--- a/apps/production/src/analytics/analytics.service.ts+++ b/apps/production/src/analytics/analytics.service.ts@@ -68,6 +68,7 @@ import {IUserFlowNode,IUserFlowLink,IUserFlow,+ IFunnel,IBuildUserFlow,IExtractChartData,IGenerateXAxis,@@ -586,12 +587,63 @@ export class AnalyticsService {}})+ await this.getFunnel(params, ['/settings', '/dashboard', '/'])+return {ascending: this.buildUserFlow(ascendingLinks),descending: this.buildUserFlow(descendingLinks),}}+ async getFunnel(params: any, pages: string[]): Promise<any> {+ // Promise<IFunnel> {++ // My 'analytics' table has the following columns: pg and prev+ // Pg is the current page and prev is the previous page+ // I need to calculate the funnel (amount of users that went from page A to page B, to page ... (i.e. A -> B -> C -> D...))+ // I use Clickhouse as my database.+ // - params is an object that contains properties like pid, groupFrom, groupTo, etc.+ // - pages is an array of pages that I need to calculate the funnel for++ // const query = `+ // SELECT+ // count() AS count+ // FROM analytics+ // WHERE+ // pid = {pid:FixedString(12)}+ // AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ // AND pg = {page:String}+ // ${prevPage ? `AND prev = {prevPage:String}` : ''}+ // `+ // The following query seems to calculate funnel the following way: A -> B, B -> C, C -> D, etc.+ // that is incorrect, because it's possible that some users went from X -> B, Y -> B and were included in the funnel+ // It should be calculated the following way: A -> B -> C -> D, etc.+ // Let's fix it by using INNER JOINS recursively++ const query = `+ SELECT+ count() AS count+ FROM analytics+ WHERE+ pid = {pid:FixedString(12)}+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}+ AND pg = {page:String}+ ${pages.length > 1 ? `AND prev = {prevPage:String}` : ''}+ `+ const promises = pages.map(async (page, index) => {+ const prevPage = index > 0 ? pages[index - 1] : null+ const result = await clickhouse+ .query(query, { params: { ...params, page, prevPage } })+ .toPromise()+ // @ts-ignore+ return result[0].count+ })++ const funnel = await Promise.all(promises)++ console.log('funnel', funnel)+ }+async getSessionHash(pid: string,userAgent: string,diff --git a/apps/production/src/analytics/interfaces/index.ts b/apps/production/src/analytics/interfaces/index.tsindex fda538f0c..8f3ad5913 100644--- a/apps/production/src/analytics/interfaces/index.ts+++ b/apps/production/src/analytics/interfaces/index.ts@@ -88,6 +88,11 @@ export interface IUserFlow {descending: IBuildUserFlow}+export interface IFunnel {+ pages: string[] // Array of page paths+ percentages: number[] // Percentages of each step+}+export interface IExtractChartData {visits: number[]uniques: number[]diff --git a/funnels.sql b/funnels.sqlnew file mode 100644index 000000000..9288da6b8--- /dev/null+++ b/funnels.sql@@ -0,0 +1,90 @@+-- My 'analytics' table has the following columns: pg and prev.+-- pg is the page name, and prev is the previous page name.+-- I need to calculate a funnel (amount of users that went from page A to page B and after that to page C (i.e. A -> B -> C...))+-- My database is Clickhouse.++-- The analytics table also have pid (project ID column) - in the query it has to be set to 'STEzHcB1rALV'+-- and created column which is a string date, it has to be set to BETWEEN '2023-05-13' AND '2023-07-13'++ -- count(t0), count(t1), count(t2)+-- SELECT+-- count(t0), count(t1), count(t2)+-- FROM analytics as t0+-- INNER JOIN analytics as t1 ON t0.pg = t1.prev AND t0.pid = t1.pid+-- INNER JOIN analytics as t2 ON t1.pg = t2.prev AND t1.pid = t2.pid+-- WHERE t0.pid = 'STEzHcB1rALV'+-- AND t0.created BETWEEN '2023-05-13' AND '2023-07-13'+-- AND t0.pg = 'page1'+-- AND t1.pg = 'page2'+-- AND t2.pg = 'page3';++SELECT+ count(t1.pg), count(t2.pg), count(t3.pg)+FROM analytics as t1+LEFT JOIN analytics as t2 ON t2.prev = t1.pg AND t2.pid = 'STEzHcB1rALV' AND t2.pg = '/dashboard'+LEFT JOIN analytics as t3 ON t3.prev = t2.pg AND t3.pid = 'STEzHcB1rALV' AND t3.pg = '/'+WHERE t1.pid = 'STEzHcB1rALV'+ AND t1.created BETWEEN '2023-05-13' AND '2023-07-13'+ AND t1.pg = '/settings';+++SELECT+ count(t1.pg) AS settings_count,+ count(t2.pg) AS dashboard_count,+ count(t3.pg) AS root_count+FROM+(+ SELECT pg, prev+ FROM analytics+ WHERE pid = 'STEzHcB1rALV'+ AND created BETWEEN '2023-05-13' AND '2023-07-13'+ AND pg = '/settings'+) AS t1+INNER JOIN+(+ SELECT pg, prev+ FROM analytics+ WHERE pid = 'STEzHcB1rALV'+ AND created BETWEEN '2023-05-13' AND '2023-07-13'+ AND pg = '/dashboard'+) AS t2 ON t2.prev = t1.pg+INNER JOIN+(+ SELECT pg, prev+ FROM analytics+ WHERE pid = 'STEzHcB1rALV'+ AND created BETWEEN '2023-05-13' AND '2023-07-13'+ AND pg = '/'+) AS t3 ON t3.prev = t2.pg;+++WITH t1 AS (+ SELECT pg, prev, pid, created+ FROM analytics+ WHERE pid = 'STEzHcB1rALV'+ AND created BETWEEN '2023-05-13' AND '2023-07-13'+ AND pg = '/settings'+),+t2 AS (+ SELECT pg, prev, pid, created+ FROM t1+ WHERE pid = 'STEzHcB1rALV'+ AND created BETWEEN '2023-05-13' AND '2023-07-13'+ AND pg = '/dashboard'+ AND prev IN (SELECT pg FROM t1)+),+t3 AS (+ SELECT pg, prev, pid, created+ FROM t2+ WHERE pid = 'STEzHcB1rALV'+ AND created BETWEEN '2023-05-13' AND '2023-07-13'+ AND pg = '/'+ AND prev IN (SELECT pg FROM t2)+)+SELECT+ count(t1.pg) AS settings_count,+ count(t2.pg) AS dashboard_count,+ count(t3.pg) AS root_count+FROM t1+INNER JOIN t2 ON t2.prev = t1.pg+INNER JOIN t3 ON t3.prev = t2.pg;