Skip to content

Commit 1918bf1

Browse files
feat: Add hashedAttributes method
1 parent fe62270 commit 1918bf1

2 files changed

Lines changed: 147 additions & 2 deletions

File tree

src/Rokt-Kit.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ var constructor = function () {
3030
self.filteredUser = {};
3131
self.userAttributes = {};
3232

33+
/**
34+
* Passes attributes to the Rokt Web SDK for server-side hashing
35+
* @param {Object} attributes - The attributes to be hashed
36+
* @returns {Object} The hashed attributes from the launcher
37+
*/
38+
function hashedAttributes(attributes) {
39+
if (!isInitialized()) {
40+
console.error('Rokt Kit: Not initialized');
41+
return null;
42+
}
43+
return self.launcher.hashedAttributes(attributes);
44+
}
45+
3346
function initForwarder(
3447
settings,
3548
_service,
@@ -192,8 +205,6 @@ var constructor = function () {
192205
});
193206
}
194207

195-
// Called by the mParticle Rokt Manager
196-
this.selectPlacements = selectPlacements;
197208

198209
// mParticle Kit Callback Methods
199210
function fetchOptimizely() {
@@ -236,10 +247,29 @@ var constructor = function () {
236247
}
237248
return {};
238249
}
250+
251+
252+
// Called by the mParticle Rokt Manager
253+
this.selectPlacements = selectPlacements;
254+
this.hashedAttributes = hashedAttributes;
255+
256+
// Kit Callback Methods
239257
this.init = initForwarder;
240258
this.setUserAttribute = setUserAttribute;
241259
this.onUserIdentified = onUserIdentified;
242260
this.removeUserAttribute = removeUserAttribute;
261+
262+
/**
263+
* Checks if the kit is properly initialized and ready for use.
264+
* Both conditions must be true:
265+
* 1. self.isInitialized - Set after successful initialization of the kit
266+
* 2. self.launcher - The Rokt launcher instance must be available
267+
* @returns {boolean} Whether the kit is fully initialized
268+
*/
269+
function isInitialized() {
270+
return !!(self.isInitialized && self.launcher);
271+
}
272+
243273
};
244274

245275
function getId() {

test/src/tests.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,121 @@ describe('Rokt Forwarder', () => {
189189
});
190190
});
191191

192+
describe('#hashedAttributes', () => {
193+
beforeEach(() => {
194+
window.Rokt = new MockRoktForwarder();
195+
window.mParticle.Rokt = window.Rokt;
196+
window.mParticle.Rokt.attachKitCalled = false;
197+
window.mParticle.Rokt.attachKit = async (kit) => {
198+
window.mParticle.Rokt.attachKitCalled = true;
199+
window.mParticle.Rokt.kit = kit;
200+
Promise.resolve();
201+
};
202+
window.mParticle.forwarder.launcher = {
203+
hashedAttributes: function (attributes) {
204+
window.mParticle.Rokt.hashedAttributesOptions = attributes;
205+
window.mParticle.Rokt.hashedAttributesCalled = true;
206+
207+
// Mocking the hashedAttributes method to show that
208+
// the attributes will be transformed by the launcher's
209+
// hashedAttributes method.
210+
return Promise.resolve({
211+
'test-attribute': 'hashed-value',
212+
});
213+
},
214+
};
215+
});
216+
217+
it('should call launcher.hashedAttributes with passed through attributes when fully initialized', function () {
218+
// Ensure both initialization conditions are met
219+
window.mParticle.forwarder.isInitialized = true;
220+
window.mParticle.forwarder.launcher = {
221+
hashedAttributes: function (attributes) {
222+
window.mParticle.Rokt.hashedAttributesOptions = attributes;
223+
window.mParticle.Rokt.hashedAttributesCalled = true;
224+
return {
225+
'test-attribute': 'hashed-value',
226+
};
227+
},
228+
};
229+
230+
var attributes = {
231+
'test-attribute': 'test-value',
232+
};
233+
234+
window.mParticle.forwarder.hashedAttributes(attributes);
235+
236+
window.Rokt.hashedAttributesCalled.should.equal(true);
237+
window.Rokt.hashedAttributesOptions.should.deepEqual(attributes);
238+
});
239+
240+
it('should return null when launcher exists but kit is not initialized', function () {
241+
// Set launcher but ensure isInitialized is false
242+
window.mParticle.forwarder.isInitialized = false;
243+
window.mParticle.forwarder.launcher = {
244+
hashedAttributes: function () {},
245+
};
246+
247+
var result = window.mParticle.forwarder.hashedAttributes({
248+
'test-attribute': 'test-value',
249+
});
250+
251+
(result === null).should.equal(true);
252+
});
253+
254+
it('should log an error when called before initialization', function () {
255+
var errorLogged = false;
256+
var errorMessage = null;
257+
window.console.error = function (message) {
258+
errorLogged = true;
259+
errorMessage = message;
260+
};
261+
262+
// Ensure kit is not initialized
263+
window.mParticle.forwarder.isInitialized = false;
264+
window.mParticle.forwarder.launcher = null;
265+
266+
window.mParticle.forwarder.hashedAttributes({
267+
'test-attribute': 'test-value',
268+
});
269+
270+
errorLogged.should.equal(true);
271+
errorMessage.should.equal('Rokt Kit: Not initialized');
272+
});
273+
274+
it('should return null when kit is initialized but launcher is missing', function () {
275+
// Mock isInitialized but remove launcher
276+
window.mParticle.forwarder.isInitialized = true;
277+
window.mParticle.forwarder.launcher = null;
278+
279+
var result = window.mParticle.forwarder.hashedAttributes({
280+
'test-attribute': 'test-value',
281+
});
282+
283+
(result === null).should.equal(true);
284+
});
285+
286+
it('should return hashed attributes from launcher', async () => {
287+
await window.mParticle.forwarder.init(
288+
{
289+
accountId: '123456',
290+
},
291+
reportService.cb,
292+
true,
293+
null,
294+
{}
295+
);
296+
297+
const result = await window.mParticle.forwarder.hashedAttributes({
298+
'test-attribute': 'test-value',
299+
});
300+
301+
result.should.deepEqual({
302+
'test-attribute': 'hashed-value',
303+
});
304+
});
305+
});
306+
192307
describe('#selectPlacements', () => {
193308
beforeEach(() => {
194309
window.Rokt = new MockRoktForwarder();

0 commit comments

Comments
 (0)