@rudderstack/analytics-js 3.25.1 → 3.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/dist/npm/legacy/bundled/cjs/index.cjs +49 -30
- package/dist/npm/legacy/bundled/esm/index.mjs +49 -30
- package/dist/npm/legacy/bundled/umd/index.js +49 -30
- package/dist/npm/legacy/cjs/index.cjs +49 -30
- package/dist/npm/legacy/content-script/cjs/index.cjs +49 -30
- package/dist/npm/legacy/content-script/esm/index.mjs +49 -30
- package/dist/npm/legacy/content-script/umd/index.js +49 -30
- package/dist/npm/legacy/esm/index.mjs +49 -30
- package/dist/npm/legacy/umd/index.js +49 -30
- package/dist/npm/modern/bundled/cjs/index.cjs +48 -29
- package/dist/npm/modern/bundled/esm/index.mjs +48 -29
- package/dist/npm/modern/bundled/umd/index.js +48 -29
- package/dist/npm/modern/cjs/index.cjs +48 -29
- package/dist/npm/modern/content-script/cjs/index.cjs +48 -29
- package/dist/npm/modern/content-script/esm/index.mjs +48 -29
- package/dist/npm/modern/content-script/umd/index.js +48 -29
- package/dist/npm/modern/esm/index.mjs +48 -29
- package/dist/npm/modern/umd/index.js +48 -29
- package/package.json +1 -1
|
@@ -521,7 +521,7 @@
|
|
|
521
521
|
error.stacktrace=`${stacktrace}\n${MANUAL_ERROR_IDENTIFIER}`;break;case operaSourceloc:default:// eslint-disable-next-line no-param-reassign
|
|
522
522
|
error['opera#sourceloc']=`${operaSourceloc}\n${MANUAL_ERROR_IDENTIFIER}`;break;}}}globalThis.dispatchEvent(new ErrorEvent('error',{error,bubbles:true,cancelable:true,composed:true}));};
|
|
523
523
|
|
|
524
|
-
const APP_NAME='RudderLabs JavaScript SDK';const APP_VERSION='3.
|
|
524
|
+
const APP_NAME='RudderLabs JavaScript SDK';const APP_VERSION='3.27.0';const APP_NAMESPACE='com.rudderlabs.javascript';const MODULE_TYPE='npm';const ADBLOCK_PAGE_CATEGORY='RudderJS-Initiated';const ADBLOCK_PAGE_NAME='ad-block page request';const ADBLOCK_PAGE_PATH='/ad-blocked';const GLOBAL_PRELOAD_BUFFER='preloadedEventsBuffer';const CONSENT_TRACK_EVENT_NAME='Consent Management Interaction';
|
|
525
525
|
|
|
526
526
|
const QUERY_PARAM_TRAIT_PREFIX='ajs_trait_';const QUERY_PARAM_PROPERTY_PREFIX='ajs_prop_';const QUERY_PARAM_ANONYMOUS_ID_KEY='ajs_aid';const QUERY_PARAM_USER_ID_KEY='ajs_uid';const QUERY_PARAM_TRACK_EVENT_NAME_KEY='ajs_event';
|
|
527
527
|
|
|
@@ -549,8 +549,9 @@
|
|
|
549
549
|
* Parse query string into preload buffer events & push into existing array before any other events
|
|
550
550
|
*/const retrieveEventsFromQueryString=(argumentsArray=[])=>{// Mapping for trait and properties values based on key prefix
|
|
551
551
|
const eventArgumentToQueryParamMap={trait:QUERY_PARAM_TRAIT_PREFIX,properties:QUERY_PARAM_PROPERTY_PREFIX};const queryObject=new URLSearchParams(globalThis.location.search);// Add track events with name and properties
|
|
552
|
-
if(queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY)){argumentsArray.unshift(['track',queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY),getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.properties)]);}//
|
|
553
|
-
|
|
552
|
+
if(queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY)){argumentsArray.unshift(['track',queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY),getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.properties)]);}// Send identify event
|
|
553
|
+
const userId=queryObject.get(QUERY_PARAM_USER_ID_KEY);const userTraits=getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.trait);if(userId||isNonEmptyObject(userTraits)){// In identify API, user ID is optional
|
|
554
|
+
const identifyApiArgs=[...(userId?[userId]:[]),userTraits];argumentsArray.unshift(['identify',...identifyApiArgs]);}// Set anonymousID
|
|
554
555
|
if(queryObject.get(QUERY_PARAM_ANONYMOUS_ID_KEY)){argumentsArray.unshift(['setAnonymousId',queryObject.get(QUERY_PARAM_ANONYMOUS_ID_KEY)]);}};/**
|
|
555
556
|
* Retrieve an existing buffered load method call and remove from the existing array
|
|
556
557
|
*/const getPreloadedLoadEvent=preloadedEventsArray=>{const loadMethodName='load';let loadEvent=[];/**
|
|
@@ -832,16 +833,14 @@
|
|
|
832
833
|
// Ex: parentFolderName will be 'sample' for url: https://example.com/sample/file.min.js
|
|
833
834
|
const parentFolderName=paths[paths.length-2];return parentFolderName===CDN_INT_DIR||SDK_FILE_NAME_PREFIXES().some(prefix=>srcFileName.startsWith(prefix)&&srcFileName.endsWith('.js'));};const getErrorDeliveryPayload=(payload,state,category)=>{const data={version:METRICS_PAYLOAD_VERSION,message_id:generateUUID(),source:{name:SOURCE_NAME,sdk_version:state.context.app.value.version,write_key:state.lifecycle.writeKey.value,install_type:state.context.app.value.installType,category:category??DEFAULT_ERROR_CATEGORY},errors:payload};return stringifyWithoutCircular(data);};/**
|
|
834
835
|
* A function to get the grouping hash value to be used for the error event.
|
|
835
|
-
* Grouping hash is suppressed for non-cdn installs.
|
|
836
836
|
* If the grouping hash is an error instance, the normalized error message is used as the grouping hash.
|
|
837
837
|
* If the grouping hash is an empty string or not specified, the default grouping hash is used.
|
|
838
838
|
* If the grouping hash is a string, it is used as is.
|
|
839
839
|
* @param curErrGroupingHash The grouping hash value part of the error event
|
|
840
840
|
* @param defaultGroupingHash The default grouping hash value. It is the error message.
|
|
841
|
-
* @param state The application state
|
|
842
841
|
* @param logger The logger instance
|
|
843
842
|
* @returns The final grouping hash value to be used for the error event
|
|
844
|
-
*/const getErrorGroupingHash=(curErrGroupingHash,defaultGroupingHash,
|
|
843
|
+
*/const getErrorGroupingHash=(curErrGroupingHash,defaultGroupingHash,logger)=>{let normalizedGroupingHash;if(!isDefined(curErrGroupingHash)){normalizedGroupingHash=defaultGroupingHash;}else if(isString(curErrGroupingHash)){normalizedGroupingHash=curErrGroupingHash;}else {const normalizedErrorInstance=normalizeError(curErrGroupingHash,logger);if(isDefined(normalizedErrorInstance)){normalizedGroupingHash=normalizedErrorInstance.message;}else {normalizedGroupingHash=defaultGroupingHash;}}return normalizedGroupingHash;};
|
|
845
844
|
|
|
846
845
|
/**
|
|
847
846
|
* A service to handle errors
|
|
@@ -865,13 +864,13 @@
|
|
|
865
864
|
*/async onError(errorInfo){try{const{error,context,customMessage,groupingHash,category}=errorInfo;const errorType=errorInfo.errorType??ErrorType.HANDLEDEXCEPTION;const errInstance=getErrInstance(error,errorType);const normalizedError=normalizeError(errInstance,this.logger);if(isUndefined(normalizedError)){return;}const customMsgVal=customMessage?`${customMessage} - `:'';const errorMsgPrefix=`${context}${LOG_CONTEXT_SEPARATOR}${customMsgVal}`;const bsException=createBugsnagException(normalizedError,errorMsgPrefix);const stacktrace=getStacktrace(normalizedError);const isSdkDispatched=stacktrace.includes(MANUAL_ERROR_IDENTIFIER);// Filter errors that are not originated in the SDK.
|
|
866
865
|
// In case of NPM installations, the unhandled errors from the SDK cannot be identified
|
|
867
866
|
// and will NOT be reported unless they occur in plugins or integrations.
|
|
868
|
-
if(!isSdkDispatched&&!isSDKError(bsException)&&errorType!==ErrorType.HANDLEDEXCEPTION){return;}if(state.reporting.isErrorReportingEnabled.value){const isAllowed=await checkIfAllowedToBeNotified(bsException,state,this.httpClient);if(isAllowed){const errorState={severity:'error',unhandled:errorType!==ErrorType.HANDLEDEXCEPTION,severityReason:{type:errorType}};//
|
|
869
|
-
//
|
|
870
|
-
//
|
|
867
|
+
if(!isSdkDispatched&&!isSDKError(bsException)&&errorType!==ErrorType.HANDLEDEXCEPTION){return;}if(state.reporting.isErrorReportingEnabled.value){const isAllowed=await checkIfAllowedToBeNotified(bsException,state,this.httpClient);if(isAllowed){const errorState={severity:'error',unhandled:errorType!==ErrorType.HANDLEDEXCEPTION,severityReason:{type:errorType}};// This will allow custom grouping of errors.
|
|
868
|
+
// In case of NPM installations, the default grouping by surrounding code
|
|
869
|
+
// does not make sense as each user application is different and will create a lot of noise in the alerts.
|
|
871
870
|
// References:
|
|
872
871
|
// https://docs.bugsnag.com/platforms/javascript/customizing-error-reports/#groupinghash
|
|
873
872
|
// https://docs.bugsnag.com/product/error-grouping/#user_defined
|
|
874
|
-
const normalizedGroupingHash=getErrorGroupingHash(groupingHash,bsException.message,
|
|
873
|
+
const normalizedGroupingHash=getErrorGroupingHash(groupingHash,bsException.message,this.logger);// Get the final payload to be sent to the metrics service
|
|
875
874
|
const bugsnagPayload=getBugsnagErrorEvent(bsException,errorState,state,normalizedGroupingHash);// send it to metrics service
|
|
876
875
|
this.httpClient.getAsyncData({url:state.metrics.metricsServiceUrl.value,options:{method:'POST',data:getErrorDeliveryPayload(bugsnagPayload,state,category),sendRawData:true},isRawResponse:true});}}// Log handled errors and errors dispatched by the SDK
|
|
877
876
|
if(errorType===ErrorType.HANDLEDEXCEPTION||isSdkDispatched){this.logger.error(bsException.message);}}catch(err){// If an error occurs while handling an error, log it
|
|
@@ -3295,7 +3294,9 @@
|
|
|
3295
3294
|
* @param event Incoming event data
|
|
3296
3295
|
*/addEvent(event){this.userSessionManager.refreshSession();const rudderEvent=this.eventFactory.create(event);this.eventRepository.enqueue(rudderEvent,event.callback);}}
|
|
3297
3296
|
|
|
3298
|
-
class UserSessionManager{
|
|
3297
|
+
class UserSessionManager{/**
|
|
3298
|
+
* Tracks whether a server-side cookie setting request is in progress or not.
|
|
3299
|
+
*/constructor(pluginsManager,storeManager,httpClient,errorHandler,logger){this.storeManager=storeManager;this.pluginsManager=pluginsManager;this.logger=logger;this.errorHandler=errorHandler;this.httpClient=httpClient;this.onError=this.onError.bind(this);this.serverSideCookieDebounceFuncs={};this.serverSideCookiesRequestInProgress={};}/**
|
|
3299
3300
|
* Initialize User session with values from storage
|
|
3300
3301
|
*/init(){this.syncStorageDataToState();// Register the effect to sync with storage
|
|
3301
3302
|
this.registerEffects();}syncStorageDataToState(){this.migrateStorageIfNeeded();this.migrateDataFromPreviousStorage();// get the values from storage and set it again
|
|
@@ -3325,20 +3326,31 @@
|
|
|
3325
3326
|
* @param callback
|
|
3326
3327
|
*/makeRequestToSetCookie(encryptedCookieData,callback){this.httpClient?.getAsyncData({url:state.serverCookies.dataServiceUrl.value,options:{method:'POST',data:stringifyWithoutCircular({reqType:'setCookies',workspaceId:state.source.value?.workspaceId,data:{options:{maxAge:state.storage.cookie.value?.maxage,path:state.storage.cookie.value?.path,domain:state.storage.cookie.value?.domain,sameSite:state.storage.cookie.value?.samesite,secure:state.storage.cookie.value?.secure,expires:state.storage.cookie.value?.expires},cookies:encryptedCookieData}}),sendRawData:true,withCredentials:true},isRawResponse:true,callback});}/**
|
|
3327
3328
|
* A function to make an external request to set the cookie from server side
|
|
3328
|
-
* @param key
|
|
3329
|
-
* @param
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3329
|
+
* @param sessionToCookiesMap map of session key to cookie name
|
|
3330
|
+
* @param cb callback function to be called when the cookie is set
|
|
3331
|
+
* @param store store to be used to get the cookie value
|
|
3332
|
+
*/setServerSideCookies(sessionToCookiesMap,cb,store){// Retrieve the cookie value from the state
|
|
3333
|
+
const sessionKeys=Object.keys(sessionToCookiesMap);const getCurrentCookieValuesFromState=()=>{return sessionKeys.map(sessionKey=>{return {name:sessionToCookiesMap[sessionKey].name,value:state.session[sessionKey].value};});};// Preserve the current cookie values
|
|
3334
|
+
const originalCookieValues={};sessionKeys.forEach(sessionKey=>{originalCookieValues[sessionToCookiesMap[sessionKey].name]=store?.get(sessionToCookiesMap[sessionKey].name);});const clearInProgressFlags=()=>{sessionKeys.forEach(sessionKey=>{this.serverSideCookiesRequestInProgress[sessionKey]=false;});};const setCookiesClientSide=()=>{getCurrentCookieValuesFromState().forEach(each=>{if(cb){cb(each.name,each.value);}});};try{const expectedCookieValues={};sessionKeys.forEach(sessionKey=>{expectedCookieValues[sessionToCookiesMap[sessionKey].name]=state.session[sessionKey].value;});// encrypt cookies values
|
|
3335
|
+
const encryptedCookieData=this.getEncryptedCookieData(getCurrentCookieValuesFromState(),store);if(encryptedCookieData.length>0){// make request to data service to set the cookie from server side
|
|
3336
|
+
this.makeRequestToSetCookie(encryptedCookieData,(res,details)=>{// Mark the cookie req status as done
|
|
3337
|
+
clearInProgressFlags();if(details?.xhr?.status===200){getCurrentCookieValuesFromState().forEach(cData=>{const originalCookieVal=originalCookieValues[cData.name];const currentCookieVal=store?.get(cData.name);// Check if the expected cookie values are set.
|
|
3338
|
+
if(stringifyWithoutCircular(expectedCookieValues[cData.name],false,[])!==stringifyWithoutCircular(currentCookieVal,false,[])){// It's fine if the values don't match as other active SDK sessions might have updated the cookie values
|
|
3339
|
+
// or other cookie requests might have updated the cookie value.
|
|
3340
|
+
// Log an error only when cookie didn't exist previously and currently also doesn't exist.
|
|
3341
|
+
if(isNull(originalCookieVal)&&isNull(currentCookieVal)){this.logger.error(FAILED_SETTING_COOKIE_FROM_SERVER_ERROR(cData.name));}if(cb){cb(cData.name,cData.value);}}});}else {this.logger.error(DATA_SERVER_REQUEST_FAIL_ERROR(details?.xhr?.status));setCookiesClientSide();}});}else {setCookiesClientSide();// Mark the cookie req status as done
|
|
3342
|
+
clearInProgressFlags();}}catch(e){this.onError(e,FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR,FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR);setCookiesClientSide();// Mark the cookie req status as done
|
|
3343
|
+
clearInProgressFlags();}}/**
|
|
3333
3344
|
* A function to sync values in storage
|
|
3334
3345
|
* @param sessionKey
|
|
3335
|
-
|
|
3336
|
-
*/syncValueToStorage(sessionKey,value){const entries=state.storage.entries.value;const storageType=entries[sessionKey]?.type;if(isStorageTypeValidForStoringData(storageType)){const curStore=this.storeManager?.getStore(storageClientDataStoreNameMap[storageType]);const key=entries[sessionKey]?.key;if(value&&(isString(value)||isNonEmptyObject(value))){// if useServerSideCookies load option is set to true
|
|
3346
|
+
*/syncValueToStorage(sessionKey){const entries=state.storage.entries.value;const storageType=entries[sessionKey]?.type;if(isStorageTypeValidForStoringData(storageType)){const curStore=this.storeManager.getStore(storageClientDataStoreNameMap[storageType]);const cookieName=entries[sessionKey]?.key;const cookieValue=state.session[sessionKey].value;if(cookieValue&&(isString(cookieValue)||isNonEmptyObject(cookieValue))){// if useServerSideCookies load option is set to true
|
|
3337
3347
|
// set the cookie from server side
|
|
3338
|
-
if(state.serverCookies.isEnabledServerSideCookies.value&&storageType===COOKIE_STORAGE){
|
|
3348
|
+
if(state.serverCookies.isEnabledServerSideCookies.value&&storageType===COOKIE_STORAGE){// Mark the requests as in progress.
|
|
3349
|
+
this.serverSideCookiesRequestInProgress[sessionKey]=true;if(this.serverSideCookieDebounceFuncs[sessionKey]){globalThis.clearTimeout(this.serverSideCookieDebounceFuncs[sessionKey]);}this.serverSideCookieDebounceFuncs[sessionKey]=globalThis.setTimeout(()=>{// Create a map of session key to cookie name
|
|
3350
|
+
const sessionToCookiesMap={[sessionKey]:{name:cookieName}};this.setServerSideCookies(sessionToCookiesMap,(cookieName,cookieValue)=>{curStore?.set(cookieName,cookieValue);},curStore);},SERVER_SIDE_COOKIES_DEBOUNCE_TIME);}else {curStore?.set(cookieName,cookieValue);}}else {curStore?.remove(cookieName);}}}/**
|
|
3339
3351
|
* Function to update storage whenever state value changes
|
|
3340
3352
|
*/registerEffects(){// This will work as long as the user session entry key names are same as the state keys
|
|
3341
|
-
USER_SESSION_KEYS.forEach(sessionKey=>{E(()=>{this.syncValueToStorage(sessionKey
|
|
3353
|
+
USER_SESSION_KEYS.forEach(sessionKey=>{E(()=>{this.syncValueToStorage(sessionKey);});});}/**
|
|
3342
3354
|
* Sets anonymous id in the following precedence:
|
|
3343
3355
|
*
|
|
3344
3356
|
* 1. anonymousId: Id directly provided to the function.
|
|
@@ -3355,30 +3367,37 @@
|
|
|
3355
3367
|
// This is needed for entries that are fetched from the storage
|
|
3356
3368
|
// during the current session (for example, session info)
|
|
3357
3369
|
this.migrateStorageIfNeeded([store],[sessionKey]);const storageKey=entries[sessionKey]?.key;return store?.get(storageKey)??null;}return null;}getExternalAnonymousIdByCookieName(key){const storageEngine=getStorageEngine(COOKIE_STORAGE);if(storageEngine?.isEnabled){return storageEngine.getItem(key)??null;}return null;}/**
|
|
3370
|
+
* Fetches the value for a session key. Preferably from storage, if the server-side
|
|
3371
|
+
* cookies request is not in progress. Otherwise, from the state.
|
|
3372
|
+
* @param sessionKey - The session key to fetch the value for
|
|
3373
|
+
* @returns - The value for the session key
|
|
3374
|
+
*/getUserSessionValue(sessionKey){// If the server-side cookies request is in progress, fetch the value from the state.
|
|
3375
|
+
if(this.serverSideCookiesRequestInProgress[sessionKey]){return state.session[sessionKey].value;}// Otherwise, fetch the value from storage.
|
|
3376
|
+
return this.getEntryValue(sessionKey);}/**
|
|
3358
3377
|
* Fetches User Id
|
|
3359
3378
|
* @returns
|
|
3360
|
-
*/getUserId(){return this.
|
|
3379
|
+
*/getUserId(){return this.getUserSessionValue('userId');}/**
|
|
3361
3380
|
* Fetches User Traits
|
|
3362
3381
|
* @returns
|
|
3363
|
-
*/getUserTraits(){return this.
|
|
3382
|
+
*/getUserTraits(){return this.getUserSessionValue('userTraits');}/**
|
|
3364
3383
|
* Fetches Group Id
|
|
3365
3384
|
* @returns
|
|
3366
|
-
*/getGroupId(){return this.
|
|
3385
|
+
*/getGroupId(){return this.getUserSessionValue('groupId');}/**
|
|
3367
3386
|
* Fetches Group Traits
|
|
3368
3387
|
* @returns
|
|
3369
|
-
*/getGroupTraits(){return this.
|
|
3388
|
+
*/getGroupTraits(){return this.getUserSessionValue('groupTraits');}/**
|
|
3370
3389
|
* Fetches Initial Referrer
|
|
3371
3390
|
* @returns
|
|
3372
|
-
*/getInitialReferrer(){return this.
|
|
3391
|
+
*/getInitialReferrer(){return this.getUserSessionValue('initialReferrer');}/**
|
|
3373
3392
|
* Fetches Initial Referring domain
|
|
3374
3393
|
* @returns
|
|
3375
|
-
*/getInitialReferringDomain(){return this.
|
|
3394
|
+
*/getInitialReferringDomain(){return this.getUserSessionValue('initialReferringDomain');}/**
|
|
3376
3395
|
* Fetches session tracking information from storage
|
|
3377
3396
|
* @returns
|
|
3378
|
-
*/getSessionInfo(){return this.
|
|
3397
|
+
*/getSessionInfo(){return this.getUserSessionValue('sessionInfo');}/**
|
|
3379
3398
|
* Fetches auth token from storage
|
|
3380
3399
|
* @returns
|
|
3381
|
-
*/getAuthToken(){return this.
|
|
3400
|
+
*/getAuthToken(){return this.getUserSessionValue('authToken');}/**
|
|
3382
3401
|
* If session is active it returns the sessionId
|
|
3383
3402
|
* @returns
|
|
3384
3403
|
*/getSessionId(){const sessionInfo=this.getSessionInfo()??DEFAULT_USER_SESSION_VALUES.sessionInfo;if(sessionInfo.autoTrack&&!hasSessionExpired(sessionInfo)||sessionInfo.manualTrack){return sessionInfo.id??null;}return null;}/**
|
|
@@ -3394,7 +3413,7 @@
|
|
|
3394
3413
|
if(sessionInfo.sessionStart===undefined){sessionInfo={...sessionInfo,sessionStart:true};}else if(sessionInfo.sessionStart){sessionInfo={...sessionInfo,sessionStart:false};}}// Always write to state (in-turn to storage) to keep the session info up to date.
|
|
3395
3414
|
state.session.sessionInfo.value=sessionInfo;if(state.lifecycle.status.value!=='readyExecuted'){// Force update the storage as the 'effect' blocks are not getting triggered
|
|
3396
3415
|
// when processing preload buffered requests
|
|
3397
|
-
this.syncValueToStorage('sessionInfo'
|
|
3416
|
+
this.syncValueToStorage('sessionInfo');}}resetAndStartNewSession(){const session=state.session;const{manualTrack,autoTrack,timeout,cutOff}=session.sessionInfo.value;if(autoTrack){const sessionInfo={...DEFAULT_USER_SESSION_VALUES.sessionInfo,timeout};if(cutOff){sessionInfo.cutOff={enabled:cutOff.enabled,duration:cutOff.duration};}session.sessionInfo.value=sessionInfo;this.startOrRenewAutoTracking(session.sessionInfo.value);}else if(manualTrack){this.startManualTrackingInternal();}}/**
|
|
3398
3417
|
* Reset state values
|
|
3399
3418
|
* @param options options for reset
|
|
3400
3419
|
* @returns
|
|
@@ -510,7 +510,7 @@ error.stack=`${stack}\n${MANUAL_ERROR_IDENTIFIER}`;break;case stacktrace:// esli
|
|
|
510
510
|
error.stacktrace=`${stacktrace}\n${MANUAL_ERROR_IDENTIFIER}`;break;case operaSourceloc:default:// eslint-disable-next-line no-param-reassign
|
|
511
511
|
error['opera#sourceloc']=`${operaSourceloc}\n${MANUAL_ERROR_IDENTIFIER}`;break;}}}globalThis.dispatchEvent(new ErrorEvent('error',{error,bubbles:true,cancelable:true,composed:true}));};
|
|
512
512
|
|
|
513
|
-
const APP_NAME='RudderLabs JavaScript SDK';const APP_VERSION='3.
|
|
513
|
+
const APP_NAME='RudderLabs JavaScript SDK';const APP_VERSION='3.27.0';const APP_NAMESPACE='com.rudderlabs.javascript';const MODULE_TYPE='npm';const ADBLOCK_PAGE_CATEGORY='RudderJS-Initiated';const ADBLOCK_PAGE_NAME='ad-block page request';const ADBLOCK_PAGE_PATH='/ad-blocked';const GLOBAL_PRELOAD_BUFFER='preloadedEventsBuffer';const CONSENT_TRACK_EVENT_NAME='Consent Management Interaction';
|
|
514
514
|
|
|
515
515
|
const QUERY_PARAM_TRAIT_PREFIX='ajs_trait_';const QUERY_PARAM_PROPERTY_PREFIX='ajs_prop_';const QUERY_PARAM_ANONYMOUS_ID_KEY='ajs_aid';const QUERY_PARAM_USER_ID_KEY='ajs_uid';const QUERY_PARAM_TRACK_EVENT_NAME_KEY='ajs_event';
|
|
516
516
|
|
|
@@ -538,8 +538,9 @@ data[dataKey]=params.get(key);}});return data;};/**
|
|
|
538
538
|
* Parse query string into preload buffer events & push into existing array before any other events
|
|
539
539
|
*/const retrieveEventsFromQueryString=(argumentsArray=[])=>{// Mapping for trait and properties values based on key prefix
|
|
540
540
|
const eventArgumentToQueryParamMap={trait:QUERY_PARAM_TRAIT_PREFIX,properties:QUERY_PARAM_PROPERTY_PREFIX};const queryObject=new URLSearchParams(globalThis.location.search);// Add track events with name and properties
|
|
541
|
-
if(queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY)){argumentsArray.unshift(['track',queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY),getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.properties)]);}//
|
|
542
|
-
|
|
541
|
+
if(queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY)){argumentsArray.unshift(['track',queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY),getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.properties)]);}// Send identify event
|
|
542
|
+
const userId=queryObject.get(QUERY_PARAM_USER_ID_KEY);const userTraits=getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.trait);if(userId||isNonEmptyObject(userTraits)){// In identify API, user ID is optional
|
|
543
|
+
const identifyApiArgs=[...(userId?[userId]:[]),userTraits];argumentsArray.unshift(['identify',...identifyApiArgs]);}// Set anonymousID
|
|
543
544
|
if(queryObject.get(QUERY_PARAM_ANONYMOUS_ID_KEY)){argumentsArray.unshift(['setAnonymousId',queryObject.get(QUERY_PARAM_ANONYMOUS_ID_KEY)]);}};/**
|
|
544
545
|
* Retrieve an existing buffered load method call and remove from the existing array
|
|
545
546
|
*/const getPreloadedLoadEvent=preloadedEventsArray=>{const loadMethodName='load';let loadEvent=[];/**
|
|
@@ -821,16 +822,14 @@ resolve(true);}}else {resolve(!ERROR_MESSAGES_TO_BE_FILTERED.some(e=>e.test(errM
|
|
|
821
822
|
// Ex: parentFolderName will be 'sample' for url: https://example.com/sample/file.min.js
|
|
822
823
|
const parentFolderName=paths[paths.length-2];return parentFolderName===CDN_INT_DIR||SDK_FILE_NAME_PREFIXES().some(prefix=>srcFileName.startsWith(prefix)&&srcFileName.endsWith('.js'));};const getErrorDeliveryPayload=(payload,state,category)=>{const data={version:METRICS_PAYLOAD_VERSION,message_id:generateUUID(),source:{name:SOURCE_NAME,sdk_version:state.context.app.value.version,write_key:state.lifecycle.writeKey.value,install_type:state.context.app.value.installType,category:category??DEFAULT_ERROR_CATEGORY},errors:payload};return stringifyWithoutCircular(data);};/**
|
|
823
824
|
* A function to get the grouping hash value to be used for the error event.
|
|
824
|
-
* Grouping hash is suppressed for non-cdn installs.
|
|
825
825
|
* If the grouping hash is an error instance, the normalized error message is used as the grouping hash.
|
|
826
826
|
* If the grouping hash is an empty string or not specified, the default grouping hash is used.
|
|
827
827
|
* If the grouping hash is a string, it is used as is.
|
|
828
828
|
* @param curErrGroupingHash The grouping hash value part of the error event
|
|
829
829
|
* @param defaultGroupingHash The default grouping hash value. It is the error message.
|
|
830
|
-
* @param state The application state
|
|
831
830
|
* @param logger The logger instance
|
|
832
831
|
* @returns The final grouping hash value to be used for the error event
|
|
833
|
-
*/const getErrorGroupingHash=(curErrGroupingHash,defaultGroupingHash,
|
|
832
|
+
*/const getErrorGroupingHash=(curErrGroupingHash,defaultGroupingHash,logger)=>{let normalizedGroupingHash;if(!isDefined(curErrGroupingHash)){normalizedGroupingHash=defaultGroupingHash;}else if(isString(curErrGroupingHash)){normalizedGroupingHash=curErrGroupingHash;}else {const normalizedErrorInstance=normalizeError(curErrGroupingHash,logger);if(isDefined(normalizedErrorInstance)){normalizedGroupingHash=normalizedErrorInstance.message;}else {normalizedGroupingHash=defaultGroupingHash;}}return normalizedGroupingHash;};
|
|
834
833
|
|
|
835
834
|
/**
|
|
836
835
|
* A service to handle errors
|
|
@@ -854,13 +853,13 @@ document.addEventListener('securitypolicyviolation',event=>{const blockedURL=isS
|
|
|
854
853
|
*/async onError(errorInfo){try{const{error,context,customMessage,groupingHash,category}=errorInfo;const errorType=errorInfo.errorType??ErrorType.HANDLEDEXCEPTION;const errInstance=getErrInstance(error,errorType);const normalizedError=normalizeError(errInstance,this.logger);if(isUndefined(normalizedError)){return;}const customMsgVal=customMessage?`${customMessage} - `:'';const errorMsgPrefix=`${context}${LOG_CONTEXT_SEPARATOR}${customMsgVal}`;const bsException=createBugsnagException(normalizedError,errorMsgPrefix);const stacktrace=getStacktrace(normalizedError);const isSdkDispatched=stacktrace.includes(MANUAL_ERROR_IDENTIFIER);// Filter errors that are not originated in the SDK.
|
|
855
854
|
// In case of NPM installations, the unhandled errors from the SDK cannot be identified
|
|
856
855
|
// and will NOT be reported unless they occur in plugins or integrations.
|
|
857
|
-
if(!isSdkDispatched&&!isSDKError(bsException)&&errorType!==ErrorType.HANDLEDEXCEPTION){return;}if(state.reporting.isErrorReportingEnabled.value){const isAllowed=await checkIfAllowedToBeNotified(bsException,state,this.httpClient);if(isAllowed){const errorState={severity:'error',unhandled:errorType!==ErrorType.HANDLEDEXCEPTION,severityReason:{type:errorType}};//
|
|
858
|
-
//
|
|
859
|
-
//
|
|
856
|
+
if(!isSdkDispatched&&!isSDKError(bsException)&&errorType!==ErrorType.HANDLEDEXCEPTION){return;}if(state.reporting.isErrorReportingEnabled.value){const isAllowed=await checkIfAllowedToBeNotified(bsException,state,this.httpClient);if(isAllowed){const errorState={severity:'error',unhandled:errorType!==ErrorType.HANDLEDEXCEPTION,severityReason:{type:errorType}};// This will allow custom grouping of errors.
|
|
857
|
+
// In case of NPM installations, the default grouping by surrounding code
|
|
858
|
+
// does not make sense as each user application is different and will create a lot of noise in the alerts.
|
|
860
859
|
// References:
|
|
861
860
|
// https://docs.bugsnag.com/platforms/javascript/customizing-error-reports/#groupinghash
|
|
862
861
|
// https://docs.bugsnag.com/product/error-grouping/#user_defined
|
|
863
|
-
const normalizedGroupingHash=getErrorGroupingHash(groupingHash,bsException.message,
|
|
862
|
+
const normalizedGroupingHash=getErrorGroupingHash(groupingHash,bsException.message,this.logger);// Get the final payload to be sent to the metrics service
|
|
864
863
|
const bugsnagPayload=getBugsnagErrorEvent(bsException,errorState,state,normalizedGroupingHash);// send it to metrics service
|
|
865
864
|
this.httpClient.getAsyncData({url:state.metrics.metricsServiceUrl.value,options:{method:'POST',data:getErrorDeliveryPayload(bugsnagPayload,state,category),sendRawData:true},isRawResponse:true});}}// Log handled errors and errors dispatched by the SDK
|
|
866
865
|
if(errorType===ErrorType.HANDLEDEXCEPTION||isSdkDispatched){this.logger.error(bsException.message);}}catch(err){// If an error occurs while handling an error, log it
|
|
@@ -1585,7 +1584,9 @@ enrichedEvent.userId=to??enrichedEvent.userId;return enrichedEvent;}/**
|
|
|
1585
1584
|
* @param event Incoming event data
|
|
1586
1585
|
*/addEvent(event){this.userSessionManager.refreshSession();const rudderEvent=this.eventFactory.create(event);this.eventRepository.enqueue(rudderEvent,event.callback);}}
|
|
1587
1586
|
|
|
1588
|
-
class UserSessionManager{
|
|
1587
|
+
class UserSessionManager{/**
|
|
1588
|
+
* Tracks whether a server-side cookie setting request is in progress or not.
|
|
1589
|
+
*/constructor(pluginsManager,storeManager,httpClient,errorHandler,logger){this.storeManager=storeManager;this.pluginsManager=pluginsManager;this.logger=logger;this.errorHandler=errorHandler;this.httpClient=httpClient;this.onError=this.onError.bind(this);this.serverSideCookieDebounceFuncs={};this.serverSideCookiesRequestInProgress={};}/**
|
|
1589
1590
|
* Initialize User session with values from storage
|
|
1590
1591
|
*/init(){this.syncStorageDataToState();// Register the effect to sync with storage
|
|
1591
1592
|
this.registerEffects();}syncStorageDataToState(){this.migrateStorageIfNeeded();this.migrateDataFromPreviousStorage();// get the values from storage and set it again
|
|
@@ -1615,20 +1616,31 @@ cutOffDuration=DEFAULT_SESSION_CUT_OFF_DURATION_MS;}else if(cutOffDuration<sessi
|
|
|
1615
1616
|
* @param callback
|
|
1616
1617
|
*/makeRequestToSetCookie(encryptedCookieData,callback){this.httpClient?.getAsyncData({url:state.serverCookies.dataServiceUrl.value,options:{method:'POST',data:stringifyWithoutCircular({reqType:'setCookies',workspaceId:state.source.value?.workspaceId,data:{options:{maxAge:state.storage.cookie.value?.maxage,path:state.storage.cookie.value?.path,domain:state.storage.cookie.value?.domain,sameSite:state.storage.cookie.value?.samesite,secure:state.storage.cookie.value?.secure,expires:state.storage.cookie.value?.expires},cookies:encryptedCookieData}}),sendRawData:true,withCredentials:true},isRawResponse:true,callback});}/**
|
|
1617
1618
|
* A function to make an external request to set the cookie from server side
|
|
1618
|
-
* @param key
|
|
1619
|
-
* @param
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1619
|
+
* @param sessionToCookiesMap map of session key to cookie name
|
|
1620
|
+
* @param cb callback function to be called when the cookie is set
|
|
1621
|
+
* @param store store to be used to get the cookie value
|
|
1622
|
+
*/setServerSideCookies(sessionToCookiesMap,cb,store){// Retrieve the cookie value from the state
|
|
1623
|
+
const sessionKeys=Object.keys(sessionToCookiesMap);const getCurrentCookieValuesFromState=()=>{return sessionKeys.map(sessionKey=>{return {name:sessionToCookiesMap[sessionKey].name,value:state.session[sessionKey].value};});};// Preserve the current cookie values
|
|
1624
|
+
const originalCookieValues={};sessionKeys.forEach(sessionKey=>{originalCookieValues[sessionToCookiesMap[sessionKey].name]=store?.get(sessionToCookiesMap[sessionKey].name);});const clearInProgressFlags=()=>{sessionKeys.forEach(sessionKey=>{this.serverSideCookiesRequestInProgress[sessionKey]=false;});};const setCookiesClientSide=()=>{getCurrentCookieValuesFromState().forEach(each=>{if(cb){cb(each.name,each.value);}});};try{const expectedCookieValues={};sessionKeys.forEach(sessionKey=>{expectedCookieValues[sessionToCookiesMap[sessionKey].name]=state.session[sessionKey].value;});// encrypt cookies values
|
|
1625
|
+
const encryptedCookieData=this.getEncryptedCookieData(getCurrentCookieValuesFromState(),store);if(encryptedCookieData.length>0){// make request to data service to set the cookie from server side
|
|
1626
|
+
this.makeRequestToSetCookie(encryptedCookieData,(res,details)=>{// Mark the cookie req status as done
|
|
1627
|
+
clearInProgressFlags();if(details?.xhr?.status===200){getCurrentCookieValuesFromState().forEach(cData=>{const originalCookieVal=originalCookieValues[cData.name];const currentCookieVal=store?.get(cData.name);// Check if the expected cookie values are set.
|
|
1628
|
+
if(stringifyWithoutCircular(expectedCookieValues[cData.name],false,[])!==stringifyWithoutCircular(currentCookieVal,false,[])){// It's fine if the values don't match as other active SDK sessions might have updated the cookie values
|
|
1629
|
+
// or other cookie requests might have updated the cookie value.
|
|
1630
|
+
// Log an error only when cookie didn't exist previously and currently also doesn't exist.
|
|
1631
|
+
if(isNull(originalCookieVal)&&isNull(currentCookieVal)){this.logger.error(FAILED_SETTING_COOKIE_FROM_SERVER_ERROR(cData.name));}if(cb){cb(cData.name,cData.value);}}});}else {this.logger.error(DATA_SERVER_REQUEST_FAIL_ERROR(details?.xhr?.status));setCookiesClientSide();}});}else {setCookiesClientSide();// Mark the cookie req status as done
|
|
1632
|
+
clearInProgressFlags();}}catch(e){this.onError(e,FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR,FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR);setCookiesClientSide();// Mark the cookie req status as done
|
|
1633
|
+
clearInProgressFlags();}}/**
|
|
1623
1634
|
* A function to sync values in storage
|
|
1624
1635
|
* @param sessionKey
|
|
1625
|
-
|
|
1626
|
-
*/syncValueToStorage(sessionKey,value){const entries=state.storage.entries.value;const storageType=entries[sessionKey]?.type;if(isStorageTypeValidForStoringData(storageType)){const curStore=this.storeManager?.getStore(storageClientDataStoreNameMap[storageType]);const key=entries[sessionKey]?.key;if(value&&(isString(value)||isNonEmptyObject(value))){// if useServerSideCookies load option is set to true
|
|
1636
|
+
*/syncValueToStorage(sessionKey){const entries=state.storage.entries.value;const storageType=entries[sessionKey]?.type;if(isStorageTypeValidForStoringData(storageType)){const curStore=this.storeManager.getStore(storageClientDataStoreNameMap[storageType]);const cookieName=entries[sessionKey]?.key;const cookieValue=state.session[sessionKey].value;if(cookieValue&&(isString(cookieValue)||isNonEmptyObject(cookieValue))){// if useServerSideCookies load option is set to true
|
|
1627
1637
|
// set the cookie from server side
|
|
1628
|
-
if(state.serverCookies.isEnabledServerSideCookies.value&&storageType===COOKIE_STORAGE){
|
|
1638
|
+
if(state.serverCookies.isEnabledServerSideCookies.value&&storageType===COOKIE_STORAGE){// Mark the requests as in progress.
|
|
1639
|
+
this.serverSideCookiesRequestInProgress[sessionKey]=true;if(this.serverSideCookieDebounceFuncs[sessionKey]){globalThis.clearTimeout(this.serverSideCookieDebounceFuncs[sessionKey]);}this.serverSideCookieDebounceFuncs[sessionKey]=globalThis.setTimeout(()=>{// Create a map of session key to cookie name
|
|
1640
|
+
const sessionToCookiesMap={[sessionKey]:{name:cookieName}};this.setServerSideCookies(sessionToCookiesMap,(cookieName,cookieValue)=>{curStore?.set(cookieName,cookieValue);},curStore);},SERVER_SIDE_COOKIES_DEBOUNCE_TIME);}else {curStore?.set(cookieName,cookieValue);}}else {curStore?.remove(cookieName);}}}/**
|
|
1629
1641
|
* Function to update storage whenever state value changes
|
|
1630
1642
|
*/registerEffects(){// This will work as long as the user session entry key names are same as the state keys
|
|
1631
|
-
USER_SESSION_KEYS.forEach(sessionKey=>{E(()=>{this.syncValueToStorage(sessionKey
|
|
1643
|
+
USER_SESSION_KEYS.forEach(sessionKey=>{E(()=>{this.syncValueToStorage(sessionKey);});});}/**
|
|
1632
1644
|
* Sets anonymous id in the following precedence:
|
|
1633
1645
|
*
|
|
1634
1646
|
* 1. anonymousId: Id directly provided to the function.
|
|
@@ -1645,30 +1657,37 @@ const autoCapturedAnonymousId=this.pluginsManager?.invokeSingle('storage.getAnon
|
|
|
1645
1657
|
// This is needed for entries that are fetched from the storage
|
|
1646
1658
|
// during the current session (for example, session info)
|
|
1647
1659
|
this.migrateStorageIfNeeded([store],[sessionKey]);const storageKey=entries[sessionKey]?.key;return store?.get(storageKey)??null;}return null;}getExternalAnonymousIdByCookieName(key){const storageEngine=getStorageEngine(COOKIE_STORAGE);if(storageEngine?.isEnabled){return storageEngine.getItem(key)??null;}return null;}/**
|
|
1660
|
+
* Fetches the value for a session key. Preferably from storage, if the server-side
|
|
1661
|
+
* cookies request is not in progress. Otherwise, from the state.
|
|
1662
|
+
* @param sessionKey - The session key to fetch the value for
|
|
1663
|
+
* @returns - The value for the session key
|
|
1664
|
+
*/getUserSessionValue(sessionKey){// If the server-side cookies request is in progress, fetch the value from the state.
|
|
1665
|
+
if(this.serverSideCookiesRequestInProgress[sessionKey]){return state.session[sessionKey].value;}// Otherwise, fetch the value from storage.
|
|
1666
|
+
return this.getEntryValue(sessionKey);}/**
|
|
1648
1667
|
* Fetches User Id
|
|
1649
1668
|
* @returns
|
|
1650
|
-
*/getUserId(){return this.
|
|
1669
|
+
*/getUserId(){return this.getUserSessionValue('userId');}/**
|
|
1651
1670
|
* Fetches User Traits
|
|
1652
1671
|
* @returns
|
|
1653
|
-
*/getUserTraits(){return this.
|
|
1672
|
+
*/getUserTraits(){return this.getUserSessionValue('userTraits');}/**
|
|
1654
1673
|
* Fetches Group Id
|
|
1655
1674
|
* @returns
|
|
1656
|
-
*/getGroupId(){return this.
|
|
1675
|
+
*/getGroupId(){return this.getUserSessionValue('groupId');}/**
|
|
1657
1676
|
* Fetches Group Traits
|
|
1658
1677
|
* @returns
|
|
1659
|
-
*/getGroupTraits(){return this.
|
|
1678
|
+
*/getGroupTraits(){return this.getUserSessionValue('groupTraits');}/**
|
|
1660
1679
|
* Fetches Initial Referrer
|
|
1661
1680
|
* @returns
|
|
1662
|
-
*/getInitialReferrer(){return this.
|
|
1681
|
+
*/getInitialReferrer(){return this.getUserSessionValue('initialReferrer');}/**
|
|
1663
1682
|
* Fetches Initial Referring domain
|
|
1664
1683
|
* @returns
|
|
1665
|
-
*/getInitialReferringDomain(){return this.
|
|
1684
|
+
*/getInitialReferringDomain(){return this.getUserSessionValue('initialReferringDomain');}/**
|
|
1666
1685
|
* Fetches session tracking information from storage
|
|
1667
1686
|
* @returns
|
|
1668
|
-
*/getSessionInfo(){return this.
|
|
1687
|
+
*/getSessionInfo(){return this.getUserSessionValue('sessionInfo');}/**
|
|
1669
1688
|
* Fetches auth token from storage
|
|
1670
1689
|
* @returns
|
|
1671
|
-
*/getAuthToken(){return this.
|
|
1690
|
+
*/getAuthToken(){return this.getUserSessionValue('authToken');}/**
|
|
1672
1691
|
* If session is active it returns the sessionId
|
|
1673
1692
|
* @returns
|
|
1674
1693
|
*/getSessionId(){const sessionInfo=this.getSessionInfo()??DEFAULT_USER_SESSION_VALUES.sessionInfo;if(sessionInfo.autoTrack&&!hasSessionExpired(sessionInfo)||sessionInfo.manualTrack){return sessionInfo.id??null;}return null;}/**
|
|
@@ -1684,7 +1703,7 @@ this.migrateStorageIfNeeded([store],[sessionKey]);const storageKey=entries[sessi
|
|
|
1684
1703
|
if(sessionInfo.sessionStart===undefined){sessionInfo={...sessionInfo,sessionStart:true};}else if(sessionInfo.sessionStart){sessionInfo={...sessionInfo,sessionStart:false};}}// Always write to state (in-turn to storage) to keep the session info up to date.
|
|
1685
1704
|
state.session.sessionInfo.value=sessionInfo;if(state.lifecycle.status.value!=='readyExecuted'){// Force update the storage as the 'effect' blocks are not getting triggered
|
|
1686
1705
|
// when processing preload buffered requests
|
|
1687
|
-
this.syncValueToStorage('sessionInfo'
|
|
1706
|
+
this.syncValueToStorage('sessionInfo');}}resetAndStartNewSession(){const session=state.session;const{manualTrack,autoTrack,timeout,cutOff}=session.sessionInfo.value;if(autoTrack){const sessionInfo={...DEFAULT_USER_SESSION_VALUES.sessionInfo,timeout};if(cutOff){sessionInfo.cutOff={enabled:cutOff.enabled,duration:cutOff.duration};}session.sessionInfo.value=sessionInfo;this.startOrRenewAutoTracking(session.sessionInfo.value);}else if(manualTrack){this.startManualTrackingInternal();}}/**
|
|
1688
1707
|
* Reset state values
|
|
1689
1708
|
* @param options options for reset
|
|
1690
1709
|
* @returns
|
|
@@ -519,7 +519,7 @@ error.stack=`${stack}\n${MANUAL_ERROR_IDENTIFIER}`;break;case stacktrace:// esli
|
|
|
519
519
|
error.stacktrace=`${stacktrace}\n${MANUAL_ERROR_IDENTIFIER}`;break;case operaSourceloc:default:// eslint-disable-next-line no-param-reassign
|
|
520
520
|
error['opera#sourceloc']=`${operaSourceloc}\n${MANUAL_ERROR_IDENTIFIER}`;break;}}}globalThis.dispatchEvent(new ErrorEvent('error',{error,bubbles:true,cancelable:true,composed:true}));};
|
|
521
521
|
|
|
522
|
-
const APP_NAME='RudderLabs JavaScript SDK';const APP_VERSION='3.
|
|
522
|
+
const APP_NAME='RudderLabs JavaScript SDK';const APP_VERSION='3.27.0';const APP_NAMESPACE='com.rudderlabs.javascript';const MODULE_TYPE='npm';const ADBLOCK_PAGE_CATEGORY='RudderJS-Initiated';const ADBLOCK_PAGE_NAME='ad-block page request';const ADBLOCK_PAGE_PATH='/ad-blocked';const GLOBAL_PRELOAD_BUFFER='preloadedEventsBuffer';const CONSENT_TRACK_EVENT_NAME='Consent Management Interaction';
|
|
523
523
|
|
|
524
524
|
const QUERY_PARAM_TRAIT_PREFIX='ajs_trait_';const QUERY_PARAM_PROPERTY_PREFIX='ajs_prop_';const QUERY_PARAM_ANONYMOUS_ID_KEY='ajs_aid';const QUERY_PARAM_USER_ID_KEY='ajs_uid';const QUERY_PARAM_TRACK_EVENT_NAME_KEY='ajs_event';
|
|
525
525
|
|
|
@@ -547,8 +547,9 @@ data[dataKey]=params.get(key);}});return data;};/**
|
|
|
547
547
|
* Parse query string into preload buffer events & push into existing array before any other events
|
|
548
548
|
*/const retrieveEventsFromQueryString=(argumentsArray=[])=>{// Mapping for trait and properties values based on key prefix
|
|
549
549
|
const eventArgumentToQueryParamMap={trait:QUERY_PARAM_TRAIT_PREFIX,properties:QUERY_PARAM_PROPERTY_PREFIX};const queryObject=new URLSearchParams(globalThis.location.search);// Add track events with name and properties
|
|
550
|
-
if(queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY)){argumentsArray.unshift(['track',queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY),getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.properties)]);}//
|
|
551
|
-
|
|
550
|
+
if(queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY)){argumentsArray.unshift(['track',queryObject.get(QUERY_PARAM_TRACK_EVENT_NAME_KEY),getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.properties)]);}// Send identify event
|
|
551
|
+
const userId=queryObject.get(QUERY_PARAM_USER_ID_KEY);const userTraits=getEventDataFromQueryString(queryObject,eventArgumentToQueryParamMap.trait);if(userId||isNonEmptyObject(userTraits)){// In identify API, user ID is optional
|
|
552
|
+
const identifyApiArgs=[...(userId?[userId]:[]),userTraits];argumentsArray.unshift(['identify',...identifyApiArgs]);}// Set anonymousID
|
|
552
553
|
if(queryObject.get(QUERY_PARAM_ANONYMOUS_ID_KEY)){argumentsArray.unshift(['setAnonymousId',queryObject.get(QUERY_PARAM_ANONYMOUS_ID_KEY)]);}};/**
|
|
553
554
|
* Retrieve an existing buffered load method call and remove from the existing array
|
|
554
555
|
*/const getPreloadedLoadEvent=preloadedEventsArray=>{const loadMethodName='load';let loadEvent=[];/**
|
|
@@ -830,16 +831,14 @@ resolve(true);}}else {resolve(!ERROR_MESSAGES_TO_BE_FILTERED.some(e=>e.test(errM
|
|
|
830
831
|
// Ex: parentFolderName will be 'sample' for url: https://example.com/sample/file.min.js
|
|
831
832
|
const parentFolderName=paths[paths.length-2];return parentFolderName===CDN_INT_DIR||SDK_FILE_NAME_PREFIXES().some(prefix=>srcFileName.startsWith(prefix)&&srcFileName.endsWith('.js'));};const getErrorDeliveryPayload=(payload,state,category)=>{const data={version:METRICS_PAYLOAD_VERSION,message_id:generateUUID(),source:{name:SOURCE_NAME,sdk_version:state.context.app.value.version,write_key:state.lifecycle.writeKey.value,install_type:state.context.app.value.installType,category:category??DEFAULT_ERROR_CATEGORY},errors:payload};return stringifyWithoutCircular(data);};/**
|
|
832
833
|
* A function to get the grouping hash value to be used for the error event.
|
|
833
|
-
* Grouping hash is suppressed for non-cdn installs.
|
|
834
834
|
* If the grouping hash is an error instance, the normalized error message is used as the grouping hash.
|
|
835
835
|
* If the grouping hash is an empty string or not specified, the default grouping hash is used.
|
|
836
836
|
* If the grouping hash is a string, it is used as is.
|
|
837
837
|
* @param curErrGroupingHash The grouping hash value part of the error event
|
|
838
838
|
* @param defaultGroupingHash The default grouping hash value. It is the error message.
|
|
839
|
-
* @param state The application state
|
|
840
839
|
* @param logger The logger instance
|
|
841
840
|
* @returns The final grouping hash value to be used for the error event
|
|
842
|
-
*/const getErrorGroupingHash=(curErrGroupingHash,defaultGroupingHash,
|
|
841
|
+
*/const getErrorGroupingHash=(curErrGroupingHash,defaultGroupingHash,logger)=>{let normalizedGroupingHash;if(!isDefined(curErrGroupingHash)){normalizedGroupingHash=defaultGroupingHash;}else if(isString(curErrGroupingHash)){normalizedGroupingHash=curErrGroupingHash;}else {const normalizedErrorInstance=normalizeError(curErrGroupingHash,logger);if(isDefined(normalizedErrorInstance)){normalizedGroupingHash=normalizedErrorInstance.message;}else {normalizedGroupingHash=defaultGroupingHash;}}return normalizedGroupingHash;};
|
|
843
842
|
|
|
844
843
|
/**
|
|
845
844
|
* A service to handle errors
|
|
@@ -863,13 +862,13 @@ document.addEventListener('securitypolicyviolation',event=>{const blockedURL=isS
|
|
|
863
862
|
*/async onError(errorInfo){try{const{error,context,customMessage,groupingHash,category}=errorInfo;const errorType=errorInfo.errorType??ErrorType.HANDLEDEXCEPTION;const errInstance=getErrInstance(error,errorType);const normalizedError=normalizeError(errInstance,this.logger);if(isUndefined(normalizedError)){return;}const customMsgVal=customMessage?`${customMessage} - `:'';const errorMsgPrefix=`${context}${LOG_CONTEXT_SEPARATOR}${customMsgVal}`;const bsException=createBugsnagException(normalizedError,errorMsgPrefix);const stacktrace=getStacktrace(normalizedError);const isSdkDispatched=stacktrace.includes(MANUAL_ERROR_IDENTIFIER);// Filter errors that are not originated in the SDK.
|
|
864
863
|
// In case of NPM installations, the unhandled errors from the SDK cannot be identified
|
|
865
864
|
// and will NOT be reported unless they occur in plugins or integrations.
|
|
866
|
-
if(!isSdkDispatched&&!isSDKError(bsException)&&errorType!==ErrorType.HANDLEDEXCEPTION){return;}if(state.reporting.isErrorReportingEnabled.value){const isAllowed=await checkIfAllowedToBeNotified(bsException,state,this.httpClient);if(isAllowed){const errorState={severity:'error',unhandled:errorType!==ErrorType.HANDLEDEXCEPTION,severityReason:{type:errorType}};//
|
|
867
|
-
//
|
|
868
|
-
//
|
|
865
|
+
if(!isSdkDispatched&&!isSDKError(bsException)&&errorType!==ErrorType.HANDLEDEXCEPTION){return;}if(state.reporting.isErrorReportingEnabled.value){const isAllowed=await checkIfAllowedToBeNotified(bsException,state,this.httpClient);if(isAllowed){const errorState={severity:'error',unhandled:errorType!==ErrorType.HANDLEDEXCEPTION,severityReason:{type:errorType}};// This will allow custom grouping of errors.
|
|
866
|
+
// In case of NPM installations, the default grouping by surrounding code
|
|
867
|
+
// does not make sense as each user application is different and will create a lot of noise in the alerts.
|
|
869
868
|
// References:
|
|
870
869
|
// https://docs.bugsnag.com/platforms/javascript/customizing-error-reports/#groupinghash
|
|
871
870
|
// https://docs.bugsnag.com/product/error-grouping/#user_defined
|
|
872
|
-
const normalizedGroupingHash=getErrorGroupingHash(groupingHash,bsException.message,
|
|
871
|
+
const normalizedGroupingHash=getErrorGroupingHash(groupingHash,bsException.message,this.logger);// Get the final payload to be sent to the metrics service
|
|
873
872
|
const bugsnagPayload=getBugsnagErrorEvent(bsException,errorState,state,normalizedGroupingHash);// send it to metrics service
|
|
874
873
|
this.httpClient.getAsyncData({url:state.metrics.metricsServiceUrl.value,options:{method:'POST',data:getErrorDeliveryPayload(bugsnagPayload,state,category),sendRawData:true},isRawResponse:true});}}// Log handled errors and errors dispatched by the SDK
|
|
875
874
|
if(errorType===ErrorType.HANDLEDEXCEPTION||isSdkDispatched){this.logger.error(bsException.message);}}catch(err){// If an error occurs while handling an error, log it
|
|
@@ -3293,7 +3292,9 @@ enrichedEvent.userId=to??enrichedEvent.userId;return enrichedEvent;}/**
|
|
|
3293
3292
|
* @param event Incoming event data
|
|
3294
3293
|
*/addEvent(event){this.userSessionManager.refreshSession();const rudderEvent=this.eventFactory.create(event);this.eventRepository.enqueue(rudderEvent,event.callback);}}
|
|
3295
3294
|
|
|
3296
|
-
class UserSessionManager{
|
|
3295
|
+
class UserSessionManager{/**
|
|
3296
|
+
* Tracks whether a server-side cookie setting request is in progress or not.
|
|
3297
|
+
*/constructor(pluginsManager,storeManager,httpClient,errorHandler,logger){this.storeManager=storeManager;this.pluginsManager=pluginsManager;this.logger=logger;this.errorHandler=errorHandler;this.httpClient=httpClient;this.onError=this.onError.bind(this);this.serverSideCookieDebounceFuncs={};this.serverSideCookiesRequestInProgress={};}/**
|
|
3297
3298
|
* Initialize User session with values from storage
|
|
3298
3299
|
*/init(){this.syncStorageDataToState();// Register the effect to sync with storage
|
|
3299
3300
|
this.registerEffects();}syncStorageDataToState(){this.migrateStorageIfNeeded();this.migrateDataFromPreviousStorage();// get the values from storage and set it again
|
|
@@ -3323,20 +3324,31 @@ cutOffDuration=DEFAULT_SESSION_CUT_OFF_DURATION_MS;}else if(cutOffDuration<sessi
|
|
|
3323
3324
|
* @param callback
|
|
3324
3325
|
*/makeRequestToSetCookie(encryptedCookieData,callback){this.httpClient?.getAsyncData({url:state.serverCookies.dataServiceUrl.value,options:{method:'POST',data:stringifyWithoutCircular({reqType:'setCookies',workspaceId:state.source.value?.workspaceId,data:{options:{maxAge:state.storage.cookie.value?.maxage,path:state.storage.cookie.value?.path,domain:state.storage.cookie.value?.domain,sameSite:state.storage.cookie.value?.samesite,secure:state.storage.cookie.value?.secure,expires:state.storage.cookie.value?.expires},cookies:encryptedCookieData}}),sendRawData:true,withCredentials:true},isRawResponse:true,callback});}/**
|
|
3325
3326
|
* A function to make an external request to set the cookie from server side
|
|
3326
|
-
* @param key
|
|
3327
|
-
* @param
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
3327
|
+
* @param sessionToCookiesMap map of session key to cookie name
|
|
3328
|
+
* @param cb callback function to be called when the cookie is set
|
|
3329
|
+
* @param store store to be used to get the cookie value
|
|
3330
|
+
*/setServerSideCookies(sessionToCookiesMap,cb,store){// Retrieve the cookie value from the state
|
|
3331
|
+
const sessionKeys=Object.keys(sessionToCookiesMap);const getCurrentCookieValuesFromState=()=>{return sessionKeys.map(sessionKey=>{return {name:sessionToCookiesMap[sessionKey].name,value:state.session[sessionKey].value};});};// Preserve the current cookie values
|
|
3332
|
+
const originalCookieValues={};sessionKeys.forEach(sessionKey=>{originalCookieValues[sessionToCookiesMap[sessionKey].name]=store?.get(sessionToCookiesMap[sessionKey].name);});const clearInProgressFlags=()=>{sessionKeys.forEach(sessionKey=>{this.serverSideCookiesRequestInProgress[sessionKey]=false;});};const setCookiesClientSide=()=>{getCurrentCookieValuesFromState().forEach(each=>{if(cb){cb(each.name,each.value);}});};try{const expectedCookieValues={};sessionKeys.forEach(sessionKey=>{expectedCookieValues[sessionToCookiesMap[sessionKey].name]=state.session[sessionKey].value;});// encrypt cookies values
|
|
3333
|
+
const encryptedCookieData=this.getEncryptedCookieData(getCurrentCookieValuesFromState(),store);if(encryptedCookieData.length>0){// make request to data service to set the cookie from server side
|
|
3334
|
+
this.makeRequestToSetCookie(encryptedCookieData,(res,details)=>{// Mark the cookie req status as done
|
|
3335
|
+
clearInProgressFlags();if(details?.xhr?.status===200){getCurrentCookieValuesFromState().forEach(cData=>{const originalCookieVal=originalCookieValues[cData.name];const currentCookieVal=store?.get(cData.name);// Check if the expected cookie values are set.
|
|
3336
|
+
if(stringifyWithoutCircular(expectedCookieValues[cData.name],false,[])!==stringifyWithoutCircular(currentCookieVal,false,[])){// It's fine if the values don't match as other active SDK sessions might have updated the cookie values
|
|
3337
|
+
// or other cookie requests might have updated the cookie value.
|
|
3338
|
+
// Log an error only when cookie didn't exist previously and currently also doesn't exist.
|
|
3339
|
+
if(isNull(originalCookieVal)&&isNull(currentCookieVal)){this.logger.error(FAILED_SETTING_COOKIE_FROM_SERVER_ERROR(cData.name));}if(cb){cb(cData.name,cData.value);}}});}else {this.logger.error(DATA_SERVER_REQUEST_FAIL_ERROR(details?.xhr?.status));setCookiesClientSide();}});}else {setCookiesClientSide();// Mark the cookie req status as done
|
|
3340
|
+
clearInProgressFlags();}}catch(e){this.onError(e,FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR,FAILED_SETTING_COOKIE_FROM_SERVER_GLOBAL_ERROR);setCookiesClientSide();// Mark the cookie req status as done
|
|
3341
|
+
clearInProgressFlags();}}/**
|
|
3331
3342
|
* A function to sync values in storage
|
|
3332
3343
|
* @param sessionKey
|
|
3333
|
-
|
|
3334
|
-
*/syncValueToStorage(sessionKey,value){const entries=state.storage.entries.value;const storageType=entries[sessionKey]?.type;if(isStorageTypeValidForStoringData(storageType)){const curStore=this.storeManager?.getStore(storageClientDataStoreNameMap[storageType]);const key=entries[sessionKey]?.key;if(value&&(isString(value)||isNonEmptyObject(value))){// if useServerSideCookies load option is set to true
|
|
3344
|
+
*/syncValueToStorage(sessionKey){const entries=state.storage.entries.value;const storageType=entries[sessionKey]?.type;if(isStorageTypeValidForStoringData(storageType)){const curStore=this.storeManager.getStore(storageClientDataStoreNameMap[storageType]);const cookieName=entries[sessionKey]?.key;const cookieValue=state.session[sessionKey].value;if(cookieValue&&(isString(cookieValue)||isNonEmptyObject(cookieValue))){// if useServerSideCookies load option is set to true
|
|
3335
3345
|
// set the cookie from server side
|
|
3336
|
-
if(state.serverCookies.isEnabledServerSideCookies.value&&storageType===COOKIE_STORAGE){
|
|
3346
|
+
if(state.serverCookies.isEnabledServerSideCookies.value&&storageType===COOKIE_STORAGE){// Mark the requests as in progress.
|
|
3347
|
+
this.serverSideCookiesRequestInProgress[sessionKey]=true;if(this.serverSideCookieDebounceFuncs[sessionKey]){globalThis.clearTimeout(this.serverSideCookieDebounceFuncs[sessionKey]);}this.serverSideCookieDebounceFuncs[sessionKey]=globalThis.setTimeout(()=>{// Create a map of session key to cookie name
|
|
3348
|
+
const sessionToCookiesMap={[sessionKey]:{name:cookieName}};this.setServerSideCookies(sessionToCookiesMap,(cookieName,cookieValue)=>{curStore?.set(cookieName,cookieValue);},curStore);},SERVER_SIDE_COOKIES_DEBOUNCE_TIME);}else {curStore?.set(cookieName,cookieValue);}}else {curStore?.remove(cookieName);}}}/**
|
|
3337
3349
|
* Function to update storage whenever state value changes
|
|
3338
3350
|
*/registerEffects(){// This will work as long as the user session entry key names are same as the state keys
|
|
3339
|
-
USER_SESSION_KEYS.forEach(sessionKey=>{E(()=>{this.syncValueToStorage(sessionKey
|
|
3351
|
+
USER_SESSION_KEYS.forEach(sessionKey=>{E(()=>{this.syncValueToStorage(sessionKey);});});}/**
|
|
3340
3352
|
* Sets anonymous id in the following precedence:
|
|
3341
3353
|
*
|
|
3342
3354
|
* 1. anonymousId: Id directly provided to the function.
|
|
@@ -3353,30 +3365,37 @@ const autoCapturedAnonymousId=this.pluginsManager?.invokeSingle('storage.getAnon
|
|
|
3353
3365
|
// This is needed for entries that are fetched from the storage
|
|
3354
3366
|
// during the current session (for example, session info)
|
|
3355
3367
|
this.migrateStorageIfNeeded([store],[sessionKey]);const storageKey=entries[sessionKey]?.key;return store?.get(storageKey)??null;}return null;}getExternalAnonymousIdByCookieName(key){const storageEngine=getStorageEngine(COOKIE_STORAGE);if(storageEngine?.isEnabled){return storageEngine.getItem(key)??null;}return null;}/**
|
|
3368
|
+
* Fetches the value for a session key. Preferably from storage, if the server-side
|
|
3369
|
+
* cookies request is not in progress. Otherwise, from the state.
|
|
3370
|
+
* @param sessionKey - The session key to fetch the value for
|
|
3371
|
+
* @returns - The value for the session key
|
|
3372
|
+
*/getUserSessionValue(sessionKey){// If the server-side cookies request is in progress, fetch the value from the state.
|
|
3373
|
+
if(this.serverSideCookiesRequestInProgress[sessionKey]){return state.session[sessionKey].value;}// Otherwise, fetch the value from storage.
|
|
3374
|
+
return this.getEntryValue(sessionKey);}/**
|
|
3356
3375
|
* Fetches User Id
|
|
3357
3376
|
* @returns
|
|
3358
|
-
*/getUserId(){return this.
|
|
3377
|
+
*/getUserId(){return this.getUserSessionValue('userId');}/**
|
|
3359
3378
|
* Fetches User Traits
|
|
3360
3379
|
* @returns
|
|
3361
|
-
*/getUserTraits(){return this.
|
|
3380
|
+
*/getUserTraits(){return this.getUserSessionValue('userTraits');}/**
|
|
3362
3381
|
* Fetches Group Id
|
|
3363
3382
|
* @returns
|
|
3364
|
-
*/getGroupId(){return this.
|
|
3383
|
+
*/getGroupId(){return this.getUserSessionValue('groupId');}/**
|
|
3365
3384
|
* Fetches Group Traits
|
|
3366
3385
|
* @returns
|
|
3367
|
-
*/getGroupTraits(){return this.
|
|
3386
|
+
*/getGroupTraits(){return this.getUserSessionValue('groupTraits');}/**
|
|
3368
3387
|
* Fetches Initial Referrer
|
|
3369
3388
|
* @returns
|
|
3370
|
-
*/getInitialReferrer(){return this.
|
|
3389
|
+
*/getInitialReferrer(){return this.getUserSessionValue('initialReferrer');}/**
|
|
3371
3390
|
* Fetches Initial Referring domain
|
|
3372
3391
|
* @returns
|
|
3373
|
-
*/getInitialReferringDomain(){return this.
|
|
3392
|
+
*/getInitialReferringDomain(){return this.getUserSessionValue('initialReferringDomain');}/**
|
|
3374
3393
|
* Fetches session tracking information from storage
|
|
3375
3394
|
* @returns
|
|
3376
|
-
*/getSessionInfo(){return this.
|
|
3395
|
+
*/getSessionInfo(){return this.getUserSessionValue('sessionInfo');}/**
|
|
3377
3396
|
* Fetches auth token from storage
|
|
3378
3397
|
* @returns
|
|
3379
|
-
*/getAuthToken(){return this.
|
|
3398
|
+
*/getAuthToken(){return this.getUserSessionValue('authToken');}/**
|
|
3380
3399
|
* If session is active it returns the sessionId
|
|
3381
3400
|
* @returns
|
|
3382
3401
|
*/getSessionId(){const sessionInfo=this.getSessionInfo()??DEFAULT_USER_SESSION_VALUES.sessionInfo;if(sessionInfo.autoTrack&&!hasSessionExpired(sessionInfo)||sessionInfo.manualTrack){return sessionInfo.id??null;}return null;}/**
|
|
@@ -3392,7 +3411,7 @@ this.migrateStorageIfNeeded([store],[sessionKey]);const storageKey=entries[sessi
|
|
|
3392
3411
|
if(sessionInfo.sessionStart===undefined){sessionInfo={...sessionInfo,sessionStart:true};}else if(sessionInfo.sessionStart){sessionInfo={...sessionInfo,sessionStart:false};}}// Always write to state (in-turn to storage) to keep the session info up to date.
|
|
3393
3412
|
state.session.sessionInfo.value=sessionInfo;if(state.lifecycle.status.value!=='readyExecuted'){// Force update the storage as the 'effect' blocks are not getting triggered
|
|
3394
3413
|
// when processing preload buffered requests
|
|
3395
|
-
this.syncValueToStorage('sessionInfo'
|
|
3414
|
+
this.syncValueToStorage('sessionInfo');}}resetAndStartNewSession(){const session=state.session;const{manualTrack,autoTrack,timeout,cutOff}=session.sessionInfo.value;if(autoTrack){const sessionInfo={...DEFAULT_USER_SESSION_VALUES.sessionInfo,timeout};if(cutOff){sessionInfo.cutOff={enabled:cutOff.enabled,duration:cutOff.duration};}session.sessionInfo.value=sessionInfo;this.startOrRenewAutoTracking(session.sessionInfo.value);}else if(manualTrack){this.startManualTrackingInternal();}}/**
|
|
3396
3415
|
* Reset state values
|
|
3397
3416
|
* @param options options for reset
|
|
3398
3417
|
* @returns
|