Skip to content

Commit 27b4260

Browse files
fix: Handle cases where local session storage is not available
1 parent 89c467c commit 27b4260

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

src/Rokt-Kit.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ var constructor = function () {
155155
return replaceOtherWithEmailsha256(userIdentities);
156156
}
157157

158+
function returnLocalSessionAttributes() {
159+
if (
160+
!window.mParticle.Rokt ||
161+
typeof window.mParticle.Rokt.getLocalSessionAttributes !==
162+
'function'
163+
) {
164+
return {};
165+
}
166+
return window.mParticle.Rokt.getLocalSessionAttributes();
167+
}
168+
158169
function replaceOtherWithEmailsha256(_data) {
159170
var data = mergeObjects({}, _data || {});
160171
if (_data.hasOwnProperty(OTHER_IDENTITY)) {
@@ -211,14 +222,7 @@ var constructor = function () {
211222

212223
var filteredUserIdentities = returnUserIdentities(filteredUser);
213224

214-
var localSessionAttributes = {};
215-
216-
try {
217-
localSessionAttributes =
218-
window.mParticle.Rokt.getLocalSessionAttributes();
219-
} catch (error) {
220-
console.error('Error getting local session attributes:', error);
221-
}
225+
var localSessionAttributes = returnLocalSessionAttributes();
222226

223227
var selectPlacementsAttributes = mergeObjects(
224228
filteredUserIdentities,
@@ -256,6 +260,7 @@ var constructor = function () {
256260
function processEvent(event) {
257261
if (
258262
!isKitReady() ||
263+
isEmpty(self.placementEventMappingLookup) ||
259264
typeof window.mParticle.Rokt.setLocalSessionAttribute !== 'function'
260265
) {
261266
return;
@@ -494,6 +499,10 @@ function hashEventMessage(messageType, eventType, eventName) {
494499
);
495500
}
496501

502+
function isEmpty(value) {
503+
return value == null || !(Object.keys(value) || value).length;
504+
}
505+
497506
if (window && window.mParticle && window.mParticle.addForwarder) {
498507
window.mParticle.addForwarder({
499508
name: name,

test/src/tests.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,44 @@ describe('Rokt Forwarder', () => {
743743
},
744744
});
745745
});
746+
747+
it('should not throw an error if getLocalSessionAttributes is not available', async () => {
748+
let errorLogged = false;
749+
const originalConsoleError = console.error;
750+
console.error = function (message) {
751+
if (
752+
message &&
753+
message.indexOf &&
754+
message.indexOf(
755+
'Error getting local session attributes'
756+
) !== -1
757+
) {
758+
errorLogged = true;
759+
}
760+
originalConsoleError.apply(console, arguments);
761+
};
762+
763+
delete window.mParticle.Rokt.getLocalSessionAttributes;
764+
765+
await window.mParticle.forwarder.init(
766+
{
767+
accountId: '123456',
768+
},
769+
reportService.cb,
770+
true
771+
);
772+
773+
await window.mParticle.forwarder.selectPlacements({
774+
identifier: 'test-placement',
775+
attributes: {
776+
'test-attribute': 'test-value',
777+
},
778+
});
779+
780+
errorLogged.should.equal(false);
781+
782+
console.error = originalConsoleError;
783+
});
746784
});
747785

748786
describe('User Attributes', () => {

0 commit comments

Comments
 (0)