Tag customers with product tags from their order with Mechanic.

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

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
Script
{% if event.preview %}
  {% assign product_tags_from_order = "club-limited, december-sale" %}
{% 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 == blank %}
  {% assign tags_to_copy = product_tags_from_order %}
{% else %}
  {% assign tags_to_copy = "" %}

  {% assign whitelisted_tags_with_possible_whitespace = options.only_copy_these_tags | split: "," %}
  {% capture whitelisted_tags_string %}{% for tag in whitelisted_tags_with_possible_whitespace %}{{ tag | strip }}{% unless forloop.last %},{% endunless %}{% endfor %}{% endcapture %}
  {% assign whitelisted_tags = whitelisted_tags_string | split: "," %}

  {% 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 | add_tag: whitelisted_tag %}
    {% endif %}
  {% endfor %}
{% endif %}

{% assign tags_to_save = order.customer.tags | add_tags: tags_to_copy %}
{% if tags_to_save == order.customer.tags and event.preview != true %}
  {"log": "The customer already has all tags from this order, so there's nothing to update."}
{% else %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "customer",
          {{ order.customer.id | json }}
        ],
        {
          "tags": {{ tags_to_save | json }}
        }
      ]
    }
  }
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.