Skip to content

Commit

Permalink
chore: resolved tech debt for microsoft clarity, mixpanel and moengage
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Oct 9, 2023
1 parent 8c2848c commit 211e9ea
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { NAME } from '@rudderstack/analytics-js-common/constants/integrations/MicrosoftClarity/constants';
import {
NAME,
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/MicrosoftClarity/constants';
import Logger from '../../utils/logger';
/* eslint-disable class-methods-use-this */

Expand Down Expand Up @@ -26,26 +29,25 @@ class MicrosoftClarity {
}

init() {
logger.debug('===In init Microsoft Clarity===');
this.loadScript();
}

isLoaded() {
logger.debug('===In isLoaded Microsoft Clarity===');
logger.debug(`In isLoaded ${DISPLAY_NAME}`);
return !!window.clarity;
}

isReady() {
logger.debug('===In isReady Microsoft Clarity===');
logger.debug(`In isReady ${DISPLAY_NAME}`);
return !!window.clarity;
}

identify(rudderElement) {
logger.debug('===In Microsoft Clarity Identify===');
logger.debug(`In ${DISPLAY_NAME} identify`);
const { message } = rudderElement;
const { userId, context } = message;
if (!userId) {
logger.error('[Microsoft Clarity] :: userId is required for an identify call');
logger.error(`${DISPLAY_NAME} : userId is required for an identify call`);
return;
}
let sessionId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable class-methods-use-this */
import get from 'get-value';
import { logger } from '@rudderstack/analytics-js-common/v1.1/utils/logUtil';
import { NAME } from '@rudderstack/analytics-js-common/constants/integrations/Mixpanel/constants';
import {
NAME,
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/Mixpanel/constants';
import Logger from '../../utils/logger';
import { pick, removeUndefinedAndNullValues, isNotEmpty } from '../../utils/commonUtils';
import {
mapTraits,
Expand All @@ -15,6 +18,8 @@ import {
} from './util';
import { loadNativeSdk } from './nativeSdkLoader';

const logger = new Logger(NAME);

class Mixpanel {
constructor(config, analytics, destinationInfo) {
if (analytics.logLevel) {
Expand Down Expand Up @@ -60,7 +65,6 @@ class Mixpanel {
}

init() {
logger.debug('===in init Mixpanel===');
// eslint-disable-next-line no-var
loadNativeSdk();
const options = {
Expand Down Expand Up @@ -89,14 +93,14 @@ class Mixpanel {
}

isLoaded() {
logger.debug('in Mixpanel isLoaded');
logger.debug(`In isLoaded ${DISPLAY_NAME}`);
logger.debug(!!(window.mixpanel && window.mixpanel.config));
window.mixpanel.register({ mp_lib: 'Rudderstack: web' });
return !!window?.mixpanel?.config;
}

isReady() {
logger.debug('in Mixpanel isReady');
logger.debug(`In isReady ${DISPLAY_NAME}`);
return !!window?.mixpanel?.config;
}

Expand All @@ -105,7 +109,7 @@ class Mixpanel {
* @param {*} rudderElement
*/
identify(rudderElement) {
logger.debug('in Mixpanel identify');
logger.debug(`In ${DISPLAY_NAME} identify`);

let peopleProperties = parseConfigArray(this.peopleProperties, 'property');
peopleProperties = extendTraits(peopleProperties);
Expand Down Expand Up @@ -174,7 +178,7 @@ class Mixpanel {
* @param {*} rudderElement
*/
page(rudderElement) {
logger.debug('in Mixpanel page');
logger.debug(`In ${DISPLAY_NAME} page`);
const { name, properties } = rudderElement.message;
const { category } = properties;
// consolidated Page Calls
Expand Down Expand Up @@ -207,7 +211,7 @@ class Mixpanel {
* @param {*} rudderElement
*/
track(rudderElement) {
logger.debug('in Mixpanel track');
logger.debug(`In ${DISPLAY_NAME} track`);
const { message } = rudderElement;
const eventIncrements = parseConfigArray(this.eventIncrements, 'property');
const propIncrements = parseConfigArray(this.propIncrements, 'property');
Expand Down Expand Up @@ -272,18 +276,18 @@ class Mixpanel {
* @param {*} rudderElement
*/
group(rudderElement) {
logger.debug('in Mixpanel group');
logger.debug(`In ${DISPLAY_NAME} group`);
const { userId, groupId, traits } = rudderElement.message;
if (!userId) {
logger.debug('===Mixpanel: valid userId is required for group===');
logger.debug(`${DISPLAY_NAME} : valid userId is required for group`);
return;
}
if (!groupId) {
logger.debug('===Mixpanel: valid groupId is required for group===');
logger.debug(`${DISPLAY_NAME} : valid groupId is required for group`);
return;
}
if (!this.groupKeySettings || this.groupKeySettings.length === 0) {
logger.debug('===Mixpanel: groupIdentifierTraits is required for group===');
logger.debug(`${DISPLAY_NAME} : groupIdentifierTraits is required for group`);
return;
}
/**
Expand All @@ -303,25 +307,25 @@ class Mixpanel {
* @param {*} rudderElement
*/
alias(rudderElement) {
logger.debug('in Mixpanel alias');
logger.debug(`In ${DISPLAY_NAME} alias`);
if (this.identityMergeApi === 'simplified') {
logger.debug("===Mixpanel: Alias call is deprecated in 'Simplified ID Merge'===");
logger.debug(`${DISPLAY_NAME} : Alias call is deprecated in 'Simplified ID Merge'`);
return;
}

const { previousId, userId } = rudderElement.message;
const newId = userId;
if (!previousId) {
logger.debug('===Mixpanel: previousId is required for alias call===');
logger.debug(`${DISPLAY_NAME} : previousId is required for alias call`);
return;
}
if (!newId) {
logger.debug('===Mixpanel: userId is required for alias call===');
logger.debug(`${DISPLAY_NAME} : userId is required for alias call`);
return;
}

if (window.mixpanel.get_distinct_id && window.mixpanel.get_distinct_id() === newId) {
logger.debug('===Mixpanel: userId is same as previousId. Skipping alias ===');
logger.debug(`${DISPLAY_NAME} : userId is same as previousId. Skipping alias`);
return;
}
window.mixpanel.alias(newId, previousId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-prototype-builtins */
import { logger } from '@rudderstack/analytics-js-common/v1.1/utils/logUtil';
import {
NAME,
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/Amplitude/constants';
import Logger from '../../utils/logger';
import { getDefinedTraits, extractCustomFields } from '../../utils/utils';

const logger = new Logger(NAME);

const keysToExtract = ['context.traits'];
const exclusionKeys = [
'email',
Expand Down Expand Up @@ -45,14 +51,14 @@ const formatTraits = message => {
try {
outgoingTraits = extractCustomFields(message, outgoingTraits, keysToExtract, exclusionKeys);
} catch (err) {
logger.debug(`Error occured at extractCustomFields ${err}`);
logger.debug(`${DISPLAY_NAME} : Error occured at extractCustomFields ${err}`);
}
return outgoingTraits;
};

const parseConfigArray = (arr, key) => {
if (!arr) {
logger.debug('===Mixpanel: arr is undefined or null===');
logger.debug(`${DISPLAY_NAME} : arr is undefined or null`);
return;
}
// eslint-disable-next-line consistent-return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* eslint-disable class-methods-use-this */
import each from '@ndhoule/each';
import { logger } from '@rudderstack/analytics-js-common/v1.1/utils/logUtil';
import { NAME } from '@rudderstack/analytics-js-common/constants/integrations/MoEngage/constants';
import {
NAME,
DISPLAY_NAME,
} from '@rudderstack/analytics-js-common/constants/integrations/MoEngage/constants';
import Logger from '../../utils/logger';
import { loadNativeSdk } from './nativeSdkLoader';

const logger = new Logger(NAME);

// custom traits mapping context.traits --> moengage properties
const traitsMap = {
firstName: 'first_name',
Expand Down Expand Up @@ -37,18 +42,16 @@ class MoEngage {
}

init() {
const self = this;
logger.debug('===in init MoEngage===');
loadNativeSdk();
// setting the region if us then not needed.
if (this.region !== 'US') {
self.moeClient = window.moe({
this.moeClient = window.moe({
app_id: this.apiId,
debug_logs: this.debug ? 1 : 0,
cluster: this.region === 'EU' ? 'eu' : 'in',
});
} else {
self.moeClient = window.moe({
this.moeClient = window.moe({
app_id: this.apiId,
debug_logs: this.debug ? 1 : 0,
});
Expand All @@ -57,20 +60,20 @@ class MoEngage {
}

isLoaded() {
logger.debug('in MoEngage isLoaded');
logger.debug(`In isLoaded ${DISPLAY_NAME}`);
return !!window.moeBannerText;
}

isReady() {
logger.debug('in MoEngage isReady');
logger.debug(`In isReady ${DISPLAY_NAME}`);
return !!window.moeBannerText;
}

track(rudderElement) {
logger.debug('inside track');
logger.debug(`In ${DISPLAY_NAME} track`);
// Check if the user id is same as previous session if not a new session will start
if (!rudderElement.message) {
logger.error('Payload not correct');
logger.error(`${DISPLAY_NAME} : Payload not correct`);
return;
}
const { event, properties, userId } = rudderElement.message;
Expand All @@ -79,7 +82,7 @@ class MoEngage {
}
// track event : https://docs.moengage.com/docs/tracking-events
if (!event) {
logger.error('Event name not present');
logger.error(`${DISPLAY_NAME} : Event name is not present`);
return;
}
if (properties) {
Expand All @@ -90,14 +93,13 @@ class MoEngage {
}

reset() {
logger.debug('inside reset');
logger.debug(`${DISPLAY_NAME} : inside reset`);
// reset the user id
this.initialUserId = this.analytics.getUserId();
this.moeClient.destroy_session();
}

identify(rudderElement) {
const self = this;
const { userId, context } = rudderElement.message;
let traits = null;
if (context) {
Expand All @@ -117,13 +119,13 @@ class MoEngage {
each((value, key) => {
// check if name is present
if (key === 'name') {
self.moeClient.add_user_name(value);
this.moeClient.add_user_name(value);
}
if (Object.prototype.hasOwnProperty.call(traitsMap, key)) {
const method = `add_${traitsMap[key]}`;
self.moeClient[method](value);
this.moeClient[method](value);
} else {
self.moeClient.add_user_attribute(key, value);
this.moeClient.add_user_attribute(key, value);
}
}, traits);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LOAD_ORIGIN } from '@rudderstack/analytics-js-common/v1.1/utils/constants';

function loadNativeSdk() {
// loading the script for moengage web sdk
(function (i, s, o, g, r, a, m, n) {
i.moengage_object = r;
var t = {};
Expand Down

0 comments on commit 211e9ea

Please sign in to comment.