Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ var constructor = function () {
self.filteredUser = {};
self.userAttributes = {};

/**
* Passes attributes to the Rokt Web SDK for server-side hashing
Comment thread
alexs-mparticle marked this conversation as resolved.
Outdated
* @see https://docs.rokt.com/developers/integration-guides/web/library/integration-launcher#hash-attributes
* @param {Object} attributes - The attributes to be hashed
* @returns {Promise<Object|null>} A Promise resolving to the
* hashed attributes from the launcher, or `null` if the kit is not initialized
*/
function hashAttributes(attributes) {
if (!isInitialized()) {
console.error('Rokt Kit: Not initialized');
return null;
}
return self.launcher.hashAttributes(attributes);
}

function initForwarder(
settings,
_service,
Expand Down Expand Up @@ -81,12 +96,15 @@ var constructor = function () {
}
}

/**
* Selects placements for Rokt Web SDK with merged attributes, filters, and experimentation options
* @see https://docs.rokt.com/developers/integration-guides/web/library/select-placements-options/
* @param {Object} options - The options object for selecting placements containing:
* - identifier {string}: The placement identifier
* - attributes {Object}: Optional attributes to merge with existing attributes
* @returns {Promise<void>} A Promise resolving to the Rokt launcher's selectPlacements method with processed attributes
*/
function selectPlacements(options) {
// https://docs.rokt.com/developers/integration-guides/web/library/select-placements-options/
// options should contain:
// - identifier
// - attributes

var attributes = (options && options.attributes) || {};
var placementAttributes = mergeObjects(self.userAttributes, attributes);

Expand Down Expand Up @@ -192,9 +210,6 @@ var constructor = function () {
});
}

// Called by the mParticle Rokt Manager
this.selectPlacements = selectPlacements;

// mParticle Kit Callback Methods
function fetchOptimizely() {
var forwarders = window.mParticle
Expand Down Expand Up @@ -236,10 +251,27 @@ var constructor = function () {
}
return {};
}

// Called by the mParticle Rokt Manager
this.selectPlacements = selectPlacements;
this.hashAttributes = hashAttributes;

// Kit Callback Methods
this.init = initForwarder;
this.setUserAttribute = setUserAttribute;
this.onUserIdentified = onUserIdentified;
this.removeUserAttribute = removeUserAttribute;

/**
* Checks if the kit is properly initialized and ready for use.
* Both conditions must be true:
* 1. self.isInitialized - Set after successful initialization of the kit
* 2. self.launcher - The Rokt launcher instance must be available
* @returns {boolean} Whether the kit is fully initialized
*/
function isInitialized() {
return !!(self.isInitialized && self.launcher);
}
};

function getId() {
Expand Down
115 changes: 115 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,121 @@ describe('Rokt Forwarder', () => {
});
});

describe('#hashAttributes', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async (kit) => {
window.mParticle.Rokt.attachKitCalled = true;
window.mParticle.Rokt.kit = kit;
Promise.resolve();
};
window.mParticle.forwarder.launcher = {
hashAttributes: function (attributes) {
window.mParticle.Rokt.hashAttributesOptions = attributes;
Comment thread
alexs-mparticle marked this conversation as resolved.
Outdated
window.mParticle.Rokt.hashAttributesCalled = true;

// Mocking the hashAttributes method to show that
// the attributes will be transformed by the launcher's
// hashAttributes method.
return Promise.resolve({
'test-attribute': 'hashed-value',
});
},
};
});

it('should call launcher.hashAttributes with passed through attributes when fully initialized', function () {
// Ensure both initialization conditions are met
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = {
hashAttributes: function (attributes) {
window.mParticle.Rokt.hashAttributesOptions = attributes;
window.mParticle.Rokt.hashAttributesCalled = true;
return {
'test-attribute': 'hashed-value',
};
},
};

var attributes = {
'test-attribute': 'test-value',
};

window.mParticle.forwarder.hashAttributes(attributes);

window.Rokt.hashAttributesCalled.should.equal(true);
window.Rokt.hashAttributesOptions.should.deepEqual(attributes);
});

it('should return null when launcher exists but kit is not initialized', function () {
// Set launcher but ensure isInitialized is false
window.mParticle.forwarder.isInitialized = false;
window.mParticle.forwarder.launcher = {
hashAttributes: function () {},
};

var result = window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});

(result === null).should.equal(true);
});

it('should log an error when called before initialization', function () {
var errorLogged = false;
var errorMessage = null;
window.console.error = function (message) {
errorLogged = true;
errorMessage = message;
};

// Ensure kit is not initialized
window.mParticle.forwarder.isInitialized = false;
window.mParticle.forwarder.launcher = null;

window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});

errorLogged.should.equal(true);
errorMessage.should.equal('Rokt Kit: Not initialized');
});

it('should return null when kit is initialized but launcher is missing', function () {
Comment thread
alexs-mparticle marked this conversation as resolved.
// Mock isInitialized but remove launcher
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = null;

var result = window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});

(result === null).should.equal(true);
});

it('should return hashed attributes from launcher', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);

const result = await window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});

result.should.deepEqual({
'test-attribute': 'hashed-value',
});
});
});

describe('#selectPlacements', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
Expand Down