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
78 changes: 72 additions & 6 deletions src/MixpanelEventForwarder.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,74 @@ var constructor = function () {
if (!testMode) {
renderSnippet();
}
mixpanel.init(
settings.token,
// Build init options object
var initOptions = {
api_host: forwarderSettings.baseUrl,
};

// Session Replay boolean settings
var boolSettings = [
{ key: 'recordHeatmapData', mappedKey: 'record_heatmap_data' },
{ key: 'autocapture', mappedKey: 'autocapture' },
{ key: 'recordCanvas', mappedKey: 'record_canvas' },
];

// Session Replay numeric settings
var numericSettings = [
{
api_host: forwarderSettings.baseUrl,
key: 'recordSessionsPercent',
mappedKey: 'record_sessions_percent',
},
'mparticle'
);
{
key: 'recordIdleTimeoutMs',
mappedKey: 'record_idle_timeout_ms',
},
{ key: 'recordMaxMs', mappedKey: 'record_max_ms' },
{ key: 'recordMinMs', mappedKey: 'record_min_ms' },
];

// Session Replay string settings
var stringSettings = [
{
key: 'recordMaskTextSelector',
mappedKey: 'record_mask_text_selector',
},
{
key: 'recordBlockSelector',
mappedKey: 'record_block_selector',
},
{ key: 'recordBlockClass', mappedKey: 'record_block_class' },
{
key: 'recordMaskTextClass',
mappedKey: 'record_mask_text_class',
},
];

// Process boolean settings
boolSettings.forEach(function (setting) {
if (forwarderSettings[setting.key] != null) {
initOptions[setting.mappedKey] =
forwarderSettings[setting.key] === 'True';
}
});

// Process numeric settings
numericSettings.forEach(function (setting) {
var numericValue = parseIntSafe(forwarderSettings[setting.key]);
if (numericValue !== undefined) {
initOptions[setting.mappedKey] = numericValue;
}
});

// Process string settings
stringSettings.forEach(function (setting) {
if (forwarderSettings[setting.key]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you're not checkign != null here?

If this was missed, we could make a simple utility function to make this a little readable.

function hasValue(obj, key) {
    return obj[key] != null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add != null for string settings as it would allow empty strings like recordMaskTextSelector: '' to pass through to Mixpanel, which would break test like "should ignore empty string values for privacy selectors".

initOptions[setting.mappedKey] =
forwarderSettings[setting.key];
}
});

mixpanel.init(settings.token, initOptions, 'mparticle');

isInitialized = true;

Expand Down Expand Up @@ -193,7 +254,7 @@ var constructor = function () {
// When mParticle identifies a user, because the user might
// actually be anonymous, we only want to send an
// identify request to Mixpanel if the user is
// actually known. If a user has any user identities, they are
// actually known. If a user has any user identities, they are
// considered to be "known" users.
var userIdentities = getUserIdentities(user);

Expand Down Expand Up @@ -394,6 +455,11 @@ function isObject(val) {
);
}

function parseIntSafe(value) {
var n = parseInt(value, 10);
return isNaN(n) ? undefined : n;
}

if (typeof window !== 'undefined') {
if (window && window.mParticle && window.mParticle.addForwarder) {
window.mParticle.addForwarder({
Expand Down
Loading