How to track customer revenue by collecting data from Shopify, WooCommerce or BigCommerce

This tutorial will show you how to report your customer's metrics, like the amount spent when a specific event is triggered using the MoonMail SDK. MoonMail Event-based campaigns are a powerful way to send a message to those of your customers who trigger a specific event. 

Topics

  1. Design your event
  2. Create your audience
  3. Create your event-based campaign
    1. Add campaign details
    2. Choose your audience
    3. Create your message
    4. Choose when to send the campaign
    5. Review and launch
  4. Report events in your application using the MoonMail SDK
  5. Conclusion


1. Design your event

If you run an eCommerce site like a Shopify store or WooCommerce you could report your customer's expenses to your MoonMail account to easily message a group of those customers whose expending is bigger than a given threshold. Let's say you want to send a specific email campaign with a special offer to those customers who spend more than $500 per month.
In the first place, you need to give a name to the event you're going to send with the MoonMail SDK. As you're going to report your customer's expenses, you might call it just  Customer.Expense.
When the Customer.Expense event is sent you'll also need to send some user attributes and metrics for the contact who is going to created/updated with the SDK. You will send a user attribute named  Firstname and a metric named  SpentAmount so, you can create the audience and customize the email body your event-based campaign is going to send. 

2. Create your audience

In this step, you'll create a dynamic segment for the contacts you'll add to your account with the MoonMail SDK.

There are two steps involved in creating a dynamic segment with all your present and future contacts that meet specific criteria. First, you set up the segment. Next, you set up the segment groups for the segment.

To create a segment

  1. Sign in to your MoonMail account.
  2. Choose the Segments section. The Segments page opens and displays segments that you previously defined.
  3. Choose Create segment.
  4. Under Create a segment, choose Build a segment.
  5. For Segment name, type a name for the segment to make it easy to recognize later like Customers with monthly expenses bigger than $500.

Configure the segment groups

  1. Under Segment group 1, next to Include contacts that are in, choose all.
  2. Next to of the following segments, choose no base segment at all to identify all the contacts in your account.
  3. Nex to Contacts that match, choose all of the following filters to apply all the filters in the next step at the same time.
  4. Choose Add a filter group to apply all the filters you need to refine the segment. Use this option to filter by contact specific attributes/metrics. Next to Contact is choose active, and next to during choose last 30 days to select the contacts that fulfill the filter for the last month. To add an attribute, choose Add a filter and add the Metrics SpentAmount greater than 500 as shown in the following image
  5. When you finish setting up the segment, choose Create segment.

By completing these steps, you've created a dynamic segment will all the existing contacts in your MoonMail account that spent more than $500 in the last month. But also, all the contacts you create in the future that meet the criteria (spending more than $500 in the last 30 days) in your account, will be available in this segment too.


3. Create your event-based campaign

In this step, you'll create an event-based campaign that will react to the events you send to the newly created contacts, assigned to the previous segment, with the MoonMail SDK.

To create a campaign

  1. Choose Create campaign in the Campaigns section.

Step 1: Add campaign details

  1. Choose the Event-based campaign type.
  2.  On the Campaign details page, for Campaign name enter a descriptive name for the campaign.
  3. Optionally add a description in the Campaign description. Using a description makes it easier to find or search for the campaign later.
  4. Choose Next.
Step 2: Choose your audience
  1. In this step, you have to choose a campaign sub-type. Choose the Standard campaign to send a campaign with a single message.
  2. On the Campaign audience page, choose the Customers with monthly expenses bigger than $500 Segment you created previously to send your campaign to the contacts that are going to be created for this segment. 
  3. Choose  Next.
Step 3: Create your message
In this step, you'll write an email message customized with the contact's attributes sent with the MoonMail SDK. This email will be received by the contact created with the SDK. 
On the Create your message page, do the following:
  1. For From address, enter the from address for your email message. You must verify a domain in your MoonMail account to prove that you own it before you can use email addresses on that domain as a "From address".
  2. For Subject, enter the subject line for your email message, for example, Special offer.
  3. For  HTML Body, enter the email body. We'll use dynamic variables to access the attributes of the contact sent with the MoonMail SDK. For example, you could offer some specific items for big-spending customers:
  4. Hello {{UserAttributes.Firstname}}, 
    
    You might be interested in the following items in our catalog:
    [...]
    		
  5. Choose Next.

Step 4: Choose when to send the campaign

In this step, you have to schedule the campaign and choose the event that will trigger it.

  1. For Choose an event that will trigger the campaign, choose the name of the event that triggers the execution of the campaign. For this tutorial type Customer.Expense as the event type that will trigger this campaign.
  2. Under Time range when campaign is active, choose a start date. MoonMail sends the campaign only if the event that you specified earlier occurs after the start date.
  3. Note the Start date and time that you choose has to be at least one minute in the future.
  4. For the End date, choose an end date. MoonMail sends the campaign only if the event that you specified earlier occurs before the end date.
  5. Choose Next.

Step 5: Review and launch

This is the final step to create a campaign.
  1. On the Review and launch page, review the settings for the campaign. If you need to make changes, use the navigation section on the left side of the window to go directly to the page that contains the content that you want to edit.
  2. A spam checking process is run automatically in the background. If the message is considered to be spam you will need to make some changes in Step 3: Create your message.
  3. If all the settings are correct and the message is not considered to be spam, choose Activate campaign.

4. Report events in your application using the MoonMail SDK

To report the events in your application you need to use the MoonMail SDK. To start doing so create a Node.js project with the following commands.

npm init -y 
npm install moonmail-js-sdk --save-dev
	

After that, you need to add a script.js file to your project with the following content. 

const MoonMail = require("moonmail-js-sdk");

(async () => {
  // Step 1: Setup the SDK with your MoonMail account ID
  const api = MoonMail.init("<YOUR-ACCOUNT-ID>");

  // Step 2: Setup the contact
  const Contact = {
    Address: "john@doe.com",  // <-- YOUR-CUSTOMER-EMAIL-ADDRESS
    UserAttributes: {
      Firstname: "John"
    },
    Metrics: { SpentAmount: 600 } // Adds $600 to the SpentAmount metric 
  };

  // Step 3: Setup the event
  const Events = [
    {
      EventType: "Customer.Expense" 
    }
  ];

  // Step 4: Send the data to your MoonMail account
  await api.putEvents({ Contact, Events });
})();
		

You can see four steps in the script:

  1. Setup the SDK with your MoonMail account ID. The first step initializes the SDK with your account ID. You can find it in your account settings.
  2. Setup the contact. In this step, you're going to create the Contact object you're going to create or update in your MoonMail account and its attributes. In the Address key, input the email address of your customer. Also, in UserAttributes input the keys that you need to customize your email campaign, i.e., Firstname. You also need to add the Metrics key with the metrics you want to create/update in your contact. In this case, you can create a SpentAmount metric with the latest expenditure for the customer. Metrics are updated not overwritten and they are always numbers, not strings.
  3. Setup the event. In this step, you're going to create the Events array. In this array create an object with the EventType key with the name of the event you want to be triggered, i.e., Customer.Expense.
  4. Send the data to your MoonMail account. With the putEvents method, you can send to your MoonMail account the information about the contacts and events you've set up.

Finally, just run the script:

node script.js
		

5. Conclusion

If you've followed correctly the instructions your customer john@doe.com will receive the following email when you trigger the Customer.Expense event with the MoonMail SDK, because the SpentAmount metric for the contact is greater than $500.

Hello John,   

You might be interested in the following items in our catalog:
[...]