Mechanic Get Mechanic
Task library

Auto-tag customers when another tag is added

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

Try this task

When it runs

  • Occurs whenever a customer is created
  • Occurs whenever a customer is updated

What you choose

  • Tags to watch for and tags to add
  • Remove tag to add when the corresponding tag to watch for is removed

Documentation

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

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.

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/create
shopify/customers/update
How task subscriptions work

Option types and flags

Task code

{% 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 Liquid reference