MoonMail JS Overview

Our JavaScript SDK makes it easy to bring data from your app or website into MoonMail, add new or update existing contacts, trigger event-based campaigns, and more.

Get your account ID

If you don't have a MoonMail account already, you’ll need to create one in order to use the SDK. Navigate to the general settings of your account and copy the Account ID

Setup moonmail.js

You need to include moonmail.js on your website page by either adding the script tag to the head of your HTML file, or importing it from the moonmail-js-sdk module:

<script src="https://js.moonmail.io/sdk/latest/moonmail.umd.js"></script>

Initialize

You can provide a default contact information. This contact information will be used in subsequent calls.

var moonmail = MoonMail.init({
accountId: 'YOUR_ACCOUNT_ID',
contact: {
address: 'email@example.com',
attributes: {
hobbies: ['piano', 'hiking']
}
}
});

Create or update contact

You can add new contact or updates existing with a given address. Pass additional attributes and metrics to provide useful information that can be used for segmentation.

moonmail.updateContact({
attributes: {
hobbies: ['hiking'],
metrics: {
age: 27
}
}
});

Note that this code will update default contact.

note

Contact metric values must be a Number type such as a float or integer. Contact attribute values must have the type String or be an array of strings.

In your MoonMail dashboard you can create a dynamic segment to target specific contacts, for example a segment that filters all contacts where hobbies attribute includes hiking.

Trigger event

You can trigger custom events from you app that can be used to activate an event-based campaign.

moonmail.triggerEvent('Customer.Purchase');

You can also pass additional event attributes and metrics to further specify when event-based campaign is triggered.

moonmail.triggerEvent({
eventType: 'Customer.Purchase',
attributes: {category: 'VIP'},
metrics: {amount: 1000}
});

For example in this case you can create an event-based campaign that is triggered only if event has a metric amount > 1000.

note

Event metric values must be a Number type such as a float or integer. Event attribute values must have the type String or be an array of strings.

You can also pass additional contact information as a second parameter

moonmail.triggerEvent('Customer.Purchase', {attributes: {foo: ['bar']}});

To trigger multiple events at once pass them as an array

moonmail.triggerEvent(['Customer.NewOrder', 'Customer.Purchase']);

Or with additional event attributes and metrics

moonmail.triggerEvent([
'Customer.NewOrder',
{
eventType: 'Customer.Purchase',
attributes: {category: 'VIP'},
metrics: {amount: 1000}
}
]);

Changing default contact

When you initialize MoonMail SDK you can provide a default contact as a second attribute. This contact information will be used in subsequent calls. In order to change the default contact, you can call configure method. Pass the same parameters to configure method as when you initialize MoonMail SDK.

moonmail.configure({
contact: {
address: 'email2@example.com',
attributes: {foo: 'bar'}
}
});