Sync an inverse customer tag, with Mechanic.

Mechanic is a development platform for Shopify. :)

Sync an inverse customer tag

by Isaac Bowen (team@usemechanic.com)

Shopify's customer group filters allow you to filter by the tags the customer contains – but not the tags a customer does not contain. Use this task to sync an "inverse" tag to all customers who do not have a particular tag, allowing you to filter customers in Shopify by that inverse tag.

Runs when a customer is created, when a customer is updated, when a user triggers the task, and when a bulk operation finishes. Configuration includes customer tag to watch and customer tag to use when missing.

15-day free trial – unlimited tasks

Documentation

To explain by example, if you've already tagged your wholesale customers with wholesale, configure the "Customer tag to watch" option with "wholesale", and the "Customer tag to use when missing" option with "retail". This task will automatically tag all non-wholesale customers with "retail", and automatically remove that tag to any customers who later receive the tag "wholesale". Please note: if you remove the "retail" tag, this task will simply re-add it. To remove the "retail" tag, you would need to add the "wholesale" tag to that customer.

Run this task manually to scan all of the customers in your store. Otherwise, this task will monitor and sync tags for all new and updated customers, as they come through.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when a customer is created (shopify/customers/create)
when a customer is updated (shopify/customers/update)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
customer tag to watch (required), customer tag to use when missing (required)
Script
{% assign customers = array %}

{% if event.topic contains "shopify/customers/" %}
  {% if event.preview %}
    {% assign customer = hash %}
    {% assign customer["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
    {% assign customer["tags"] = "" %}
  {% endif %}

  {% assign customer_node = hash %}
  {% assign customer_node["id"] = customer.admin_graphql_api_id %}
  {% assign customer_node["tags"] = customer.tags | split: ", " %}
  {% assign customers[0] = customer_node %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      customers {
        edges {
          node {
            id
            tags
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture objects_jsonl %}
      {"id":"gid://shopify/Customer/1234567890","tags": []}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign customers = bulkOperation.objects %}
{% endif %}

{% for customer in customers %}
  {% assign add_tag = false %}
  {% assign remove_tag = false %}

  {% if customer.tags contains options.customer_tag_to_watch__required %}
    {% if customer.tags contains options.customer_tag_to_use_when_missing__required %}
      {% assign remove_tag = true %}
    {% endif %}
  {% else %}
    {% unless customer.tags contains options.customer_tag_to_use_when_missing__required %}
      {% assign add_tag = true %}
    {% endunless %}
  {% endif %}

  {% if add_tag or remove_tag %}
    {% action "shopify" %}
      mutation {
        tags{% if add_tag %}Add{% else %}Remove{% endif %}(
          id: {{ customer.id | json }}
          tags: {{ options.customer_tag_to_use_when_missing__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Customer tag to watch
wholesale
Customer tag to use when missing
retail