diff --git a/src/Rokt-Kit.js b/src/Rokt-Kit.js index 31fdacb..cc5c915 100644 --- a/src/Rokt-Kit.js +++ b/src/Rokt-Kit.js @@ -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; }) @@ -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); @@ -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() { @@ -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, diff --git a/test/src/tests.js b/test/src/tests.js index f25ac62..cf0303e 100644 --- a/test/src/tests.js +++ b/test/src/tests.js @@ -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(); }; @@ -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 ); }); });