Skip to content

Commit 548cb81

Browse files
fix: Handle cases where local session storage is not available (#43)
1 parent 89c467c commit 548cb81

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

src/Rokt-Kit.js

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

158+
function returnLocalSessionAttributes() {
159+
if (
160+
isEmpty(self.placementEventMappingLookup) ||
161+
!window.mParticle.Rokt ||
162+
typeof window.mParticle.Rokt.getLocalSessionAttributes !==
163+
'function'
164+
) {
165+
return {};
166+
}
167+
return window.mParticle.Rokt.getLocalSessionAttributes();
168+
}
169+
158170
function replaceOtherWithEmailsha256(_data) {
159171
var data = mergeObjects({}, _data || {});
160172
if (_data.hasOwnProperty(OTHER_IDENTITY)) {
@@ -211,14 +223,7 @@ var constructor = function () {
211223

212224
var filteredUserIdentities = returnUserIdentities(filteredUser);
213225

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-
}
226+
var localSessionAttributes = returnLocalSessionAttributes();
222227

223228
var selectPlacementsAttributes = mergeObjects(
224229
filteredUserIdentities,
@@ -256,6 +261,7 @@ var constructor = function () {
256261
function processEvent(event) {
257262
if (
258263
!isKitReady() ||
264+
isEmpty(self.placementEventMappingLookup) ||
259265
typeof window.mParticle.Rokt.setLocalSessionAttribute !== 'function'
260266
) {
261267
return;
@@ -494,6 +500,10 @@ function hashEventMessage(messageType, eventType, eventName) {
494500
);
495501
}
496502

503+
function isEmpty(value) {
504+
return value == null || !(Object.keys(value) || value).length;
505+
}
506+
497507
if (window && window.mParticle && window.mParticle.addForwarder) {
498508
window.mParticle.addForwarder({
499509
name: name,

test/src/tests.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,14 @@ describe('Rokt Forwarder', () => {
720720
await window.mParticle.forwarder.init(
721721
{
722722
accountId: '123456',
723+
placementEventMapping: JSON.stringify([
724+
{
725+
jsmap: 'test-event-hash',
726+
map: 'test-event-map',
727+
maptype: 'EventClass.Id',
728+
value: 'test-mapped-flag',
729+
},
730+
]),
723731
},
724732
reportService.cb,
725733
true
@@ -743,6 +751,44 @@ describe('Rokt Forwarder', () => {
743751
},
744752
});
745753
});
754+
755+
it('should not throw an error if getLocalSessionAttributes is not available', async () => {
756+
let errorLogged = false;
757+
const originalConsoleError = console.error;
758+
console.error = function (message) {
759+
if (
760+
message &&
761+
message.indexOf &&
762+
message.indexOf(
763+
'Error getting local session attributes'
764+
) !== -1
765+
) {
766+
errorLogged = true;
767+
}
768+
originalConsoleError.apply(console, arguments);
769+
};
770+
771+
delete window.mParticle.Rokt.getLocalSessionAttributes;
772+
773+
await window.mParticle.forwarder.init(
774+
{
775+
accountId: '123456',
776+
},
777+
reportService.cb,
778+
true
779+
);
780+
781+
await window.mParticle.forwarder.selectPlacements({
782+
identifier: 'test-placement',
783+
attributes: {
784+
'test-attribute': 'test-value',
785+
},
786+
});
787+
788+
errorLogged.should.equal(false);
789+
790+
console.error = originalConsoleError;
791+
});
746792
});
747793

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

0 commit comments

Comments
 (0)