Auto-tag customers when another tag is added, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag customers when another tag is added

by Isaac Bowen (team@usemechanic.com)

Does exactly as it says. :) Optionally, use combinations of tags to require, and/or combinations of tags to add.

Runs when a customer is created and when a customer is updated. Configuration includes tags to watch for and tags to add and remove tag to add when the corresponding tag to watch for is removed.

15-day free trial – unlimited tasks

Documentation

Configure this task with customer tags to watch for on the left, and associated tags to add on the right. (Feel free to use comma-delimited lists on either side of that mapping!) This task will run whenever a customer is created or updated, tagging as configured.

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)
Options
tags to watch for and tags to add (keyval, required), remove tag to add when the corresponding tag to watch for is removed (boolean)
Script
{% if event.preview %}
  {% assign customer = hash %}
  {% assign customer["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
  {% assign customer["tags"] = options.tags_to_watch_for_and_tags_to_add__keyval_required.first.first %}
{% endif %}

{% assign existing_tags = customer.tags | split: ", " %}
{% assign deserved_tags = array %}

{% assign tags_to_add = array %}
{% assign tags_to_remove = array %}

{% for keyval in options.tags_to_watch_for_and_tags_to_add__keyval_required %}
  {% assign tags_to_watch_for = keyval[0] | replace: ", ", "," | split: "," %}
  {% assign has_tags_to_watch_for = true %}

  {% for a_tag_to_watch_for in tags_to_watch_for %}
    {% unless existing_tags contains a_tag_to_watch_for %}
      {% assign has_tags_to_watch_for = false %}
    {% endunless %}
  {% endfor %}

  {% unless has_tags_to_watch_for %}
    {% continue %}
  {% endunless %}

  {% assign tags = keyval[1] | replace: ", ", "," | split: "," %}
  {% assign deserved_tags = deserved_tags | concat: tags %}

  {% for tag in tags %}
    {% unless existing_tags contains tag %}
      {% assign tags_to_add[tags_to_add.size] = tag %}
    {% endunless %}
  {% endfor %}
{% endfor %}

{% if options.remove_tag_to_add_when_the_corresponding_tag_to_watch_for_is_removed__boolean %}
  {% assign existing_tags_plus_tags_to_add = existing_tags | concat: tags_to_add %}

  {% for keyval in options.tags_to_watch_for_and_tags_to_add__keyval_required %}
    {% assign tags_to_watch_for = keyval[0] | replace: ", ", "," | split: "," %}
    {% assign has_tags_to_watch_for = true %}

    {% for a_tag_to_watch_for in tags_to_watch_for %}
      {% unless existing_tags_plus_tags_to_add contains a_tag_to_watch_for %}
        {% assign has_tags_to_watch_for = false %}
      {% endunless %}
    {% endfor %}

    {% if has_tags_to_watch_for %}
      {% continue %}
    {% endif %}

    {% assign tags = keyval[1] | replace: ", ", "," | split: "," %}

    {% for tag in tags %}
      {% unless existing_tags contains tag %}
        {% continue %}
      {% endunless %}

      {% if deserved_tags contains tag %}
        {% continue %}
      {% endif %}

      {% assign tags_to_remove[tags_to_remove.size] = tag %}
    {% endfor %}
  {% endfor %}
{% endif %}

{% if tags_to_add != empty or tags_to_remove != empty %}
  {% action "shopify" %}
    mutation {
      {% if tags_to_add != empty %}
        tagsAdd(
          id: {{ customer.admin_graphql_api_id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      {% endif %}

      {% if tags_to_remove != empty %}
        tagsRemove(
          id: {{ customer.admin_graphql_api_id | json }}
          tags: {{ tags_to_remove | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      {% endif %}
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.