-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added for moengage and tested #153
Changes from 5 commits
80cbf2c
65cc81d
0635e15
b0ee668
ceef7d7
ed61f49
2443122
bf4ef41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import each from "@ndhoule/each"; | ||
import logger from "../../utils/logUtil"; | ||
|
||
// custom traits mapping context.traits --> moengage properties | ||
const traitsMap = { | ||
firstName: "first_name", | ||
lastName: "last_name", | ||
firstname: "first_name", | ||
lastname: "last_name", | ||
email: "email", | ||
phone: "mobile", | ||
name: "user_name", | ||
username: "user_name", | ||
gender: "gender", | ||
birthday: "birthday", | ||
id: null, | ||
}; | ||
class MoEngage { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please include comments involving the destination documentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
constructor(config, analyticsinstance) { | ||
this.apiId = config.apiId; | ||
this.debug = config.debug; | ||
this.region = config.region; | ||
this.name = "MoEngage"; | ||
this.analyticsinstance = analyticsinstance; | ||
} | ||
|
||
init() { | ||
const self = this; | ||
logger.debug("===in init MoEnagage==="); | ||
// loading the script for moengage web sdk | ||
/* eslint-disable */ | ||
(function (i, s, o, g, r, a, m, n) { | ||
i.moengage_object = r; | ||
var t = {}; | ||
var q = function (f) { | ||
return function () { | ||
(i.moengage_q = i.moengage_q || []).push({ f, a: arguments }); | ||
}; | ||
}; | ||
var f = [ | ||
"track_event", | ||
"add_user_attribute", | ||
"add_first_name", | ||
"add_last_name", | ||
"add_email", | ||
"add_mobile", | ||
"add_user_name", | ||
"add_gender", | ||
"add_birthday", | ||
"destroy_session", | ||
"add_unique_user_id", | ||
"moe_events", | ||
"call_web_push", | ||
"track", | ||
"location_type_attribute", | ||
]; | ||
var h = { onsite: ["getData", "registerCallback"] }; | ||
for (var k in f) { | ||
t[f[k]] = q(f[k]); | ||
} | ||
for (var k in h) | ||
for (var l in h[k]) { | ||
null == t[k] && (t[k] = {}), (t[k][h[k][l]] = q(k + "." + h[k][l])); | ||
} | ||
a = s.createElement(o); | ||
m = s.getElementsByTagName(o)[0]; | ||
a.async = 1; | ||
a.src = g; | ||
m.parentNode.insertBefore(a, m); | ||
i.moe = | ||
i.moe || | ||
function () { | ||
n = arguments[0]; | ||
return t; | ||
}; | ||
a.onload = function () { | ||
if (n) { | ||
i[r] = moe(n); | ||
} | ||
}; | ||
})( | ||
window, | ||
document, | ||
"script", | ||
document.location.protocol === "https:" | ||
? "https://cdn.moengage.com/webpush/moe_webSdk.min.latest.js" | ||
: "http://cdn.moengage.com/webpush/moe_webSdk.min.latest.js", | ||
"Moengage" | ||
); | ||
/* eslint-enable */ | ||
|
||
// setting the region if us then not needed. | ||
if (this.region !== "US") { | ||
self.moeClient = window.moe({ | ||
app_id: this.apiId, | ||
debug_logs: this.debug ? 1 : 0, | ||
cluster: this.region === "EU" ? "eu" : "in", | ||
}); | ||
} else { | ||
self.moeClient = window.moe({ | ||
app_id: this.apiId, | ||
debug_logs: this.debug ? 1 : 0, | ||
}); | ||
} | ||
this.initialUserId = this.analyticsinstance.userId; | ||
} | ||
|
||
isLoaded = () => { | ||
logger.debug("in MoEngage isLoaded"); | ||
return !!window.moeBannerText; | ||
}; | ||
|
||
isReady = () => { | ||
logger.debug("in MoEngage isReady"); | ||
return !!window.moeBannerText; | ||
}; | ||
|
||
track(rudderElement) { | ||
logger.debug("inside 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"); | ||
return; | ||
} | ||
const { event, properties, userId } = rudderElement.message; | ||
if (userId) { | ||
if (this.initialUserId !== userId) { | ||
this.reset(); | ||
} | ||
} | ||
// track event : https://docs.moengage.com/docs/tracking-events | ||
if (!event) { | ||
logger.error("Event name not present"); | ||
return; | ||
} | ||
if (properties) { | ||
this.moeClient.track_event(event, properties); | ||
} else { | ||
this.moeClient.track_event(event); | ||
} | ||
} | ||
|
||
reset() { | ||
logger.debug("inside reset"); | ||
// reset the user id | ||
this.initialUserId = this.analyticsinstance.userId; | ||
this.moeClient.destroy_session(); | ||
} | ||
|
||
identify(rudderElement) { | ||
const self = this; | ||
const { userId } = rudderElement.message; | ||
let traits = null; | ||
if (rudderElement.message.context) { | ||
traits = rudderElement.message.context.traits; | ||
} | ||
// check if user id is same or not | ||
if (this.initialUserId !== userId) { | ||
this.reset(); | ||
} | ||
// if user is present map | ||
if (userId) { | ||
this.moeClient.add_unique_user_id(userId); | ||
} | ||
|
||
// track user attributes : https://docs.moengage.com/docs/tracking-web-user-attributes | ||
if (traits) { | ||
each(function add(value, key) { | ||
// check if name is present | ||
if (key === "name") { | ||
self.moeClient.add_user_name(value); | ||
} | ||
if (Object.prototype.hasOwnProperty.call(traitsMap, key)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can the below 3 loops be merged into one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but we have to loop through traits too right? to send the remaining attributes? |
||
const method = `add_${traitsMap[key]}`; | ||
self.moeClient[method](value); | ||
} else { | ||
self.moeClient.add_user_attribute(key, value); | ||
} | ||
}, traits); | ||
} | ||
} | ||
} | ||
|
||
export default MoEngage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import MoEngage from "./browser"; | ||
|
||
export default MoEngage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we're supporting camelCase, should we support
userName
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done