Skip to content

Commit

Permalink
feat(packages/sui-segment-wrapper): set ga user id on identify
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoruiz committed Oct 21, 2024
1 parent 23933a4 commit 1184d81
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/sui-segment-wrapper/src/repositories/googleRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ const getGoogleField = async field => {

export const getGoogleClientID = () => getGoogleField(FIELDS.clientId)
export const getGoogleSessionID = () => getGoogleField(FIELDS.sessionId)

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

if (!googleAnalyticsMeasurementId || !userId) return

window.gtag?.('set', googleAnalyticsMeasurementId, {
user_id: userId
})
}
4 changes: 3 additions & 1 deletion 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} from './repositories/googleRepository.js'
import {getGoogleClientID, getGoogleSessionID, sendGoogleUserId} 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,6 +208,8 @@ const track = (event, properties, context = {}, callback) =>
const identify = async (userId, traits, options, callback) => {
const gdprPrivacyValue = await getGdprPrivacyValue()

sendGoogleUserId(userId)

return window.analytics.identify(
userId,
checkAnalyticsGdprIsAccepted(gdprPrivacyValue) ? traits : {},
Expand Down
30 changes: 29 additions & 1 deletion packages/sui-segment-wrapper/test/segmentWrapperSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {expect} from 'chai'
import sinon from 'sinon'

import {getAdobeVisitorData} from '../src/repositories/adobeRepository.js'
import {setConfig} from '../src/config.js'
import {setConfig, getConfig} from '../src/config.js'
import suiAnalytics from '../src/index.js'
import {defaultContextProperties} from '../src/middlewares/source/defaultContextProperties.js'
import {pageReferrer} from '../src/middlewares/source/pageReferrer.js'
Expand Down Expand Up @@ -220,6 +220,7 @@ describe('Segment Wrapper', function () {
it('should send Google Analytics integration with true if user declined consents', async () => {
// Add the needed config to enable Google Analytics
setConfig('googleAnalyticsMeasurementId', 123)

await simulateUserDeclinedConsents()

await suiAnalytics.track(
Expand Down Expand Up @@ -310,6 +311,7 @@ describe('Segment Wrapper', function () {

describe('when the identify event is called', () => {
const DEFAULT_SEGMENT_CALLBACK_TIMEOUT = 350

it('should call sdk identify of users that accepts consents', async function () {
await simulateUserAcceptConsents()

Expand All @@ -319,6 +321,7 @@ describe('Segment Wrapper', function () {
await waitUntil(() => spy.callCount, {
timeout: DEFAULT_SEGMENT_CALLBACK_TIMEOUT
})

expect(spy.callCount).to.equal(1)
})

Expand All @@ -331,8 +334,33 @@ describe('Segment Wrapper', function () {
await waitUntil(() => spy.callCount, {
timeout: DEFAULT_SEGMENT_CALLBACK_TIMEOUT
}).catch(() => null)

expect(spy.callCount).to.equal(1)
})

describe('and GA Measurment ID is set', () => {
beforeEach(() => {
setConfig('googleAnalyticsMeasurementId', 123)
})

it('should set the user id into `gtag` properly', async function () {
await simulateUserAcceptConsents()
await suiAnalytics.identify('myTestUserId')

const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId')

const getGaUserId = async () =>
new Promise(resolve => {
window.gtag('get', googleAnalyticsMeasurementId, 'user_id', userId => {
resolve(userId)
})
})

const gaUserId = await getGaUserId()

expect(gaUserId).to.equal('myTestUserId')
})
})
})

describe('when TCF is present on the page', () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/sui-segment-wrapper/test/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ export const stubFetch = ({responses = [{urlRe: /^http/, fetchResponse: {}}]} =
}

export const stubGoogleAnalytics = () => {
const mockClientId = 'fakeClientId'
const mockSessionId = 'fakeSessionId'
const savedFields = {
client_id: 'fakeClientId',
session_id: 'fakeSessionId'
}

window.gtag = (key, id, fieldName, done) => {
if (key === 'get') {
const value = fieldName === 'client_id' ? mockClientId : mockSessionId
return done(savedFields?.[fieldName])
}

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

0 comments on commit 1184d81

Please sign in to comment.