@@ -19,8 +19,8 @@ module.exports = (params) => {
1919
2020with
2121
22- ${ if ( params . includePages ) {
23- `first_and_last_page_values as (
22+ ${ params . includePages ?
23+ `first_and_last_page_values as (
2424select distinct
2525 session_id,
2626 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
@@ -43,10 +43,36 @@ select distinct
4343 } ) } as last_${ value } `) . join ( ",\n " ) }
4444 from
4545 ${ ctx . ref ( params . defaultConfig . schema , "segment_sessionized_pages" ) } as sessionized_pages
46- )`
47- } }
46+ ),` : `` }
47+
48+ ${ params . includeScreens ?
49+ `first_and_last_screen_values as (
50+ select distinct
51+ session_id,
52+ ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
53+ ( [ key , value ] ) => `${ crossdb . windowFunction ( {
54+ func : "first_value" ,
55+ value : value ,
56+ ignore_nulls : false ,
57+ partition_fields : "session_id" ,
58+ order_fields : 'sessionized_screens.timestamp asc' ,
59+ frame_clause : "rows between unbounded preceding and unbounded following" ,
60+ } ) } as first_${ value } `) . join ( ",\n " ) } ,
61+ ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
62+ ( [ key , value ] ) => `${ crossdb . windowFunction ( {
63+ func : "last_value" ,
64+ value : value ,
65+ ignore_nulls : false ,
66+ partition_fields : "session_id" ,
67+ order_fields : 'sessionized_screens.timestamp asc' ,
68+ frame_clause : "rows between unbounded preceding and unbounded following" ,
69+ } ) } as last_${ value } `) . join ( ",\n " ) }
70+ from
71+ ${ ctx . ref ( params . defaultConfig . schema , "segment_sessionized_screens" ) } as sessionized_screens
72+ ),` : `` }
4873
4974output as (
75+
5076select
5177 segment_sessionized_events.session_id,
5278 segment_sessionized_events.session_index,
@@ -57,68 +83,89 @@ select
5783 -- stats about the session
5884 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
5985 ${ segmentCommon . enabledEvents ( params ) . map ( ( event ) =>
60- `count(segment_sessionized_events.${ event } _id) as total_${ event } s, ` ) . join ( `,\n ` ) }
86+ `count(segment_sessionized_events.${ event } _id) as total_${ event } s` ) . join ( `,\n ` ) } ,
6187 ${ crossdb . timestampDiff ( "millisecond" , "min(segment_sessionized_events.timestamp)" , "max(segment_sessionized_events.timestamp)" ) } as duration_millis
6288 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as stats` ) } ,
89+
6390 -- first values in the session for page fields
64- ${if ( params . includePages ) {
65- ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
91+ ${ params . includePages ?
92+ ` ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
6693 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
6794 ( [ key , value ] ) => `first_and_last_page_values.first_${ value } ` ) . join ( ",\n " ) }
6895 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as first_page_values` ) } ,
6996 -- last values in the session for page fields
7097 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
7198 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
7299 ( [ key , value ] ) => `first_and_last_page_values.last_${ value } ` ) . join ( ",\n " ) }
73- ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as last_page_values` ) }
74- }}
100+ ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as last_page_values` ) } ` : `` }
101+
102+ -- first values in the session for screen fields
103+ ${ params . includeScreens ?
104+ `${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
105+ ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
106+ ( [ key , value ] ) => `first_and_last_screen_values.first_${ value } ` ) . join ( ",\n " ) }
107+ ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as first_screen_values` ) } ,
108+ -- last values in the session for screen fields
109+ ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
110+ ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
111+ ( [ key , value ] ) => `first_and_last_screen_values.last_${ value } ` ) . join ( ",\n " ) }
112+ ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as last_screen_values` ) } ` : `` }
113+
114+
75115 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `-- repeated array of records
76116 ,array_agg(
77117 struct(
78- segment_sessionized_events.timestamp,
79- ${ if ( includeTracks ) {
80- `struct(
118+ segment_sessionized_events.timestamp
119+ ${ params . includeTracks ?
120+ `, struct(
81121 segment_sessionized_tracks.timestamp,
82122 segment_sessionized_tracks.track_id,
83123 ${ Object . entries ( segmentCommon . allTrackFields ( params ) ) . map (
84- ( [ key , value ] ) => `segment_sessionized_tracks.${ value } ` ) . join ( ",\n " ) }
85- ) as track,`
86- } }
87- ${ if ( includePages ) {
88- `struct(
124+ ( [ key , value ] ) => `segment_sessionized_tracks.${ value } ` ) . join ( ",\n " ) }
125+ ) as track` : `` }
126+ ${ params . includePages ?
127+ `, struct(
89128 segment_sessionized_pages.timestamp,
90129 segment_sessionized_pages.page_id,
91130 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
92- ( [ key , value ] ) => `segment_sessionized_pages.${ value } ` ) . join ( ",\n " ) }
93- ) as page,`
94- } }
95- ${ if ( includeScreens ) {
96- `struct(
131+ ( [ key , value ] ) => `segment_sessionized_pages.${ value } ` ) . join ( ",\n " ) }
132+ ) as page` : `` }
133+ ${ params . includeScreens ?
134+ `, struct(
97135 segment_sessionized_pages.timestamp,
98136 segment_sessionized_pages.screen_id,
99137 ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
100- ( [ key , value ] ) => `segment_sessionized_screens.${ value } ` ) . join ( ",\n " ) }
101- ) as screen`
102- } }
138+ ( [ key , value ] ) => `segment_sessionized_screens.${ value } ` ) . join ( ",\n " ) }
139+ ) as screen` : `` }
103140 ) order by segment_sessionized_events.timestamp asc
104- ) as records` ) }
141+ ) as records`
142+ ) }
105143from
106144 ${ ctx . ref ( params . defaultConfig . schema , "segment_sessionized_events" ) } as segment_sessionized_events
107- left join first_and_last_page_values
108- using(session_id)
145+ ${ params . includePages ?
146+ `left join first_and_last_page_values
147+ using(session_id)` : `` }
148+ ${ params . includeScreens ?
149+ `left join first_and_last_screen_values
150+ using(session_id)` : `` }
109151 ${ ctx . when ( global . session . config . warehouse == "bigquery" ,
110152 segmentCommon . enabledEvents ( params ) . map ( ( event ) =>
111- `left join ${ ctx . ref ( params . defaultConfig . schema , " segment_sessionized_${event}s" ) } as segment_sessionized_${ event } s
153+ `left join ${ ctx . ref ( params . defaultConfig . schema , ` segment_sessionized_${ event } s` ) } as segment_sessionized_${ event } s
112154 using(${ event } _id)` ) . join ( `\n ` ) ) }
113155group by
114- session_id, session_index, user_id
115- ${if ( params . includePages ) {
116- ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
117- ( [ key , value ] ) => `, first_${ value } ` ) . join ( " " ) }
156+ session_id, session_index, user_id
157+ ${ params . includePages ?
158+ ` ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
159+ ( [ key , value ] ) => `, first_and_last_page_values. first_${ value } ` ) . join ( " " ) }
118160 ${ Object . entries ( segmentCommon . allPageFields ( params ) ) . map (
119- ( [ key , value ] ) => `, last_${ value } ` ) . join ( " " ) }
120- }}
161+ ( [ key , value ] ) => `, first_and_last_page_values.last_${ value } ` ) . join ( " " ) } ` : `` }
162+
163+ ${ params . includeScreens ?
164+ `${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
165+ ( [ key , value ] ) => `, first_and_last_screen_values.first_${ value } ` ) . join ( " " ) }
166+ ${ Object . entries ( segmentCommon . allScreenFields ( params ) ) . map (
167+ ( [ key , value ] ) => `, first_and_last_screen_values.last_${ value } ` ) . join ( " " ) } ` : `` }
168+ )
121169
122- select * from output
123- ` )
124- }
170+ select * from output` )
171+ }
0 commit comments