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.
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:
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.
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
:
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.
Go back to triggers to finish setting up your trigger.
Here's an explanation of the different columns:
Customer update
event once again.big-spenders.js
.Click "Save triggers".
An easy way to test this automation is to create an order in the Shopify admin. Follow these steps to create a test order:
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:
The log item expands and you will see a lot of data:
We can't go through everything here, but a few things are worth to mention:
payload
: This holds normalized information about the event, documented in the event payloads documentation.originalPayload
: This is the raw event, as we get it from Shopify. The reason why this is included is because we don't yet normalize everything.id
: This is the ID of the customer, which you need to edit the tags for the customer.totalSpent
: This is the value you need for determining if the customer has spent more than $100 in your store. Note that it's a string, so we need to parse it to a number.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:
tag
module is imported from @supermatic/shopify/tag
.async
keyword is added to the function, since tag.add
is an asynchronous function. As you can see, await
is also used to wait for it to finish.tag.add
is the name of the store (the mystore
part in mystore.myshopify.com
).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.
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.