@@ -2,6 +2,11 @@ const segmentCommon = require("./common");
22const crossdb = require ( "./crossdb" ) ;
33
44module . exports = ( params ) => {
5+
6+ const customPageFieldsObj = params . customPageFields . reduce ( ( acc , item ) => ( { ...acc , [ item ] : item } ) , { } ) ;
7+
8+ const customTrackFieldsObj = params . customTrackFields . reduce ( ( acc , item ) => ( { ...acc , [ item ] : item } ) , { } ) ;
9+
510 return publish ( "segment_sessions" , {
611 description : "Sessions contain a combined view of tracks and pages from segment. Each session is a period of sustained activity, with a new session starting after a 30min+ period of inactivity. Each session contains a repeated field of records which are either tracks or pages. Common fields are extracted out into the top level and type specific fields are kept within two structs: records.track and records.page" ,
712 columns : {
@@ -16,15 +21,15 @@ module.exports = (params) => {
1621with first_and_last_page_values as (
1722select distinct
1823 session_id,
19- ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
24+ ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
2025 ( [ key , value ] ) => `${ crossdb . windowFunction ( {
2126 func : "first_value" ,
2227 value : value ,
2328 ignore_nulls : true ,
2429 partition_fields : "session_id" ,
2530 order_fields : 'sessionized_pages.timestamp asc' ,
2631 } ) } as first_${ value } `) . join ( ",\n " ) } ,
27- ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
32+ ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
2833 ( [ key , value ] ) => `${ crossdb . windowFunction ( {
2934 func : "last_value" ,
3035 value : value ,
@@ -52,13 +57,13 @@ select
5257
5358 -- first values in the session for page fields
5459 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
55- ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
60+ ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
5661 ( [ key , value ] ) => `first_and_last_page_values.first_${ value } ` ) . join ( ",\n " ) }
5762 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as first_page_values` ) } ,
5863
5964 -- last values in the session for page fields
6065 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `struct(\n ` ) }
61- ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
66+ ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
6267 ( [ key , value ] ) => `first_and_last_page_values.last_${ value } ` ) . join ( ",\n " ) }
6368 ${ ctx . when ( global . session . config . warehouse == "bigquery" , `) as last_page_values` ) }
6469
@@ -69,13 +74,13 @@ select
6974 struct(
7075 segment_sessionized_tracks.timestamp,
7176 segment_sessionized_tracks.track_id,
72- ${ Object . entries ( { ...segmentCommon . TRACK_FIELDS , ...segmentCommon . customTrackFieldsObj } ) . map (
77+ ${ Object . entries ( { ...segmentCommon . TRACK_FIELDS , ...customTrackFieldsObj } ) . map (
7378 ( [ key , value ] ) => `segment_sessionized_tracks.${ value } ` ) . join ( ",\n " ) }
7479 ) as track,
7580 struct(
7681 segment_sessionized_pages.timestamp,
7782 segment_sessionized_pages.page_id,
78- ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
83+ ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
7984 ( [ key , value ] ) => `segment_sessionized_pages.${ value } ` ) . join ( ",\n " ) }
8085 ) as page
8186 ) order by segment_sessionized_events.timestamp asc
9095 left join ${ ctx . ref ( params . defaultConfig . schema , "segment_sessionized_tracks" ) } as segment_sessionized_tracks
9196 using(track_id)` ) }
9297group by
93- session_id, session_index, user_id ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
98+ session_id, session_index, user_id ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
9499 ( [ key , value ] ) => `, first_${ value } ` ) . join ( " " ) }
95- ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...segmentCommon . customPageFieldsObj } ) . map (
100+ ${ Object . entries ( { ...segmentCommon . PAGE_FIELDS , ...customPageFieldsObj } ) . map (
96101 ( [ key , value ] ) => `, last_${ value } ` ) . join ( " " ) }
97102` )
98103}
0 commit comments