11import type { Filter , SimpleQueryConfig , TimeUnit } from "../types" ;
22
33export const ProfilesBuilders : Record < string , SimpleQueryConfig > = {
4- profile_list : {
5- customSql : (
6- websiteId : string ,
7- startDate : string ,
8- endDate : string ,
9- _filters ?: Filter [ ] ,
10- _granularity ?: TimeUnit ,
11- limit ?: number ,
12- offset ?: number ,
13- _timezone ?: string ,
14- filterConditions ?: string [ ] ,
15- filterParams ?: Record < string , Filter [ "value" ] >
16- ) => {
17- const combinedWhereClause = filterConditions ?. length
18- ? `AND ${ filterConditions . join ( " AND " ) } `
19- : "" ;
4+ profile_list : {
5+ customSql : (
6+ websiteId : string ,
7+ startDate : string ,
8+ endDate : string ,
9+ _filters ?: Filter [ ] ,
10+ _granularity ?: TimeUnit ,
11+ limit ?: number ,
12+ offset ?: number ,
13+ _timezone ?: string ,
14+ filterConditions ?: string [ ] ,
15+ filterParams ?: Record < string , Filter [ "value" ] >
16+ ) => {
17+ const combinedWhereClause = filterConditions ?. length
18+ ? `AND ${ filterConditions . join ( " AND " ) } `
19+ : "" ;
2020
21- return {
22- sql : `
21+ return {
22+ sql : `
2323 WITH visitor_profiles AS (
2424 SELECT
2525 anonymous_id as visitor_id,
2626 MIN(time) as first_visit,
2727 MAX(time) as last_visit,
2828 COUNT(DISTINCT session_id) as session_count,
2929 COUNT(*) as total_events,
30- COUNT(DISTINCT path) as unique_pages,
30+ COUNT(DISTINCT CASE WHEN event_name = 'screen_view' THEN path ELSE NULL END ) as unique_pages,
3131 any(user_agent) as user_agent,
3232 any(country) as country,
3333 any(region) as region,
@@ -53,7 +53,7 @@ export const ProfilesBuilders: Record<string, SimpleQueryConfig> = {
5353 MAX(e.time) as session_end,
5454 LEAST(dateDiff('second', MIN(e.time), MAX(e.time)), 28800) as duration,
5555 COUNT(*) as page_views,
56- COUNT(DISTINCT e. path) as unique_pages,
56+ COUNT(DISTINCT CASE WHEN e.event_name = 'screen_view' THEN e. path ELSE NULL END ) as unique_pages,
5757 any(e.user_agent) as user_agent,
5858 any(e.country) as country,
5959 any(e.region) as region,
@@ -115,41 +115,41 @@ export const ProfilesBuilders: Record<string, SimpleQueryConfig> = {
115115 LEFT JOIN visitor_sessions vs ON vp.visitor_id = vs.visitor_id
116116 ORDER BY vp.last_visit DESC, vs.session_start DESC
117117 ` ,
118- params : {
119- websiteId,
120- startDate,
121- endDate : `${ endDate } 23:59:59` ,
122- limit : limit || 25 ,
123- offset : offset || 0 ,
124- ...filterParams ,
125- } ,
126- } ;
127- } ,
128- } ,
118+ params : {
119+ websiteId,
120+ startDate,
121+ endDate : `${ endDate } 23:59:59` ,
122+ limit : limit || 25 ,
123+ offset : offset || 0 ,
124+ ...filterParams ,
125+ } ,
126+ } ;
127+ } ,
128+ } ,
129129
130- profile_detail : {
131- customSql : (
132- websiteId : string ,
133- startDate : string ,
134- endDate : string ,
135- filters ?: Filter [ ] ,
136- _granularity ?: TimeUnit ,
137- _limit ?: number ,
138- _offset ?: number ,
139- _timezone ?: string ,
140- _filterConditions ?: string [ ] ,
141- _filterParams ?: Record < string , Filter [ "value" ] >
142- ) => {
143- const visitorId = filters ?. find ( ( f ) => f . field === "anonymous_id" ) ?. value ;
130+ profile_detail : {
131+ customSql : (
132+ websiteId : string ,
133+ startDate : string ,
134+ endDate : string ,
135+ filters ?: Filter [ ] ,
136+ _granularity ?: TimeUnit ,
137+ _limit ?: number ,
138+ _offset ?: number ,
139+ _timezone ?: string ,
140+ _filterConditions ?: string [ ] ,
141+ _filterParams ?: Record < string , Filter [ "value" ] >
142+ ) => {
143+ const visitorId = filters ?. find ( ( f ) => f . field === "anonymous_id" ) ?. value ;
144144
145- if ( ! visitorId || typeof visitorId !== "string" ) {
146- throw new Error (
147- "anonymous_id filter is required for profile_detail query"
148- ) ;
149- }
145+ if ( ! visitorId || typeof visitorId !== "string" ) {
146+ throw new Error (
147+ "anonymous_id filter is required for profile_detail query"
148+ ) ;
149+ }
150150
151- return {
152- sql : `
151+ return {
152+ sql : `
153153 SELECT
154154 anonymous_id as visitor_id,
155155 MIN(time) as first_visit,
@@ -171,36 +171,36 @@ export const ProfilesBuilders: Record<string, SimpleQueryConfig> = {
171171 AND time <= toDateTime({endDate:String})
172172 GROUP BY anonymous_id
173173 ` ,
174- params : {
175- websiteId,
176- visitorId,
177- startDate,
178- endDate : `${ endDate } 23:59:59` ,
179- } ,
180- } ;
181- } ,
182- } ,
174+ params : {
175+ websiteId,
176+ visitorId,
177+ startDate,
178+ endDate : `${ endDate } 23:59:59` ,
179+ } ,
180+ } ;
181+ } ,
182+ } ,
183183
184- profile_sessions : {
185- customSql : (
186- websiteId : string ,
187- startDate : string ,
188- endDate : string ,
189- filters ?: Filter [ ] ,
190- _granularity ?: TimeUnit ,
191- limit = 100 ,
192- offset = 0
193- ) => {
194- const visitorId = filters ?. find ( ( f ) => f . field === "anonymous_id" ) ?. value ;
184+ profile_sessions : {
185+ customSql : (
186+ websiteId : string ,
187+ startDate : string ,
188+ endDate : string ,
189+ filters ?: Filter [ ] ,
190+ _granularity ?: TimeUnit ,
191+ limit = 100 ,
192+ offset = 0
193+ ) => {
194+ const visitorId = filters ?. find ( ( f ) => f . field === "anonymous_id" ) ?. value ;
195195
196- if ( ! visitorId || typeof visitorId !== "string" ) {
197- throw new Error (
198- "anonymous_id filter is required for profile_sessions query"
199- ) ;
200- }
196+ if ( ! visitorId || typeof visitorId !== "string" ) {
197+ throw new Error (
198+ "anonymous_id filter is required for profile_sessions query"
199+ ) ;
200+ }
201201
202- return {
203- sql : `
202+ return {
203+ sql : `
204204 WITH user_sessions AS (
205205 SELECT
206206 session_id,
@@ -272,18 +272,18 @@ export const ProfilesBuilders: Record<string, SimpleQueryConfig> = {
272272 LEFT JOIN session_events se ON us.session_id = se.session_id
273273 ORDER BY us.first_visit DESC
274274 ` ,
275- params : {
276- websiteId,
277- visitorId,
278- startDate,
279- endDate : `${ endDate } 23:59:59` ,
280- limit,
281- offset,
282- } ,
283- } ;
284- } ,
285- plugins : {
286- normalizeGeo : true ,
287- } ,
288- } ,
275+ params : {
276+ websiteId,
277+ visitorId,
278+ startDate,
279+ endDate : `${ endDate } 23:59:59` ,
280+ limit,
281+ offset,
282+ } ,
283+ } ;
284+ } ,
285+ plugins : {
286+ normalizeGeo : true ,
287+ } ,
288+ } ,
289289} ;
0 commit comments