Auto-tag customers with product tags from their order, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag customers with product tags from their order

by Isaac Bowen (team@usemechanic.com)

When orders come in, use this task to copy product tags from the order to the customer on file. Useful for automatically segmenting your customers based on attributes of their purchases. To keep things tidy, optionally configure this task to only look for a certain set of tags.

Runs when an order is created. Configuration includes only copy these tags.

15-day free trial – unlimited tasks

Documentation

This task will scan the product tags for each order, as the order is created, and copy those tags over to the customer record.

To keep things clean, fill in the "Only copy these tags" option with a comma-separated list of tags you want to watch for.

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 an order is created (shopify/orders/create)
Options
only copy these tags (array)
Script
{% if event.preview %}
  {% if options.only_copy_these_tags__array != blank %}
    {% assign product_tags_from_order = options.only_copy_these_tags__array %}
  {% else %}
    {% assign product_tags_from_order = "club-limited,december-sale" | split: "," %}
  {% endif  %}
{% else %}
  {% assign product_tags_from_order = order.line_items | map: "product" | map: "tags" | join: ", " | split: ", " | sort | uniq %}
  {% capture log_message %}This order contains these product tags: {{ product_tags_from_order | json }}{% endcapture %}
  {"log": {{ log_message | json }}}
{% endif %}

{% if options.only_copy_these_tags__array == blank %}
  {% assign tags_to_copy = product_tags_from_order %}
{% else %}
  {% assign tags_to_copy = array %}

  {% assign whitelisted_tags = options.only_copy_these_tags__array %}
  {% capture log_message %}Whitelist is set to these tags: {{ whitelisted_tags | json }}{% endcapture %}
  {"log": {{ log_message | json }}}

  {% for whitelisted_tag in whitelisted_tags %}
    {% if product_tags_from_order contains whitelisted_tag %}
      {% assign tags_to_copy[tags_to_copy.size] = whitelisted_tag %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if tags_to_copy != empty %}
  {% if event.preview %}
    {% assign customer = '{"admin_graphql_api_id":"gid://shopify/Customer/1234567890"}' | parse_json %}
  {% else %}
    {% assign customer = order.customer %}
  {% endif %}

  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ customer.admin_graphql_api_id | json }}
        tags: {{ tags_to_copy | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.