Skip to content

Commit 1a360e7

Browse files
LE - update condition checks to use isString utility
1 parent 114f325 commit 1a360e7

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

src/Rokt-Kit.js

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var constructor = function () {
5353
}
5454

5555
function doesEventAttributeConditionMatch(condition, actualValue) {
56-
if (!condition || typeof condition.operator !== 'string') {
56+
if (!condition || !isString(condition.operator)) {
5757
return false;
5858
}
5959

@@ -64,25 +64,23 @@ var constructor = function () {
6464
return actualValue !== null;
6565
}
6666

67+
if (actualValue == null) {
68+
return false;
69+
}
70+
6771
if (operator === 'equals') {
68-
return actualValue === expectedValue;
72+
return String(actualValue) === String(expectedValue);
6973
}
7074

7175
if (operator === 'contains') {
72-
if (
73-
typeof actualValue !== 'string' ||
74-
typeof expectedValue !== 'string'
75-
) {
76-
return false;
77-
}
78-
return actualValue.indexOf(expectedValue) !== -1;
76+
return String(actualValue).indexOf(String(expectedValue)) !== -1;
7977
}
8078

8179
return false;
8280
}
8381

8482
function doesEventMatchRule(event, rule) {
85-
if (!rule || typeof rule.eventAttributeKey !== 'string') {
83+
if (!rule || !isString(rule.eventAttributeKey)) {
8684
return false;
8785
}
8886

@@ -91,11 +89,11 @@ var constructor = function () {
9189
return false;
9290
}
9391

92+
var actualValue = getEventAttributeValue(event, rule.eventAttributeKey);
93+
9494
if (conditions.length === 0) {
95-
return true;
95+
return actualValue !== null;
9696
}
97-
98-
var actualValue = getEventAttributeValue(event, rule.eventAttributeKey);
9997
for (var i = 0; i < conditions.length; i++) {
10098
if (!doesEventAttributeConditionMatch(conditions[i], actualValue)) {
10199
return false;
@@ -116,8 +114,8 @@ var constructor = function () {
116114
var mapping = placementEventAttributeMapping[i];
117115
if (
118116
!mapping ||
119-
typeof mapping.value !== 'string' ||
120-
typeof mapping.map !== 'string'
117+
!isString(mapping.value) ||
118+
!isString(mapping.map)
121119
) {
122120
continue;
123121
}
@@ -140,24 +138,14 @@ var constructor = function () {
140138
}
141139

142140
function applyPlacementEventAttributeMapping(event) {
143-
if (
144-
!self.placementEventAttributeMappingLookup ||
145-
isEmpty(self.placementEventAttributeMappingLookup)
146-
) {
147-
return;
148-
}
149-
150141
var mappedAttributeKeys = Object.keys(
151142
self.placementEventAttributeMappingLookup
152143
);
153144
for (var i = 0; i < mappedAttributeKeys.length; i++) {
154145
var mappedAttributeKey = mappedAttributeKeys[i];
155146
var rulesForMappedAttributeKey =
156147
self.placementEventAttributeMappingLookup[mappedAttributeKey];
157-
if (
158-
!rulesForMappedAttributeKey ||
159-
!rulesForMappedAttributeKey.length
160-
) {
148+
if (isEmpty(rulesForMappedAttributeKey)) {
161149
continue;
162150
}
163151

@@ -423,7 +411,7 @@ var constructor = function () {
423411
console.error('Rokt Kit: Not initialized');
424412
return Promise.reject(new Error('Rokt Kit: Not initialized'));
425413
}
426-
if (!extensionName || typeof extensionName !== 'string') {
414+
if (!extensionName || !isString(extensionName)) {
427415
return Promise.reject(
428416
new Error('Rokt Kit: Invalid extension name')
429417
);
@@ -466,7 +454,9 @@ var constructor = function () {
466454
return;
467455
}
468456

469-
applyPlacementEventAttributeMapping(event);
457+
if (!isEmpty(self.placementEventAttributeMappingLookup)) {
458+
applyPlacementEventAttributeMapping(event);
459+
}
470460

471461
if (isEmpty(self.placementEventMappingLookup)) {
472462
return;
@@ -740,6 +730,10 @@ function isEmpty(value) {
740730
return value == null || !(Object.keys(value) || value).length;
741731
}
742732

733+
function isString(value) {
734+
return typeof value === 'string';
735+
}
736+
743737
if (window && window.mParticle && window.mParticle.addForwarder) {
744738
window.mParticle.addForwarder({
745739
name: name,

test/src/tests.js

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,6 +3381,42 @@ describe('Rokt Forwarder', () => {
33813381
});
33823382
});
33833383

3384+
it('should not set local session attribute when mapped attribute key is missing from event and no conditions have been defined', async () => {
3385+
const placementEventAttributeMapping = JSON.stringify([
3386+
{
3387+
jsmap: null,
3388+
map: 'URL',
3389+
maptype: 'EventAttributeClass.Name',
3390+
value: 'hasUrl',
3391+
},
3392+
]);
3393+
3394+
await window.mParticle.forwarder.init(
3395+
{
3396+
accountId: '123456',
3397+
placementEventAttributeMapping,
3398+
},
3399+
reportService.cb,
3400+
true,
3401+
null,
3402+
{}
3403+
);
3404+
3405+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
3406+
3407+
window.mParticle._Store.localSessionAttributes = {};
3408+
window.mParticle.forwarder.process({
3409+
EventName: 'Browse',
3410+
EventCategory: EventType.Unknown,
3411+
EventDataType: MessageType.PageView,
3412+
EventAttributes: {
3413+
someOtherAttribute: 'value',
3414+
},
3415+
});
3416+
3417+
window.mParticle._Store.localSessionAttributes.should.deepEqual({});
3418+
});
3419+
33843420
it('should support exists operator for placementEventAttributeMapping conditions', async () => {
33853421
const placementEventAttributeMapping = JSON.stringify([
33863422
{
@@ -3420,7 +3456,7 @@ describe('Rokt Forwarder', () => {
34203456
});
34213457
});
34223458

3423-
it('should evaluate equals type-sensitively for placementEventAttributeMapping conditions', async () => {
3459+
it('should evaluate equals for placementEventAttributeMapping conditions', async () => {
34243460
const placementEventAttributeMapping = JSON.stringify([
34253461
{
34263462
jsmap: null,
@@ -3471,10 +3507,12 @@ describe('Rokt Forwarder', () => {
34713507
number_of_products: '2',
34723508
},
34733509
});
3474-
window.mParticle._Store.localSessionAttributes.should.deepEqual({});
3510+
window.mParticle._Store.localSessionAttributes.should.deepEqual({
3511+
multipleproducts: true,
3512+
});
34753513
});
34763514

3477-
it('should treat contains as string-only (non-strings do not match) for placementEventAttributeMapping', async () => {
3515+
it('should evaluate contains for placementEventAttributeMapping conditions', async () => {
34783516
const placementEventAttributeMapping = JSON.stringify([
34793517
{
34803518
jsmap: null,
@@ -3512,7 +3550,9 @@ describe('Rokt Forwarder', () => {
35123550
number_of_products: 2,
35133551
},
35143552
});
3515-
window.mParticle._Store.localSessionAttributes.should.deepEqual({});
3553+
window.mParticle._Store.localSessionAttributes.should.deepEqual({
3554+
containsNumber: true,
3555+
});
35163556
});
35173557

35183558
it('should require ALL rules for the same mapped key to match (AND across rules)', async () => {
@@ -3527,6 +3567,17 @@ describe('Rokt Forwarder', () => {
35273567
operator: 'contains',
35283568
attributeValue: 'sale',
35293569
},
3570+
{
3571+
operator: 'exists',
3572+
},
3573+
],
3574+
},
3575+
{
3576+
jsmap: null,
3577+
map: 'URL',
3578+
maptype: 'EventAttributeClass.Name',
3579+
value: 'saleSeeker',
3580+
conditions: [
35303581
{
35313582
operator: 'contains',
35323583
attributeValue: 'items',
@@ -3548,7 +3599,6 @@ describe('Rokt Forwarder', () => {
35483599

35493600
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
35503601

3551-
// Matches only 1/2 rules => should NOT set
35523602
window.mParticle._Store.localSessionAttributes = {};
35533603
window.mParticle.forwarder.process({
35543604
EventName: 'Browse',
@@ -3560,7 +3610,6 @@ describe('Rokt Forwarder', () => {
35603610
});
35613611
window.mParticle._Store.localSessionAttributes.should.deepEqual({});
35623612

3563-
// Matches both rules => should set
35643613
window.mParticle._Store.localSessionAttributes = {};
35653614
window.mParticle.forwarder.process({
35663615
EventName: 'Browse',
@@ -3574,7 +3623,7 @@ describe('Rokt Forwarder', () => {
35743623
saleSeeker: true,
35753624
});
35763625
});
3577-
it('should map multiple attributes for the same mapped key (AND across rules)', async () => {
3626+
it('should set multiple local session attributes for the same event attribute key', async () => {
35783627
const placementEventAttributeMapping = JSON.stringify([
35793628
{
35803629
jsmap: null,
@@ -3785,4 +3834,4 @@ describe('Rokt Forwarder', () => {
37853834
resultHash.should.equal('hashed-<48Test Event>-value');
37863835
});
37873836
});
3788-
});
3837+
});

0 commit comments

Comments
 (0)