Skip to content

Commit af2d6e1

Browse files
feat: Implements Global Session ID (#1217)
1 parent e477045 commit af2d6e1

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/library/zoid/message/component.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getLibraryVersion,
1515
runStats,
1616
logger,
17+
getOrCreateGlobalSessionID,
1718
getSessionID,
1819
getGlobalState,
1920
getCurrentTime,
@@ -248,6 +249,8 @@ export default createGlobalVariableGetter('__paypal_credit_message__', () =>
248249
// deviceID from internal iframe storage
249250
// should be populated previously by the treatments component
250251
deviceID: getOrCreateDeviceID(),
252+
// Global Session ID allows messages to be correlated to button events
253+
globalSessionID: getOrCreateGlobalSessionID(),
251254
// Session ID from parent local storage,
252255
sessionID: getSessionID()
253256
},

src/utils/logger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { request } from './miscellaneous';
1111
import { getLibraryVersion, getDisableSetCookie } from './sdk';
1212

1313
function generateLogPayload(account, { meta, events: bizEvents, tracking }) {
14-
const { deviceID, sessionID, integration_type, messaging_version } = meta.global ?? {};
14+
const { deviceID, sessionID, integration_type, messaging_version, globalSessionID } = meta.global ?? {};
1515

1616
let clientID;
1717
if (account.startsWith('client-id:')) {
@@ -98,6 +98,7 @@ function generateLogPayload(account, { meta, events: bizEvents, tracking }) {
9898

9999
// Global Details
100100
device_id: deviceID,
101+
global_session_id: globalSessionID,
101102
session_id: sessionID,
102103
integration_type,
103104
integration_version: messaging_version,

src/utils/sdk.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable eslint-comments/disable-enable-pair, no-else-return */
22
import arrayFrom from 'core-js-pure/stable/array/from';
3-
import { getStorage as getBelterStorage } from '@krakenjs/belter/src';
3+
import { getStorage as getBelterStorage, uniqueID as createBelterID } from '@krakenjs/belter/src';
44
import { SDK_QUERY_KEYS, SDK_SETTINGS } from '@paypal/sdk-constants/src';
55
import {
66
getClientID,
@@ -14,6 +14,8 @@ import {
1414
getCSPNonce,
1515
getNamespace as getSDKNamespace,
1616
getDefaultNamespace as getDefaultSDKNamespace,
17+
getGlobalSessionID as getSDKGlobalSessionID,
18+
setGlobalSessionID as setSDKGlobalSessionID,
1719
getSessionID as getSDKSessionID,
1820
getStorageID as getSDKStorageID,
1921
getStorageState as getSDKStorageState,
@@ -161,6 +163,23 @@ export function getStorage() {
161163
return getBelterStorage({ name: getNamespace() });
162164
}
163165

166+
// Uses SDK methods to get and set a global session ID
167+
// value will be passed in message_render events
168+
// and used to correlate with button events
169+
export function getOrCreateGlobalSessionID() {
170+
if (__MESSAGES__.__TARGET__ === 'SDK') {
171+
let globalSessionID = getSDKGlobalSessionID();
172+
if (!globalSessionID) {
173+
globalSessionID = createBelterID();
174+
setSDKGlobalSessionID(globalSessionID);
175+
}
176+
177+
return globalSessionID;
178+
} else {
179+
return getStorage().getSessionID();
180+
}
181+
}
182+
164183
// Use SDK methods when available, otherwise manually fetch storage via belter
165184
// see: https://github.com/paypal/paypal-sdk-client/blob/master/src/session.js
166185
export function getSessionID() {

0 commit comments

Comments
 (0)