Mergn Web SDK is available as an npm package.
npm install --save mergn-webapp-sdk@latest
import mergn from 'mergn-webapp-sdk';
<script src="https://cdn.jsdelivr.net/npm/mergn-webapp-sdk@latest/dist/index.global.js"></script>
mergn.init('Api_Key', options?: {
swPath?: string || '/'; // custom service worker path. Defaults to root '/'
}); // Replace with values applicable to you. Refer below
Api_Key
(mandatory): This value is given by Mergn Team.
options
(optional): Additional initialization parameters.
mergn-sw.js
(mandatory): A service worker file given by Mergn Team to enable features like web push notifications. This file must be kept in the root of the project like public
folder in case of React. Or a custom path given in swPath
option.
Events monitor specific user actions within your app or website, such as app launch, product view, song play, photo share, purchase, or item favoring.
mergn.recordEvent(eventName: string, eventProperties?: { eventProperty: string; value: string | number }[];
): Promise<void>
Example:
// event without properties
mergn.recordEvent('Product viewed')
// event with properties
mergn.recordEvent('Product viewed', [{ eventProperty: 'Mens Accessories', value: 59.99 }])
Note: In most cases, it's recommended to use recordEvent without awaiting the returned Promise. This allows your application to continue running without waiting for the event recording to complete.
Records or updates the attributes of user such as name, age or address.
mergn.recordAttribute(attributeName: string, value: string | number): Promise<void>
mergn.recordAttribute('Age', 35);
Note: In most cases, it's recommended to use recordAttribute without awaiting the returned Promise. This allows your application to continue running without waiting for the attribute recording to complete.
Use this method for setting identity.
await mergn.login('Unique_Identity'): Promise<void>;
Unique_Identity
(mandatory): This value is the customer's unique identity in your database like Id
or Email
.
Note: it's recommended to use login with awaiting the returned Promise. This allows your application to set identity successfully which would later be used for recording events and attributes.
Use this method where user is logged out successfully to record logout event.
mergn.logout();
mergn-webapp-sdk
automatically queue events in case of internet disruption, and sends them once internet is restored while the user is on the website
max event queue limit is 50, after which events are ignored
! MUST DISABLE ON PRODUCTION
To show logs while debugging
mergn.setDebugLevel(LOG_LEVEL);
LOG_LEVEL
(mandatory): Enter corresponding value:
NONE = 0, INFO = 1, ERROR = 2, ALL = 3,
Default is 0