Skip to content

Commit 5976304

Browse files
committed
fix(packages/sui-segment-wrapper): prevent context mutation causing GA4 integrations loss after firs
1 parent fa68707 commit 5976304

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

packages/sui-segment-wrapper/src/segmentWrapper.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ export const decorateContextWithNeededData = async ({event = '', context = {}, p
167167
getXandrId({gdprPrivacyValueAdvertising})
168168
])
169169

170-
if (!isGdprAccepted) {
171-
context.integrations = {
172-
...(context.integrations ?? {}),
173-
Personas: false,
174-
Webhooks: true,
175-
Webhook: true
176-
}
177-
}
170+
// Build integrations without mutating context
171+
const contextIntegrations = !isGdprAccepted
172+
? {
173+
...(context.integrations ?? {}),
174+
Personas: false,
175+
Webhooks: true,
176+
Webhook: true
177+
}
178+
: context.integrations
178179

179180
// Build Google Consent Mode object
180181
const googleConsents = {
@@ -200,7 +201,7 @@ export const decorateContextWithNeededData = async ({event = '', context = {}, p
200201
gdpr_privacy: gdprPrivacyValueAnalytics,
201202
gdpr_privacy_advertising: gdprPrivacyValueAdvertising,
202203
integrations: {
203-
...context.integrations,
204+
...contextIntegrations,
204205
...integrations
205206
}
206207
}
@@ -273,7 +274,6 @@ const track = (event, properties, context = {}, callback) =>
273274
decoratedProperties,
274275
{
275276
...newContext,
276-
integrations: newContext.integrations,
277277
context: {
278278
integrations: {
279279
...newContext.integrations

packages/sui-segment-wrapper/src/utils/patchAnalytics.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@ function monkeyPatchAnalyticsTrack() {
1313
}
1414
decorateContextWithNeededData({
1515
context: contextFromArgs,
16-
event
17-
}).then(context => {
18-
originalTrack.call(window.analytics, event, newProperties, context, fn)
16+
event,
17+
properties: newProperties
18+
}).then(({context: newContext, properties: decoratedProperties}) => {
19+
originalTrack.call(
20+
window.analytics,
21+
event,
22+
decoratedProperties,
23+
{
24+
...newContext,
25+
context: {
26+
integrations: {
27+
...newContext.integrations
28+
}
29+
}
30+
},
31+
fn
32+
)
1933
})
2034
return window.analytics
2135
}

0 commit comments

Comments
 (0)