Skip to content

Commit

Permalink
feat(packages/sui-segment-wrapper): modify set method
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoruiz committed Oct 21, 2024
1 parent 1184d81 commit cf37a8e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ const getGoogleField = async field => {
export const getGoogleClientID = () => getGoogleField(FIELDS.clientId)
export const getGoogleSessionID = () => getGoogleField(FIELDS.sessionId)

export const sendGoogleUserId = userId => {
export const setGoogleUserId = userId => {
const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId')

if (!googleAnalyticsMeasurementId || !userId) return

window.gtag?.('set', googleAnalyticsMeasurementId, {
user_id: userId
})
window.gtag?.('set', 'user_id', userId)
}
4 changes: 2 additions & 2 deletions packages/sui-segment-wrapper/src/segmentWrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

import {getAdobeMCVisitorID} from './repositories/adobeRepository.js'
import {getGoogleClientID, getGoogleSessionID, sendGoogleUserId} from './repositories/googleRepository.js'
import {getGoogleClientID, getGoogleSessionID, setGoogleUserId} from './repositories/googleRepository.js'
import {getConfig} from './config.js'
import {checkAnalyticsGdprIsAccepted, getGdprPrivacyValue} from './tcf.js'
import {getXandrId} from './repositories/xandrRepository.js'
Expand Down Expand Up @@ -208,7 +208,7 @@ const track = (event, properties, context = {}, callback) =>
const identify = async (userId, traits, options, callback) => {
const gdprPrivacyValue = await getGdprPrivacyValue()

sendGoogleUserId(userId)
setGoogleUserId(userId)

return window.analytics.identify(
userId,
Expand Down
10 changes: 4 additions & 6 deletions packages/sui-segment-wrapper/test/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ export const stubGoogleAnalytics = () => {
session_id: 'fakeSessionId'
}

window.gtag = (key, id, fieldName, done) => {
window.gtag = (key, id, field, done) => {
if (key === 'get') {
return done(savedFields?.[fieldName])
return done(savedFields?.[field])
}

if (key === 'set' && typeof fieldName === 'object') {
Object.keys(fieldName).forEach(field => {
savedFields[field] = fieldName[field]
})
if (key === 'set') {
savedFields[id] = field
}
}
}
Expand Down

0 comments on commit cf37a8e

Please sign in to comment.