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
153 changes: 151 additions & 2 deletions src/Rokt-Kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,139 @@ var constructor = function () {
self.userAttributes = {};
self.testHelpers = null;
self.placementEventMappingLookup = {};
self.placementEventAttributeMappingLookup = {};
self.eventQueue = [];

function getEventAttributeValue(event, attributeKey) {
var attributes = event && event.EventAttributes;
if (!attributes) {
return null;
}

if (typeof attributes[attributeKey] === 'undefined') {
return null;
}

return attributes[attributeKey];
}

function checkAttributeCondition(condition, actualValue) {
if (!condition || typeof condition.operator !== 'string') {
return false;
}

var operator = condition.operator.toLowerCase();
var expectedValue = condition.attributeValue;

if (operator === 'exists') {
return actualValue !== null;
}

if (operator === 'equals') {
return actualValue === expectedValue;
}

if (operator === 'contains') {
if (
typeof actualValue !== 'string' ||
typeof expectedValue !== 'string'
) {
return false;
}
return actualValue.indexOf(expectedValue) !== -1;
}

return false;
}

function checkMappedKeyRule(event, rule) {
if (!rule || typeof rule.attribute !== 'string') {
return false;
}

var conditions = rule.conditions;
if (!Array.isArray(conditions)) {
return false;
}

if (conditions.length === 0) {
return true;
}

var actualValue = getEventAttributeValue(event, rule.attribute);
for (var i = 0; i < conditions.length; i++) {
if (!checkAttributeCondition(conditions[i], actualValue)) {
return false;
}
}

return true;
}

function generateMappedEventAttributeLookup(
placementEventAttributeMapping
) {
var mappedEventAttributes = {};
if (!Array.isArray(placementEventAttributeMapping)) {
return mappedEventAttributes;
}
for (var i = 0; i < placementEventAttributeMapping.length; i++) {
var mapping = placementEventAttributeMapping[i];
if (
!mapping ||
typeof mapping.value !== 'string' ||
typeof mapping.map !== 'string'
) {
continue;
}

if (!mappedEventAttributes[mapping.value]) {
mappedEventAttributes[mapping.value] = [];
}

mappedEventAttributes[mapping.value].push({
attribute: mapping.map,
conditions: Array.isArray(mapping.conditions)
? mapping.conditions
: [],
});
}
return mappedEventAttributes;
}

function applyPlacementEventAttributeMapping(event) {
if (
!self.placementEventAttributeMappingLookup ||
isEmpty(self.placementEventAttributeMappingLookup)
) {
return;
}

var mappedKeys = Object.keys(self.placementEventAttributeMappingLookup);
for (var i = 0; i < mappedKeys.length; i++) {
var mappedKey = mappedKeys[i];
var rulesForMappedKey =
self.placementEventAttributeMappingLookup[mappedKey];
if (!rulesForMappedKey || !rulesForMappedKey.length) {
continue;
}

// Require ALL rules for the same key to match (AND across rules).
var allMatch = true;
for (var j = 0; j < rulesForMappedKey.length; j++) {
if (!checkMappedKeyRule(event, rulesForMappedKey[j])) {
allMatch = false;
break;
}
}
if (!allMatch) {
continue;
}

window.mParticle.Rokt.setLocalSessionAttribute(mappedKey, true);
}
}

/**
* Generates the Rokt launcher script URL with optional domain override and extensions
* @param {string} domain - The CNAME domain to use for overriding the launcher url
Expand Down Expand Up @@ -92,6 +223,12 @@ var constructor = function () {
placementEventMapping
);

var placementEventAttributeMapping = parseSettingsString(
settings.placementEventAttributeMapping
);
self.placementEventAttributeMappingLookup =
generateMappedEventAttributeLookup(placementEventAttributeMapping);

// Set dynamic OTHER_IDENTITY based on server settings
// Convert to lowercase since server sends TitleCase (e.g., 'Other' -> 'other')
if (settings.hashedEmailUserIdentityType) {
Expand All @@ -115,6 +252,8 @@ var constructor = function () {
hashEventMessage: hashEventMessage,
parseSettingsString: parseSettingsString,
generateMappedEventLookup: generateMappedEventLookup,
generateMappedEventAttributeLookup:
generateMappedEventAttributeLookup,
};
attachLauncher(accountId, launcherOptions);
return;
Expand Down Expand Up @@ -172,13 +311,18 @@ var constructor = function () {

function returnLocalSessionAttributes() {
if (
isEmpty(self.placementEventMappingLookup) ||
!window.mParticle.Rokt ||
typeof window.mParticle.Rokt.getLocalSessionAttributes !==
'function'
) {
return {};
}
if (
isEmpty(self.placementEventMappingLookup) &&
isEmpty(self.placementEventAttributeMappingLookup)
) {
return {};
}
return window.mParticle.Rokt.getLocalSessionAttributes();
}

Expand Down Expand Up @@ -306,12 +450,17 @@ var constructor = function () {
}

if (
isEmpty(self.placementEventMappingLookup) ||
typeof window.mParticle.Rokt.setLocalSessionAttribute !== 'function'
) {
return;
}

applyPlacementEventAttributeMapping(event);

if (isEmpty(self.placementEventMappingLookup)) {
return;
}

var hashedEvent = hashEventMessage(
event.EventDataType,
event.EventCategory,
Expand Down
Loading