@@ -90,21 +90,21 @@ const serialize = ({
9090 countryCode,
9191 cultureInfo,
9292 channelPrivacy,
93- } : Partial < Segment > ) => {
93+ } : Partial < Segment > , preserveUtmChars = false ) => {
94+ const normalize = ( value : string | null | undefined ) =>
95+ value && ! preserveUtmChars ? removeNonLatin1Chars ( value ) : value ;
96+
9497 const seg = {
9598 campaigns,
9699 channel,
97100 priceTables,
98101 regionId,
99- utm_campaign : utm_campaign &&
100- removeNonLatin1Chars ( utm_campaign ) . replace ( / [ \/ \[ \] { } ( ) < > . ] / g, "" ) ,
101- utm_source : utm_source &&
102- removeNonLatin1Chars ( utm_source ) . replace ( / [ \/ \[ \] { } ( ) < > . ] / g, "" ) ,
103- utm_medium : utm_medium &&
104- removeNonLatin1Chars ( utm_medium ) . replace ( / [ \/ \[ \] { } ( ) < > . ] / g, "" ) ,
105- utmi_campaign : utmi_campaign && removeNonLatin1Chars ( utmi_campaign ) ,
106- utmi_page : utmi_page && removeNonLatin1Chars ( utmi_page ) ,
107- utmi_part : utmi_part && removeNonLatin1Chars ( utmi_part ) ,
102+ utm_campaign : normalize ( utm_campaign ) ?. replace ( / [ \/ \[ \] { } ( ) < > . ] / g, "" ) ,
103+ utm_source : normalize ( utm_source ) ?. replace ( / [ \/ \[ \] { } ( ) < > . ] / g, "" ) ,
104+ utm_medium : normalize ( utm_medium ) ?. replace ( / [ \/ \[ \] { } ( ) < > . ] / g, "" ) ,
105+ utmi_campaign : normalize ( utmi_campaign ) ,
106+ utmi_page : normalize ( utmi_page ) ,
107+ utmi_part : normalize ( utmi_part ) ,
108108 currencyCode,
109109 currencySymbol,
110110 countryCode,
@@ -168,10 +168,9 @@ export const setSegmentBag = (
168168 const segmentFromCookie = vtex_segment ? parse ( vtex_segment ) : null ;
169169
170170 const segmentFromSalesChannelCookie = cookies [ SALES_CHANNEL_COOKIE ]
171- ? {
172- channel : cookies [ SALES_CHANNEL_COOKIE ] ?. split ( "=" ) [ 1 ] ,
173- }
171+ ? { channel : cookies [ SALES_CHANNEL_COOKIE ] ?. split ( "=" ) [ 1 ] }
174172 : { } ;
173+
175174 const segmentFromRequest = buildSegmentFromRequest ( req ) ;
176175
177176 const segment = {
@@ -182,10 +181,10 @@ export const setSegmentBag = (
182181 ...segmentFromSalesChannelCookie ,
183182 ...segmentFromRequest ,
184183 } ;
185- const token = serialize ( segment ) ;
184+
185+ const token = serialize ( segment , ctx . preserveUtmChars ) ;
186186 setSegmentInBag ( ctx , { payload : segment , token } ) ;
187187
188- // If the user came from a sales channel in the URL, we set the cookie
189188 if ( segmentFromRequest . channel ) {
190189 setCookie ( ctx . response . headers , {
191190 value : `sc=${ segmentFromRequest . channel } ` ,
@@ -195,7 +194,6 @@ export const setSegmentBag = (
195194 } ) ;
196195 }
197196
198- // Avoid setting cookie when segment from request matches the one generated
199197 if ( vtex_segment !== token ) {
200198 setCookie ( ctx . response . headers , {
201199 value : token ,
0 commit comments