Skip to content

Commit a6165d9

Browse files
committed
fix(packages/sui-segment-wrapper): init GA4 sync to prevent event loss on first page load
1 parent 5b79499 commit a6165d9

1 file changed

Lines changed: 29 additions & 32 deletions

File tree

  • packages/sui-segment-wrapper/src

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,43 +45,40 @@ const addMiddlewares = () => {
4545
}
4646

4747
if (isClient && window.analytics) {
48-
// Pre-cache GA4 destination detection as early as possible
49-
// This ensures the first track has the correct integration mode
50-
window.analytics.ready(() => {
51-
// Force detection to cache the result
52-
const useSegmentGA4Destination = isGA4DestinationEnabled()
48+
// Check GA4 mode synchronously to avoid delaying initialization
49+
const useSegmentGA4Destination = isGA4DestinationEnabled()
5350

54-
// Only initialize manual GA4 if destination is not enabled (legacy mode)
55-
if (!useSegmentGA4Destination) {
56-
// Legacy behavior: Initialize Google Analytics manually
57-
const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId')
58-
const dataLayerName = getConfig('googleAnalyticsDataLayer') || DEFAULT_DATA_LAYER_NAME
59-
const needsConsentManagement = getConfig('googleAnalyticsConsentManagement')
51+
// Only initialize manual GA4 if destination is not enabled (legacy mode)
52+
// This runs synchronously to ensure GA4 is ready for first events
53+
if (!useSegmentGA4Destination) {
54+
// Legacy behavior: Initialize Google Analytics manually
55+
const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId')
56+
const dataLayerName = getConfig('googleAnalyticsDataLayer') || DEFAULT_DATA_LAYER_NAME
57+
const needsConsentManagement = getConfig('googleAnalyticsConsentManagement')
6058

61-
if (googleAnalyticsMeasurementId) {
62-
const googleAnalyticsConfig = getConfig('googleAnalyticsConfig')
59+
if (googleAnalyticsMeasurementId) {
60+
const googleAnalyticsConfig = getConfig('googleAnalyticsConfig')
6361

64-
window[dataLayerName] = window[dataLayerName] || []
65-
window.gtag =
66-
window.gtag ||
67-
function gtag() {
68-
window[dataLayerName].push(arguments)
69-
}
62+
window[dataLayerName] = window[dataLayerName] || []
63+
window.gtag =
64+
window.gtag ||
65+
function gtag() {
66+
window[dataLayerName].push(arguments)
67+
}
7068

71-
window.gtag('js', new Date())
72-
if (needsConsentManagement) sendGoogleConsents()
73-
window.gtag('config', googleAnalyticsMeasurementId, {
74-
cookie_prefix: 'segment',
75-
send_page_view: false,
76-
...googleAnalyticsConfig,
77-
...getCampaignDetails()
78-
})
79-
loadGoogleAnalytics().catch(error => {
80-
console.error(error)
81-
})
82-
}
69+
window.gtag('js', new Date())
70+
if (needsConsentManagement) sendGoogleConsents()
71+
window.gtag('config', googleAnalyticsMeasurementId, {
72+
cookie_prefix: 'segment',
73+
send_page_view: false,
74+
...googleAnalyticsConfig,
75+
...getCampaignDetails()
76+
})
77+
loadGoogleAnalytics().catch(error => {
78+
console.error(error)
79+
})
8380
}
84-
})
81+
}
8582

8683
window.analytics.ready(checkAnonymousId)
8784
window.analytics.addSourceMiddleware ? addMiddlewares() : window.analytics.ready(addMiddlewares)

0 commit comments

Comments
 (0)