Skip to content

Commit

Permalink
fix: almost final changes for the feature
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Jul 28, 2023
1 parent 836669b commit 1be846f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
29 changes: 21 additions & 8 deletions src/core/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,14 +711,18 @@ class Analytics {
const clonedTraits = R.clone(traits);
const clonedOptions = R.clone(options);

const curUserTraits = this.getUserTraits();
if (clonedTraits) {
const curUserTraits = this.getUserTraits();
for (const key in clonedTraits) {
curUserTraits[key] = clonedTraits[key];
}
this.storage.setUserTraits(curUserTraits);
}
const rudderElement = new RudderElementBuilder().setType('identify').build();
rudderElement.setUserId(normalisedUserId);
rudderElement.message.context.traits = {
...curUserTraits,
};
this.storage.setUserTraits(curUserTraits);

this.processAndSendDataToDestinations('identify', rudderElement, clonedOptions, callback);
}
Expand Down Expand Up @@ -774,11 +778,13 @@ class Analytics {
if (typeof groupId === 'function')
(callback = groupId), (options = null), (traits = null), (groupId = this.getGroupId());

this.storage.setGroupId(getStringId(groupId));
const normalizedGroupId = getStringId(groupId);
this.storage.setGroupId(normalizedGroupId);
const clonedTraits = R.clone(traits);
const clonedOptions = R.clone(options);

const rudderElement = new RudderElementBuilder().setType('group').build();
rudderElement.message.groupId = normalizedGroupId;

let curGroupTraits;
if (clonedTraits) {
Expand All @@ -790,6 +796,9 @@ class Analytics {
curGroupTraits = {};
}
this.storage.setGroupTraits(curGroupTraits);
rudderElement.message.traits = {
...curGroupTraits,
};

this.processAndSendDataToDestinations('group', rudderElement, clonedOptions, callback);
}
Expand Down Expand Up @@ -862,9 +871,11 @@ class Analytics {
// assign page properties to context
// rudderElement.message.context.page = getDefaultPageProperties();
this.errorReporting.leaveBreadcrumb('Started sending data to destinations');
rudderElement.message.context.traits = {
...this.getUserTraits(),
};
rudderElement.message.context.traits = rudderElement.message.context.traits
? rudderElement.message.context.traits
: {
...this.getUserTraits(),
};

// logger.debug("anonymousId: ", this.storage.getAnonymousId())
rudderElement.message.anonymousId = this.getAnonymousId();
Expand All @@ -874,10 +885,12 @@ class Analytics {

if (type == 'group') {
if (this.getGroupId()) {
rudderElement.message.groupId = this.getGroupId();
rudderElement.message.groupId = rudderElement.message.groupId
? rudderElement.message.groupId : this.getGroupId();
}
if (this.getGroupTraits()) {
rudderElement.message.traits = {
rudderElement.message.traits = rudderElement.message.traits
? rudderElement.message.traits : {
...this.getGroupTraits(),
};
}
Expand Down
9 changes: 4 additions & 5 deletions src/features/core/session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ class UserSession {
* @param {number} timestamp
* @returns boolean
*/
isValidSession(timestamp) {
const sessionInfo = this.getSafeSessionInfo();
isValidSession(sessionInfo, timestamp) {
return timestamp <= sessionInfo.expiresAt;
}

Expand All @@ -102,7 +101,7 @@ class UserSession {
startAutoTracking(sessionInfo) {
let finalSessionInfo = sessionInfo;
const timestamp = Date.now();
if (!this.isValidSession(timestamp)) {
if (!this.isValidSession(finalSessionInfo, timestamp)) {
finalSessionInfo = {
id: timestamp, // set the current timestamp
expiresAt: timestamp + this.timeout, // set the expiry time of the session
Expand Down Expand Up @@ -156,7 +155,7 @@ class UserSession {
getSessionId() {
const sessionInfo = this.getSafeSessionInfo();
if (
(sessionInfo.autoTrack && this.isValidSession(Date.now())) ||
(sessionInfo.autoTrack && this.isValidSession(sessionInfo, Date.now())) ||
sessionInfo.manualTrack
) {
return sessionInfo.id;
Expand All @@ -181,7 +180,7 @@ class UserSession {
// renew or create a new auto-tracking session
if (sessionInfo.autoTrack) {
const timestamp = Date.now();
if (!this.isValidSession(timestamp)) {
if (!this.isValidSession(sessionInfo, timestamp)) {
sessionInfo = this.startAutoTracking(sessionInfo);
} else {
sessionInfo.expiresAt = timestamp + this.timeout; // set the expiry time of the session
Expand Down
1 change: 1 addition & 0 deletions src/utils/storage/memoryStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class InMemoryStorage {
constructor(options) {
this.opts = { enabled: true };
this.options(options ?? {});
this.isSupportAvailable = true;
}

options(inOpts) {
Expand Down
33 changes: 24 additions & 9 deletions src/utils/storage/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ class Storage {
};
}

options(options = {}) {
const globalStorageType = get(options, 'storage.type');
options(opts = {}) {
const globalStorageType = opts.type;

Object.keys(this.storageEntries).forEach((entry) => {
const storageType = globalStorageType || get(options, `storage.entries.${entry}.type`) || 'cookie';
const storageType = get(opts, `entries.${entry}.type`) || globalStorageType || 'cookie';
let selectedStorage = null;
switch (storageType) {
case 'cookie':
{
if (Cookie.isSupportAvailable) {
selectedStorage = Cookie;
} else if (Store.enabled) {
} else if (Store.isSupportAvailable) {
selectedStorage = Store;
} else {
selectedStorage = defaultInMemoryStorage;
Expand All @@ -157,7 +157,7 @@ class Storage {
}
case 'localStorage':
{
if (Store.enabled) {
if (Store.isSupportAvailable) {
selectedStorage = Store;
} else {
selectedStorage = defaultInMemoryStorage;
Expand All @@ -170,15 +170,30 @@ class Storage {
case 'none':
default:
selectedStorage = null;
break;
}
if (selectedStorage) {
selectedStorage.options(options);
selectedStorage.options(opts);
}
this.storageEntries[entry].storage = selectedStorage;
this.migrateDataFromPreviousStorage(selectedStorage, entry);
});
}

// TODO: Migrate any existing data from previous storage
// TODO: Delete storage data if the storage type is memory or none
migrateDataFromPreviousStorage(curStorage, entry) {
// in the increasing order of preference
const storages = [Store, Cookie];
storages.forEach((stg) => {
if (stg !== curStorage && stg.isSupportAvailable) {
const value = stg.get(this.storageEntries[entry].key);
if (value) {
if (curStorage) {
curStorage.set(this.storageEntries[entry].key, value);
}
stg.remove(this.storageEntries[entry].key);
}
}
});
}

/**
Expand Down Expand Up @@ -332,7 +347,7 @@ class Storage {
* First check the local storage for anonymousId
* Ref: https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#identify
*/
if (Store.enabled) {
if (Store.isSupportAvailable) {
anonId = Store.get(anonymousIdKeyMap[key]);
}
// If anonymousId is not present in local storage and check cookie support exists
Expand Down
3 changes: 2 additions & 1 deletion src/utils/storage/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import store from 'storejs';
class StoreLocal {
constructor(options) {
this.sOpts = {};
this.enabled = this.checkSupportAvailability();
this.isSupportAvailable = this.checkSupportAvailability();
this.enabled = this.isSupportAvailable;
this.options(options);
}

Expand Down

0 comments on commit 1be846f

Please sign in to comment.