Auto-tag customers that accept marketing
Use this task to auto-tag customers whose marketing preferences have been updated, adding a tag for those that do accept marketing and removing it from customers who do not. Optionally, choose whether to limit tags to customers who have single or confirmed opt-in levels. Run this task manually to scan all customers at once.
Try this taskWhen it runs
- Occurs whenever a customer is updated
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
- Customer tag to add
- Only tag confirmed opt ins
- Tag both single and confirmed opt ins
Documentation
Use this task to auto-tag customers whose marketing preferences have been updated, adding a tag for those that do accept marketing and removing it from customers who do not. Optionally, choose whether to limit tags to customers who have single or confirmed opt-in levels. Run this task manually to scan all customers at once.
Learn more from Shopify about confirmed opt-in (aka double opt-in), and how to configure this for your store.
Note: Running this task manually will sync that tag for ALL customers that accept marketing (and if checked, with required opt-in level), regardless of when their marketing preferences were last updated. If you have many customers, running this task manually could take a long time!
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/customers/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Option types and flags
- Customer tag to add (required)
- Only tag confirmed opt ins (boolean)
- Tag both single and confirmed opt ins (boolean)
Task code
{% assign customer_tag_to_add = options.customer_tag_to_add__required %}
{% assign only_tag_confirmed_opt_ins = options.only_tag_confirmed_opt_ins__boolean %}
{% assign tag_both_single_and_confirmed_opt_ins = options.tag_both_single_and_confirmed_opt_ins__boolean %}
{% if only_tag_confirmed_opt_ins and tag_both_single_and_confirmed_opt_ins %}
{% error "Choose either 'Only tag confirmed opt-ins' or 'Tag both single and confirmed opt-ins', or neither, but not both" %}
{% endif %}
{% assign customers = array %}
{% if event.topic contains "shopify/customers/" %}
{% capture query %}
query {
customer(id: {{ customer.admin_graphql_api_id | json }}) {
id
tags
defaultEmailAddress {
emailAddress
marketingUpdatedAt
marketingOptInLevel
marketingState
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign customers[0] = result.data.customer %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
customers {
edges {
node {
__typename
id
tags
defaultEmailAddress {
emailAddress
marketingUpdatedAt
marketingOptInLevel
marketingState
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign customers = bulkOperation.objects | where: "__typename", "Customer" %}
{% endif %}
{% if event.preview %}
{% capture customers_json %}
[
{
"id": "gid://shopify/Customer/1234567890",
"tags": [],
"defaultEmailAddress": {
"marketingOptInLevel": "SINGLE_OPT_IN",
"marketingState": "SUBSCRIBED"
}
},
{
"id": "gid://shopify/Customer/2345678901",
"tags": [{{ customer_tag_to_add | json }}],
"defaultEmailAddress": {
"marketingOptInLevel": null,
"marketingState": "NOT_SUBSCRIBED"
}
},
{
"id": "gid://shopify/Customer/3456789012",
"tags": [{{ customer_tag_to_add | json }}],
"defaultEmailAddress": {
"marketingOptInLevel": "SINGLE_OPT_IN",
"marketingState": "SUBSCRIBED"
}
},
{
"id": "gid://shopify/Customer/4567890123",
"tags": [],
"defaultEmailAddress": {
"marketingOptInLevel": "CONFIRMED_OPT_IN",
"marketingState": "SUBSCRIBED"
}
}
]
{% endcapture %}
{% assign customers = customers_json | parse_json %}
{% endif %}
{% for customer in customers %}
{% assign qualifies_for_tag = nil %}
{% if only_tag_confirmed_opt_ins %}
{% if customer.defaultEmailAddress.marketingOptInLevel == "CONFIRMED_OPT_IN" %}
{% if customer.defaultEmailAddress.marketingState == "SUBSCRIBED" %}
{% assign qualifies_for_tag = true %}
{% endif %}
{% endif %}
{% elsif tag_both_single_and_confirmed_opt_ins %}
{% if customer.defaultEmailAddress.marketingOptInLevel == "CONFIRMED_OPT_IN" or customer.defaultEmailAddress.marketingOptInLevel == "SINGLE_OPT_IN" %}
{% if customer.defaultEmailAddress.marketingState == "SUBSCRIBED" %}
{% assign qualifies_for_tag = true %}
{% endif %}
{% endif %}
{% elsif customer.defaultEmailAddress.marketingState == "SUBSCRIBED" %}
{% assign qualifies_for_tag = true %}
{% endif %}
{% if qualifies_for_tag %}
{% unless customer.tags contains customer_tag_to_add %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer.id | json }}
tags: {{ customer_tag_to_add | json }}
) {
node {
... on Customer {
tags_after_add: tags
defaultEmailAddress {
emailAddress
marketingUpdatedAt
marketingOptInLevel
marketingState
}
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% elsif customer.tags contains customer_tag_to_add %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ customer.id | json }}
tags: {{ customer_tag_to_add | json }}
) {
node {
... on Customer {
tags_after_remove: tags
defaultEmailAddress {
emailAddress
marketingUpdatedAt
marketingOptInLevel
marketingState
}
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
Mechanic Liquid reference