Auto-tag customers based on their orders count with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-tag customers based on their orders count

by Mechanic Team (team@usemechanic.com)

This task applies customer tags based on how many orders they've placed. Add multiple tags, with a minimum orders count each, to advance your customers through different tiers as they place more orders. Optionally, only keep the customer tag with the highest matching order minimum.

Runs when a customer is created, when a customer is updated, and when a user triggers the task. Configuration includes order count minimums and tags, only keep the tag for the highest minimum count, and only tag customers with an active account.

15-day free trial – unlimited tasks

Documentation

Run this task manually to apply your tag rules to all of your customers at once.

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)
Options
order count minimums and tags (keyval, required), only keep the tag for the highest minimum count (boolean), only tag customers with an active account (boolean)
Script
{% comment %}
  Express an opinion about option order:
  {{ options.order_count_minimums_and_tags__keyval_required }}
  {{ options.only_keep_the_tag_for_the_highest_minimum_count__boolean }}
  {{ options.only_tag_customers_with_an_active_account__boolean }}
{% endcomment %}

{% if event.preview or event.topic contains "shopify/customers/" %}
  {% assign customers = array %}
  {% assign customers[customers.size] = customer %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign customers = shop.customers %}
{% endif %}

{% for customer in customers %}
  {% if options.only_tag_customers_with_an_active_account__boolean and customer.state != "enabled" %}
    {% continue %}
  {% endif %}

  {% assign orders_count = customer.orders_count %}
  {% assign best_tag_minimum = 0 %}
  {% assign best_tag = nil %}
  {% assign all_fitting_tags = array %}
  {% assign all_possible_tags = array %}

  {% for pair in options.order_count_minimums_and_tags__keyval_required %}
    {% assign tag_minimum = pair[0] | times: 1 %}
    {% assign tag = pair[1] %}

    {% assign all_possible_tags[all_possible_tags.size] = tag %}

    {% if orders_count >= tag_minimum %}
      {% assign all_fitting_tags[all_fitting_tags.size] = tag %}

      {% if tag_minimum >= best_tag_minimum %}
        {% assign best_tag = tag %}
        {% assign best_tag_minimum = tag_minimum %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {% if options.only_keep_the_tag_for_the_highest_minimum_count__boolean %}
    {% assign customer_tags_to_save = customer.tags | remove_tags: all_possible_tags | add_tag: best_tag %}
  {% else %}
    {% assign customer_tags_to_save = customer.tags | remove_tags: all_possible_tags | add_tags: all_fitting_tags %}
  {% endif %}

  {% if event.preview or customer_tags_to_save != customer.tags %}
    {% action "shopify" %}
      [
        "update",
        ["customer", {{ customer.id | json }}],
        {
          "tags": {{ customer_tags_to_save | json }}
        }
      ]
    {% 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
Order count minimums and tags
{"1"=>"new_customer", "5"=>"returning_customer", "10"=>"committed_customer", "20"=>"hero_customer"}