Auto-tag customers that accept marketing, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever a customer is updated, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes customer tag to add, only tag confirmed opt ins, and tag both single and confirmed opt ins.

15-day free trial – unlimited tasks

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!

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/customers/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
customer tag to add (required), only tag confirmed opt ins (boolean), tag both single and confirmed opt ins (boolean)
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
        email
        emailMarketingConsent {
          consentUpdatedAt
          marketingState
          marketingOptInLevel
        }
      }
    }
  {% 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
            email
            emailMarketingConsent {
              consentUpdatedAt
              marketingState
              marketingOptInLevel
            }
          }
        }
      }
    }
  {% 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": [],
        "emailMarketingConsent": {
          "marketingState": "SUBSCRIBED",
          "marketingOptInLevel": "SINGLE_OPT_IN"
        }
      },
      {
        "id": "gid://shopify/Customer/2345678901",
        "tags": [{{ customer_tag_to_add | json }}],
        "emailMarketingConsent": {
          "marketingState": "NOT_SUBSCRIBED",
          "marketingOptInLevel": null
        }
      },
      {
        "id": "gid://shopify/Customer/3456789012",
        "tags": [{{ customer_tag_to_add | json }}],
        "emailMarketingConsent": {
          "marketingState": "SUBSCRIBED",
          "marketingOptInLevel": "SINGLE_OPT_IN"
        }
      },
      {
        "id": "gid://shopify/Customer/4567890123",
        "tags": [],
        "emailMarketingConsent": {
          "marketingState": "SUBSCRIBED",
          "marketingOptInLevel": "CONFIRMED_OPT_IN"
        }
      }
    ]
  {% 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.emailMarketingConsent.marketingOptInLevel == "CONFIRMED_OPT_IN" %}
      {% if customer.emailMarketingConsent.marketingState == "SUBSCRIBED" %}
        {% assign qualifies_for_tag = true %}
      {% endif %}
    {% endif %}

  {% elsif tag_both_single_and_confirmed_opt_ins %}
    {% if customer.emailMarketingConsent.marketingOptInLevel == "CONFIRMED_OPT_IN" or customer.emailMarketingConsent.marketingOptInLevel == "SINGLE_OPT_IN" %}
      {% if customer.emailMarketingConsent.marketingState == "SUBSCRIBED" %}
        {% assign qualifies_for_tag = true %}
      {% endif %}
    {% endif %}

  {% elsif customer.emailMarketingConsent.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 {
                email
                tags_after_add: tags
                emailMarketingConsent {
                  consentUpdatedAt
                  marketingState
                  marketingOptInLevel
                }
              }
            }
            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 {
              email
              tags_after_remove: tags
              emailMarketingConsent {
                consentUpdatedAt
                marketingState
                marketingOptInLevel
              }
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more