Wrapper for the MoEngage Web SDK
npm install react-moengage
A convenient wrapper around the MoEngage Web SDK for React.
``bash`
npm install --save react-moengage
`javascript
import MoEngage from "react-moengage"
MoEngage.init("MOENGAGE_APP_ID", {
debugLogs: 0,
swPath: "/customSw.js",
})
MoEngage.trackEvent("PurchaseMade", { ...data })
`
All methods are camel-cased versions of those specified in the MoEngage SDK documentation.
`javascript
MoEngage.trackEvent("PurchaseMade", { value: 0.5 })
MoEngage.addFirstName("Dominick")
MoEngage.addLastName("Cobb")
MoEngage.addEmail("dom@level5.com")
MoEngage.addMobile("+12399999999")
Moengage.addUserAttribute("replenishment_date", new Date(2021, 4, 30))
`
The moe object is also accessible directly if need-be:
`javascript
const moe = MoEngage.moe
moe.onsite.registerCallback(...args)
moe.onsite.getData(...args)
`
By default debugLogs is set to 1 (test mode).
As in the MoEngage SDK documentation, set debugLogs to 0 in order to push data to your LIVE environment. Set it to 1 in order to push data to your TEST environment & enable logging to the console.
Please note in order to initialise this library, the MoEngage SDK relies on the window object and therefore will only work in the browser. If server-side rendering, it is recommended to call init() in somewhere like componentDidMount() or useEffect(..., []).
`javascript
import MoEngage from "react-moengage"
class MyComponent extends React.Component {
componentDidMount() {
const options = { ... }
MoEngage.init("MOENGAGE_APP_ID", options)
}
}
``