diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts index af6f0e60a..32094f77a 100644 --- a/backend/apps/cloud/src/analytics/analytics.controller.ts +++ b/backend/apps/cloud/src/analytics/analytics.controller.ts @@ -2127,7 +2127,7 @@ export class AnalyticsController { timeBucket = getLowestPossibleTimeBucket(period, from, to) } - const [filtersQuery, filtersParams, appliedFilters] = + const [filtersQuery, filtersParams, appliedFilters, customEVFilterApplied] = this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS) const safeTimezone = this.analyticsService.getSafeTimezone(timezone) @@ -2157,6 +2157,7 @@ export class AnalyticsController { take, skip, profileType, + customEVFilterApplied, ) return { profiles, appliedFilters, take, skip } diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts index 83420d65a..8318b3b5d 100644 --- a/backend/apps/cloud/src/analytics/analytics.service.ts +++ b/backend/apps/cloud/src/analytics/analytics.service.ts @@ -4940,6 +4940,7 @@ export class AnalyticsService { take = 30, skip = 0, profileType: 'all' | 'anonymous' | 'identified' = 'all', + customEVFilterApplied = false, ): Promise { let profileTypeFilter = '' if (profileType === 'anonymous') { @@ -4948,42 +4949,94 @@ export class AnalyticsService { profileTypeFilter = `AND profileId LIKE '${AnalyticsService.PROFILE_PREFIX_USER}%'` } + let allProfileDataCTE: string + + if (customEVFilterApplied) { + allProfileDataCTE = ` + all_profile_data AS ( + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 1 AS isPageview, + 0 AS isEvent + FROM analytics + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + AND profileId IN ( + SELECT DISTINCT profileId + FROM customEV + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + ) + UNION ALL + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 0 AS isPageview, + 1 AS isEvent + FROM customEV + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + )` + } else { + allProfileDataCTE = ` + all_profile_data AS ( + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 1 AS isPageview, + 0 AS isEvent + FROM analytics + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + UNION ALL + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 0 AS isPageview, + 1 AS isEvent + FROM customEV + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + )` + } + const query = ` - WITH all_profile_data AS ( - SELECT - profileId, - psid, - cc, - os, - br, - dv, - created, - 1 AS isPageview, - 0 AS isEvent - FROM analytics - WHERE pid = {pid:FixedString(12)} - AND profileId IS NOT NULL - AND profileId != '' - ${profileTypeFilter} - ${filtersQuery} - UNION ALL - SELECT - profileId, - psid, - cc, - os, - br, - dv, - created, - 0 AS isPageview, - 1 AS isEvent - FROM customEV - WHERE pid = {pid:FixedString(12)} - AND profileId IS NOT NULL - AND profileId != '' - ${profileTypeFilter} - ${filtersQuery} - ), + WITH ${allProfileDataCTE}, profile_aggregated AS ( SELECT profileId, diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts index 39c78af4d..4015b375c 100644 --- a/backend/apps/community/src/analytics/analytics.controller.ts +++ b/backend/apps/community/src/analytics/analytics.controller.ts @@ -2068,7 +2068,7 @@ export class AnalyticsController { timeBucket = getLowestPossibleTimeBucket(period, from, to) } - const [filtersQuery, filtersParams, appliedFilters] = + const [filtersQuery, filtersParams, appliedFilters, customEVFilterApplied] = this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS) const safeTimezone = this.analyticsService.getSafeTimezone(timezone) @@ -2098,6 +2098,7 @@ export class AnalyticsController { take, skip, profileType, + customEVFilterApplied, ) return { profiles, appliedFilters, take, skip } diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts index 2d9f0ffa5..c2d1589c2 100644 --- a/backend/apps/community/src/analytics/analytics.service.ts +++ b/backend/apps/community/src/analytics/analytics.service.ts @@ -5182,6 +5182,7 @@ export class AnalyticsService { take = 30, skip = 0, profileType: 'all' | 'anonymous' | 'identified' = 'all', + customEVFilterApplied = false, ): Promise { let profileTypeFilter = '' if (profileType === 'anonymous') { @@ -5190,42 +5191,94 @@ export class AnalyticsService { profileTypeFilter = `AND profileId LIKE '${AnalyticsService.PROFILE_PREFIX_USER}%'` } + let allProfileDataCTE: string + + if (customEVFilterApplied) { + allProfileDataCTE = ` + all_profile_data AS ( + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 1 AS isPageview, + 0 AS isEvent + FROM analytics + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + AND profileId IN ( + SELECT DISTINCT profileId + FROM customEV + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + ) + UNION ALL + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 0 AS isPageview, + 1 AS isEvent + FROM customEV + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + )` + } else { + allProfileDataCTE = ` + all_profile_data AS ( + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 1 AS isPageview, + 0 AS isEvent + FROM analytics + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + UNION ALL + SELECT + profileId, + psid, + cc, + os, + br, + dv, + created, + 0 AS isPageview, + 1 AS isEvent + FROM customEV + WHERE pid = {pid:FixedString(12)} + AND profileId IS NOT NULL + AND profileId != '' + ${profileTypeFilter} + ${filtersQuery} + )` + } + const query = ` - WITH all_profile_data AS ( - SELECT - profileId, - psid, - cc, - os, - br, - dv, - created, - 1 AS isPageview, - 0 AS isEvent - FROM analytics - WHERE pid = {pid:FixedString(12)} - AND profileId IS NOT NULL - AND profileId != '' - ${profileTypeFilter} - ${filtersQuery} - UNION ALL - SELECT - profileId, - psid, - cc, - os, - br, - dv, - created, - 0 AS isPageview, - 1 AS isEvent - FROM customEV - WHERE pid = {pid:FixedString(12)} - AND profileId IS NOT NULL - AND profileId != '' - ${profileTypeFilter} - ${filtersQuery} - ), + WITH ${allProfileDataCTE}, profile_aggregated AS ( SELECT profileId,