Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
79 changes: 76 additions & 3 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ function attachLauncher(accountId, sandboxMode) {
.then(function (launcher) {
// Assign the launcher to a global variable for later access
window.Rokt.currentLauncher = launcher;
window.mParticle.Rokt.attachLauncher(launcher);

// Locally cache the launcher and filters
self.launcher = launcher;
self.filters = window.mParticle.Rokt.filters;

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

self.isInitialized = true;
})
Expand All @@ -41,10 +47,21 @@ var constructor = function () {
self.name = name;
self.moduleId = moduleId;
self.isInitialized = false;

function initForwarder(settings, service, testMode) {
self.launcher = null;
self.filters = {};
self.filteredUser = {};
self.userAttributes = {};

function initForwarder(
settings,
service,
testMode,
trackerId,
filteredUserAttributes
) {
var accountId = settings.accountId;
var sandboxMode = window.mParticle.getEnvironment() === 'development';
self.userAttributes = filteredUserAttributes;

if (testMode) {
attachLauncher(accountId, sandboxMode);
Expand Down Expand Up @@ -86,7 +103,51 @@ var constructor = function () {
}
}

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);

var userAttributeFilters = self.filters.userAttributeFilters;
var filteredAttributes = self.filters.filterUserAttributes(
placementAttributes,
userAttributeFilters
);

self.userAttributes = filteredAttributes;

var selectPlacementsOptions = mergeObjects(options, {
attributes: filteredAttributes,
});

self.launcher.selectPlacements(selectPlacementsOptions);
}

function onUserIdentified(filteredUser) {
self.filteredUser = filteredUser;
self.userAttributes = filteredUser.getAllUserAttributes();
}

function setUserAttribute(key, value) {
self.userAttributes[key] = value;
}

function removeUserAttribute(key) {
delete self.userAttributes[key];
}

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

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

function getId() {
Expand Down Expand Up @@ -128,6 +189,18 @@ function isObject(val) {
);
}

function mergeObjects() {
var resObj = {};
for (var i = 0; i < arguments.length; i += 1) {
var obj = arguments[i],
keys = Object.keys(obj);
for (var j = 0; j < keys.length; j += 1) {
resObj[keys[j]] = obj[keys[j]];
}
}
return resObj;
}

if (window && window.mParticle && window.mParticle.addForwarder) {
window.mParticle.addForwarder({
name: name,
Expand Down
8 changes: 4 additions & 4 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ describe('Rokt Forwarder', () => {
it('should initialize the Rokt Web SDK', async () => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachLauncherCalled = false;
window.mParticle.Rokt.attachLauncher = async () => {
window.mParticle.Rokt.attachLauncherCalled = true;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async () => {
window.mParticle.Rokt.attachKitCalled = true;
Promise.resolve();
};

Expand All @@ -105,7 +105,7 @@ describe('Rokt Forwarder', () => {
window.Rokt.createLauncherCalled.should.equal(true);

await waitForCondition(
() => window.mParticle.Rokt.attachLauncherCalled === true
() => window.mParticle.Rokt.attachKitCalled === true
);
});
});