Skip to content
Open
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
48 changes: 48 additions & 0 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

var name = 'Rokt';
var moduleId = 181;
var EVENT_NAME_SELECT_PLACEMENTS = 'selectplacements';

var constructor = function () {
var self = this;
Expand Down Expand Up @@ -199,10 +200,12 @@ var constructor = function () {
* @param {Object} options - The options object for selecting placements containing:
* - identifier {string}: The placement identifier
* - attributes {Object}: Optional attributes to merge with existing attributes
* - debugAttributes {Object}: Original attributes passed by developer
* @returns {Promise<void>} A Promise resolving to the Rokt launcher's selectPlacements method with processed attributes
*/
function selectPlacements(options) {
var attributes = (options && options.attributes) || {};
var debugAttributes = (options && options.debugAttributes) || {};
var placementAttributes = mergeObjects(self.userAttributes, attributes);

var filters = self.filters || {};
Expand Down Expand Up @@ -255,9 +258,50 @@ var constructor = function () {
attributes: selectPlacementsAttributes,
});

// Log custom event for selectPlacements call
logSelectPlacementsEvent(debugAttributes, selectPlacementsAttributes);

return self.launcher.selectPlacements(selectPlacementsOptions);
}

/**
* Logs a custom event when selectPlacements is called
* This enables visibility and troubleshooting
* @param {Object} debugAttributes - The attributes passed by the developer
* @param {Object} selectPlacementsAttributes - The final merged attributes sent to Rokt
*/
function logSelectPlacementsEvent(
debugAttributes,
selectPlacementsAttributes
) {
if (
!window.mParticle ||
typeof window.mParticle.logEvent !== 'function'
) {
return;
}
// Event type 8 corresponds to "Other" in mParticle's EventType enum
var EVENT_TYPE_OTHER =
(window.mParticle.EventType && window.mParticle.EventType.Other) ||
8;

// Build event attributes with both passed and final attributes as JSON strings
// debugAttributes: attributes passed by the developer
// selectPlacementsAttributes: final attributes sent to selectPlacements
var eventAttributes = {
debugAttributes: stringifyIfObject(debugAttributes),
selectPlacementsAttributes: stringifyIfObject(
selectPlacementsAttributes
),
};

window.mParticle.logEvent(
EVENT_NAME_SELECT_PLACEMENTS,
EVENT_TYPE_OTHER,
eventAttributes
);
}

/**
* Enables optional Integration Launcher extensions before selecting placements
* @param {string} extensionName - Name of the extension to enable
Expand Down Expand Up @@ -580,6 +624,10 @@ function isEmpty(value) {
return value == null || !(Object.keys(value) || value).length;
}

function stringifyIfObject(value) {
return isObject(value) ? JSON.stringify(value) : '{}';
}

if (window && window.mParticle && window.mParticle.addForwarder) {
window.mParticle.addForwarder({
name: name,
Expand Down
147 changes: 147 additions & 0 deletions test/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ describe('Rokt Forwarder', () => {
mParticle.generateHash = function (input) {
return 'hashed-<' + input + '>-value';
};
// Mock for logEvent to capture custom event logging
mParticle.loggedEvents = [];
mParticle.logEvent = function (eventName, eventType, eventAttributes) {
mParticle.loggedEvents.push({
eventName: eventName,
eventType: eventType,
eventAttributes: eventAttributes,
});
};
// -------------------START EDITING BELOW:-----------------------
var MockRoktForwarder = function () {
var self = this;
Expand Down Expand Up @@ -734,6 +743,7 @@ describe('Rokt Forwarder', () => {
window.mParticle.Rokt.attachKitCalled = true;
return Promise.resolve();
};
mParticle.loggedEvents = [];
window.mParticle.Rokt.setLocalSessionAttribute = function (
key,
value
Expand Down Expand Up @@ -2495,6 +2505,143 @@ describe('Rokt Forwarder', () => {
);
});
});

describe('#logSelectPlacementsEvent', () => {
it('should log a custom event with debugAttributes and selectPlacementsAttributes', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{
'cached-user-attr': 'cached-value',
}
);

await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
'new-attr': 'new-value',
},
debugAttributes: {
'original-attr': 'original-value',
},
});

mParticle.loggedEvents.length.should.equal(1);
mParticle.loggedEvents[0].eventName.should.equal(
'selectplacements'
);
mParticle.loggedEvents[0].eventType.should.equal(8); // EventType.Other

const eventAttributes =
mParticle.loggedEvents[0].eventAttributes;
eventAttributes.should.have.property('debugAttributes');
eventAttributes.should.have.property(
'selectPlacementsAttributes'
);

// debugAttributes should contain original attributes from developer
const debugAttrs = JSON.parse(eventAttributes.debugAttributes);
debugAttrs.should.deepEqual({
'original-attr': 'original-value',
});
});

it('should include selectPlacementsAttributes with merged user attributes, identities, and mpid', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{
'cached-user-attr': 'cached-value',
}
);

await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
'new-attr': 'new-value',
},
debugAttributes: {
'new-attr': 'new-value',
},
});

const eventAttributes =
mParticle.loggedEvents[0].eventAttributes;
const selectPlacementsAttrs = JSON.parse(
eventAttributes.selectPlacementsAttributes
);

// selectPlacementsAttributes should include merged attributes and mpid
selectPlacementsAttrs.should.have.property('mpid', '123');
selectPlacementsAttrs.should.have.property(
'new-attr',
'new-value'
);
selectPlacementsAttrs.should.have.property(
'cached-user-attr',
'cached-value'
);
});

it('should handle empty debugAttributes', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);

await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
attr: 'value',
},
});

mParticle.loggedEvents.length.should.equal(1);
const eventAttributes =
mParticle.loggedEvents[0].eventAttributes;
const debugAttrs = JSON.parse(eventAttributes.debugAttributes);
debugAttrs.should.deepEqual({});
});

it('should skip logging when mParticle.logEvent is not available', async () => {
var originalLogEvent = window.mParticle.logEvent;
window.mParticle.logEvent = undefined;

await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);

await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
attr: 'value',
},
});

window.Rokt.selectPlacementsCalled.should.equal(true);
mParticle.loggedEvents.length.should.equal(0);
window.mParticle.logEvent = originalLogEvent;
});
});
});

describe('#use', () => {
Expand Down
Loading