-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1834 from SUI-Components/add-ga4-integration-object
feat(packages/sui-segment-wrapper): add ga4 client id to segment events
- Loading branch information
Showing
17 changed files
with
180 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
import './patchAnalytics.js' | ||
import './utils/patchAnalytics.js' | ||
|
||
import {defaultContextProperties} from './middlewares/source/defaultContextProperties.js' | ||
import {pageReferrer} from './middlewares/source/pageReferrer.js' | ||
import {userScreenInfo} from './middlewares/source/userScreenInfo.js' | ||
import {userTraits} from './middlewares/source/userTraits.js' | ||
import {checkAnonymousId} from './checkAnonymousId.js' | ||
import {isClient} from './config.js' | ||
import {checkAnonymousId} from './utils/checkAnonymousId.js' | ||
import {getConfig, isClient} from './config.js' | ||
import analytics from './segmentWrapper.js' | ||
import initTcfTracking from './tcf.js' | ||
import {getUserDataAndNotify} from './universalId.js' | ||
import {loadGoogleAnalytics} from './repositories/googleRepository.js' | ||
|
||
/* Initialize TCF Tracking with Segment */ | ||
// Initialize TCF Tracking with Segment | ||
initTcfTracking() | ||
|
||
/* Generate UniversalId if available */ | ||
// Generate UniversalId if available | ||
try { | ||
getUserDataAndNotify() | ||
} catch (e) { | ||
console.error(`[segment-wrapper] UniversalID couldn't be initialized`) // eslint-disable-line | ||
} | ||
|
||
/* Initialize middlewares */ | ||
// Initialize middlewares | ||
const addMiddlewares = () => { | ||
window.analytics.addSourceMiddleware(userTraits) | ||
window.analytics.addSourceMiddleware(defaultContextProperties) | ||
window.analytics.addSourceMiddleware(userScreenInfo) | ||
window.analytics.addSourceMiddleware(pageReferrer) | ||
} | ||
|
||
/* Initialize Segment on Client */ | ||
if (isClient && window.analytics) { | ||
// Initialize Google Analtyics if needed | ||
const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId') | ||
|
||
if (googleAnalyticsMeasurementId) { | ||
window.dataLayer = window.dataLayer || [] | ||
window.gtag = | ||
window.gtag || | ||
function gtag() { | ||
window.dataLayer.push(arguments) | ||
} | ||
|
||
loadGoogleAnalytics() | ||
} | ||
|
||
window.analytics.ready(checkAnonymousId) | ||
window.analytics.addSourceMiddleware ? addMiddlewares() : window.analytics.ready(addMiddlewares) | ||
} | ||
|
||
export default analytics | ||
export {getAdobeVisitorData, getAdobeMCVisitorID} from './adobeRepository.js' | ||
export {getAdobeVisitorData, getAdobeMCVisitorID} from './repositories/adobeRepository.js' | ||
export {getUniversalId} from './universalId.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/sui-segment-wrapper/src/repositories/googleRepository.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {getConfig} from '../config.js' | ||
|
||
const FIELDS = { | ||
clientId: 'client_id', | ||
sessionId: 'session_id' | ||
} | ||
|
||
const cachedData = { | ||
[FIELDS.clientId]: null, | ||
[FIELDS.sessionId]: null | ||
} | ||
|
||
const loadScript = async src => | ||
new Promise(function (resolve, reject) { | ||
const script = document.createElement('script') | ||
|
||
script.src = src | ||
script.onload = resolve | ||
script.onerror = reject | ||
document.head.appendChild(script) | ||
}) | ||
|
||
export const loadGoogleAnalytics = async () => { | ||
const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId') | ||
|
||
// Check we have the needed config to load the script | ||
if (!googleAnalyticsMeasurementId) return Promise.resolve(false) | ||
// Create the `gtag` script | ||
const gtagScript = `https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsMeasurementId}` | ||
// Load it and retrieve the `clientId` from Google | ||
return loadScript(gtagScript) | ||
} | ||
|
||
const getGoogleField = async field => { | ||
const googleAnalyticsMeasurementId = getConfig('googleAnalyticsMeasurementId') | ||
|
||
// If `googleAnalyticsMeasurementId` is not present, don't load anything | ||
if (!googleAnalyticsMeasurementId) return Promise.resolve() | ||
|
||
return new Promise(resolve => { | ||
// Try to get the field from the stored info | ||
if (cachedData[field]) return resolve(cachedData[field]) | ||
// if not, get it from the `GoogleAnalytics` tag | ||
window.gtag?.('get', googleAnalyticsMeasurementId, field, id => { | ||
// Cache locally the field value | ||
cachedData[field] = id | ||
// Resolve the promise with the field | ||
resolve(id) | ||
}) | ||
}) | ||
} | ||
|
||
export const getGoogleClientID = () => getGoogleField(FIELDS.clientId) | ||
export const getGoogleSessionID = () => getGoogleField(FIELDS.sessionId) |
6 changes: 3 additions & 3 deletions
6
...ui-segment-wrapper/src/xandrRepository.js → ...apper/src/repositories/xandrRepository.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...sui-segment-wrapper/src/patchAnalytics.js → ...gment-wrapper/src/utils/patchAnalytics.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.