Supermatic Docs

Create your first automation

Let's say you want to tag customers with a Big spender tag if they have spent more than $100 in your store. You can then use that tag for better targeted marketing campaigns, for example.

By setting up this automation, or just reading through this guide, you'll learn how Supermatic works and how to create automations of your own. So give it a try, even if you don't think this specific automation would be a good fit for your store.

1. Find a suitable event

When setting up an automation like this, a good start is to think about for which event you should run your code. In this case, you want to add the tag as soon as a customer has spent $100, no matter if that was in a single order or several orders. So, it sounds like running your code whenever a customer is updated should be sufficient.

Go to triggers and to see the available events.

If you add a new trigger, you can see this dropdown of different events in the event column. And if you go down a bit, there's actually a Customer update event, which is perfect for this use case:

Event list

But will you get all the data you need? If you go to the event payloads section of the documentation, you can see that for the customer events, you get something called totalSpent. And this is exactly what you need.

Now that you know when your code will run, you can start writing it. You'll finish setting up the trigger in step 3.

2. Create a file

It's time to start coding, so head over to the editor.

The editor is similar to Shopify's theme editor. But instead of editing your store's appearance, you edit how your store should behave in the background. Like automatically merging orders, sending SMS, posting to social media, syncing data between stores, or in this case, tagging customers. The possibilities are endless.

Create a new file called big-spenders.js:

New file

To fetch the event, you have to export a default function. Try just logging the event, and confirm that you get the data you need.

export default (event) => {
  // `JSON.stringify` makes the event a little easier to read.
  console.log(JSON.stringify(event, null, 2));
};

Save the file by hitting Ctrl-S on Windows or Linux, and Command-S on Mac.

3. Create a trigger

Go back to triggers to finish setting up your trigger.

Here's an explanation of the different columns:

Click "Save triggers".

4. Inspect the logs

An easy way to test this automation is to create an order in the Shopify admin. Follow these steps to create a test order:

  1. Select any product in the product field.
  2. Click "Create a new customer" when selecting customer and create a test customer, if you don't have one already.
  3. Finally, click the "Collect payment" button and select "Mark as paid".

After you have created the order, go to inspect the logs. In there you should see something like in the image below. Click the little caret to the right to see the output:

Logs

The log item expands and you will see a lot of data:

Log output

We can't go through everything here, but a few things are worth to mention:

5. Write the code

Now you're ready to implement the code for the automation. Go back to your big-spenders.js file and edit it to something like this:

export default (event) => {
  if (parseInt(event.payload.totalSpent) > 100) {
    // TODO: Add tag to the customer.
  }
};

How do you add tags to the customer? In our @supermatic/shopify package we export a tag module, which can be used to manage tags for customers. Add a tag.add call to your code:

import tag from "@supermatic/shopify/tag";

export default async (event) => {
  if (parseInt(event.payload.totalSpent) > 100) {
    await tag.add(
      "mystore", // Replace this with your store name.
      event.payload.id,
      ["Big spender"]
    );
  }
};

This code isn't too complicated, but let's go through a few important details:

But what if you issue a refund, which brings the customer below the $100 threshold? Just remove the tag if the customer hasn't reached the threshold:

import tag from "@supermatic/shopify/tag";

export default async (event) => {
  if (parseInt(event.payload.totalSpent) > 100) {
    await tag.add(
      "mystore", // Replace this with your store name.
      event.payload.id,
      ["Big spender"]
    );
  } else {
    await tag.remove(
      "mystore", // Replace this with your store name.
      event.payload.id,
      ["Big spender"]
    );
  }
};

tag.add and tag.remove are both idempotent, so they will only add the tag if it doesn't exist or remove it if it does exist, respectively.

6. Try it out

Make sure that everything works by creating one or several orders for a single customer, bringing the customer's total spent over $100. You can then inspect the customer and look for the Big spender tag. Finally, delete the orders and make sure that the tag is removed.

That's it! Now you are familiar with how Supermatic works and can start creating your own automations. Don't hesitate to use the chat window in the corner or contact us at [email protected], if you have any questions.