Skip to content

Commit f950982

Browse files
fix: Add MPID to selectPlacements call (#15)
1 parent 11fa99c commit f950982

File tree

2 files changed

+176
-17
lines changed

2 files changed

+176
-17
lines changed

src/Rokt-Kit.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,14 @@ var constructor = function () {
101101

102102
self.userAttributes = filteredAttributes;
103103

104+
var mpid = self.filters.filteredUser.getMPID();
105+
106+
var selectPlacementsAttributes = mergeObjects(filteredAttributes, {
107+
mpid: mpid,
108+
});
109+
104110
var selectPlacementsOptions = mergeObjects(options, {
105-
attributes: filteredAttributes,
111+
attributes: selectPlacementsAttributes,
106112
});
107113

108114
self.launcher.selectPlacements(selectPlacementsOptions);
@@ -133,6 +139,7 @@ var constructor = function () {
133139
// Locally cache the launcher and filters
134140
self.launcher = launcher;
135141
self.filters = window.mParticle.Rokt.filters;
142+
self.filteredUser = window.mParticle.Rokt.filters.filteredUser;
136143

137144
// Attaches the kit to the Rokt manager
138145
window.mParticle.Rokt.attachKit(self);

test/src/tests.js

Lines changed: 168 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
/* eslint-env es6, mocha */
22
/* eslint-parser babel-eslint */
33

4+
const waitForCondition = async (conditionFn, timeout = 200, interval = 10) => {
5+
return new Promise((resolve, reject) => {
6+
const startTime = Date.now();
7+
8+
(function poll() {
9+
if (conditionFn()) {
10+
return resolve(undefined);
11+
} else if (Date.now() - startTime > timeout) {
12+
return reject(new Error('Timeout waiting for condition'));
13+
} else {
14+
setTimeout(poll, interval);
15+
}
16+
})();
17+
});
18+
};
19+
420
describe('Rokt Forwarder', () => {
521
var ReportingService = function () {
622
var self = this;
@@ -63,30 +79,83 @@ describe('Rokt Forwarder', () => {
6379

6480
before(() => {});
6581

66-
beforeEach(() => {});
82+
beforeEach(() => {
83+
window.Rokt = new MockRoktForwarder();
84+
window.mParticle.Rokt = window.Rokt;
85+
});
86+
87+
afterEach(() => {
88+
window.mParticle.forwarder.userAttributes = {};
89+
});
6790

6891
describe('#initForwarder', () => {
69-
it('should initialize the Rokt Web SDK', async () => {
92+
beforeEach(() => {
7093
window.Rokt = new MockRoktForwarder();
7194
window.mParticle.Rokt = window.Rokt;
7295
window.mParticle.Rokt.attachKitCalled = false;
73-
window.mParticle.Rokt.attachKit = async () => {
96+
window.mParticle.Rokt.attachKit = async (kit) => {
7497
window.mParticle.Rokt.attachKitCalled = true;
98+
window.mParticle.Rokt.kit = kit;
7599
Promise.resolve();
76100
};
101+
window.mParticle.Rokt.filters = {
102+
userAttributesFilters: [],
103+
filterUserAttributes: function (attributes) {
104+
return attributes;
105+
},
106+
filteredUser: {
107+
getMPID: function () {
108+
return '123';
109+
},
110+
},
111+
};
112+
});
77113

114+
it('should initialize the Rokt Web SDK', async () => {
78115
await mParticle.forwarder.init(
79116
{
80117
accountId: '123456',
81118
},
82119
reportService.cb,
83-
true
120+
true,
121+
null,
122+
{}
84123
);
85124

86125
window.Rokt.accountId.should.equal('123456');
87126

88127
window.Rokt.createLauncherCalled.should.equal(true);
89128
});
129+
130+
it('should set the filters on the forwarder', async () => {
131+
await mParticle.forwarder.init(
132+
{
133+
accountId: '123456',
134+
},
135+
reportService.cb,
136+
true,
137+
null,
138+
{}
139+
);
140+
141+
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
142+
143+
window.mParticle.Rokt.kit.filters.should.deepEqual({
144+
userAttributesFilters: [],
145+
filterUserAttributes: function (attributes) {
146+
return attributes;
147+
},
148+
filteredUser: {
149+
getMPID: function () {
150+
return '123';
151+
},
152+
},
153+
});
154+
155+
window.mParticle.Rokt.kit.filteredUser
156+
.getMPID()
157+
.should.equal('123');
158+
});
90159
});
91160

92161
describe('#selectPlacements', () => {
@@ -103,16 +172,20 @@ describe('Rokt Forwarder', () => {
103172
window.mParticle.Rokt.selectPlacementsCalled = true;
104173
},
105174
};
106-
});
107-
108-
it('should call launcher.selectPlacements with all passed through options', async () => {
109175
window.mParticle.forwarder.filters = {
110176
userAttributesFilters: [],
111177
filterUserAttributes: function (attributes) {
112178
return attributes;
113179
},
180+
filteredUser: {
181+
getMPID: function () {
182+
return '123';
183+
},
184+
},
114185
};
186+
});
115187

188+
it('should call launcher.selectPlacements with all passed through options', async () => {
116189
await window.mParticle.forwarder.init(
117190
{
118191
accountId: '123456',
@@ -135,20 +208,19 @@ describe('Rokt Forwarder', () => {
135208
identifier: 'test-placement',
136209
attributes: {
137210
test: 'test',
211+
mpid: '123',
138212
},
139213
});
140214
});
141215

142216
it('should call launcher.selectPlacements with filtered user attributes', async () => {
143-
window.mParticle.forwarder.filters = {
144-
userAttributesFilters: [],
145-
filterUserAttributes: function () {
217+
window.mParticle.forwarder.filters.filterUserAttributes =
218+
function () {
146219
return {
147220
'user-attribute': 'user-attribute-value',
148221
'unfiltered-attribute': 'unfiltered-value',
149222
};
150-
},
151-
};
223+
};
152224

153225
await window.mParticle.forwarder.init(
154226
{
@@ -174,6 +246,7 @@ describe('Rokt Forwarder', () => {
174246
attributes: {
175247
'user-attribute': 'user-attribute-value',
176248
'unfiltered-attribute': 'unfiltered-value',
249+
mpid: '123',
177250
},
178251
});
179252
});
@@ -184,15 +257,14 @@ describe('Rokt Forwarder', () => {
184257
// remove any attributes from the init method that are filtered.
185258
// Also, any initial attributes from the init call that have updated
186259
// durring runtime should be returned by the filterUserAttribute method.
187-
window.mParticle.forwarder.filters = {
188-
filterUserAttributes: function () {
260+
window.mParticle.forwarder.filters.filterUserAttributes =
261+
function () {
189262
return {
190263
'user-attribute': 'user-attribute-value',
191264
'unfiltered-attribute': 'unfiltered-value',
192265
'changed-attribute': 'new-value',
193266
};
194-
},
195-
};
267+
};
196268

197269
await window.mParticle.forwarder.init(
198270
{
@@ -229,8 +301,88 @@ describe('Rokt Forwarder', () => {
229301
'user-attribute': 'user-attribute-value',
230302
'unfiltered-attribute': 'unfiltered-value',
231303
'changed-attribute': 'new-value',
304+
mpid: '123',
232305
},
233306
});
234307
});
308+
309+
it('should collect mpid and send to launcher.selectPlacements', async () => {
310+
await window.mParticle.forwarder.init(
311+
{
312+
accountId: '123456',
313+
},
314+
reportService.cb,
315+
true,
316+
null,
317+
{
318+
'user-attribute': 'user-attribute-value',
319+
}
320+
);
321+
322+
await window.mParticle.forwarder.selectPlacements({
323+
identifier: 'test-placement',
324+
attributes: {
325+
'user-attribute': 'user-attribute-value',
326+
},
327+
});
328+
329+
window.Rokt.selectPlacementsCalled.should.equal(true);
330+
window.Rokt.selectPlacementsOptions.should.deepEqual({
331+
identifier: 'test-placement',
332+
attributes: {
333+
'user-attribute': 'user-attribute-value',
334+
mpid: '123',
335+
},
336+
});
337+
});
338+
});
339+
340+
describe('#setUserAttribute', () => {
341+
it('should set the user attribute', async () => {
342+
window.mParticle.forwarder.setUserAttribute(
343+
'test-attribute',
344+
'test-value'
345+
);
346+
347+
window.mParticle.forwarder.userAttributes.should.deepEqual({
348+
'test-attribute': 'test-value',
349+
});
350+
});
351+
});
352+
353+
describe('#removeUserAttribute', () => {
354+
it('should remove the user attribute', async () => {
355+
window.mParticle.forwarder.setUserAttribute(
356+
'test-attribute',
357+
'test-value'
358+
);
359+
360+
window.mParticle.forwarder.removeUserAttribute('test-attribute');
361+
362+
window.mParticle.forwarder.userAttributes.should.deepEqual({});
363+
});
364+
});
365+
366+
describe('#onUserIdentified', () => {
367+
it('should set the filtered user', async () => {
368+
window.mParticle.forwarder.onUserIdentified({
369+
getAllUserAttributes: function () {
370+
return {
371+
'test-attribute': 'test-value',
372+
};
373+
},
374+
getMPID: function () {
375+
return '123';
376+
},
377+
});
378+
379+
window.mParticle.forwarder.userAttributes.should.deepEqual({
380+
'test-attribute': 'test-value',
381+
});
382+
383+
window.mParticle.forwarder.filteredUser
384+
.getMPID()
385+
.should.equal('123');
386+
});
235387
});
236388
});

0 commit comments

Comments
 (0)