Skip to content

Commit

Permalink
hot fix and remove funnels
Browse files Browse the repository at this point in the history
  • Loading branch information
kruzhambus committed Jul 18, 2023
1 parent aa5d7a8 commit cda7a73
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 195 deletions.
47 changes: 0 additions & 47 deletions apps/production/src/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,53 +654,6 @@ 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 project
Expand Down
52 changes: 0 additions & 52 deletions apps/production/src/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ import {
IUserFlowNode,
IUserFlowLink,
IUserFlow,
IFunnel,
IBuildUserFlow,
IExtractChartData,
IGenerateXAxis,
Expand Down Expand Up @@ -587,63 +586,12 @@ 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,
Expand Down
5 changes: 0 additions & 5 deletions apps/production/src/analytics/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ 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[]
Expand Down
2 changes: 1 addition & 1 deletion apps/production/src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
InternalServerErrorException,
ConflictException,
} from '@nestjs/common'
import net from 'net'
import * as net from 'net'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { customAlphabet } from 'nanoid'
Expand Down
90 changes: 0 additions & 90 deletions funnels.sql

This file was deleted.

0 comments on commit cda7a73

Please sign in to comment.