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

Mechanic is a development platform for Shopify. :)

Auto-tag customers with product tags from their order

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. This task supports running manually, to scan and tag all customers based on their order history, or a subset of customers by using a query filter.

Runs when an order is created, when a user triggers the task, and when a bulk operation finishes. Configuration includes limit to customers matching this query for manual runs and only copy these tags.

15-day free trial – unlimited tasks

Documentation

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. This task supports running manually, to scan and tag all customers based on their order history, or a subset of customers by using a query filter.

This task will scan the product tags for each order, as the order is created, and copy those tags over to the customer record. Run this task manually to process all orders, for all customers. (This may take some time!)

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

The "Limit to customers matching this query for manual runs" option uses the same query syntax as the "Customers" section of the Shopify admin area. For example, to only include customers that have more than one order, use this query:

orders_count:>1

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.

Open source
View on GitHub to contribute to this task
Events
when an order is created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
limit to customers matching this query for manual runs, only copy these tags (array)
Script
{% if event.topic == "mechanic/user/trigger" %}
  {% assign customers_query = nil %}

  {% if options.limit_to_customers_matching_this_query_for_manual_runs != blank %}
    {% assign customers_query = options.limit_to_customers_matching_this_query_for_manual_runs %}
  {% endif %}

  {% capture bulk_operation_query %}
    query {
      customers(
        query: {{ customers_query | json }}
      ) {
        edges {
          node {
            __typename
            id
            tags
            orders {
              edges {
                node {
                  __typename
                  id
                  lineItems {
                    edges {
                      node {
                        __typename
                        id
                        product {
                          __typename
                          id
                          tags
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture bulkOperation_objects_jsonl %}
      {"__typename":"Customer","id":"gid:\/\/shopify\/Customer\/1234567890","tags":["red"]}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","__parentId":"gid:\/\/shopify\/Customer\/1234567890"}
      {"__typename":"LineItem","id":"gid:\/\/shopify\/LineItem\/1234567890","product":{"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890","tags":{{ options.only_copy_these_tags__array | join: ", " | default: "red, green" | split: ", " | json }}},"__parentId":"gid:\/\/shopify\/Order\/1234567890"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = bulkOperation_objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign customers = bulkOperation.objects | where: "__typename", "Customer" %}
  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
  {% assign line_items = bulkOperation.objects | where: "__typename", "LineItem" %}

  {% for customer in customers %}
    {% assign tags_to_copy = array %}

    {% assign order_ids = orders | where: "__parentId", customer.id | map: "id" %}

    {% for line_item in line_items %}
      {% unless order_ids contains line_item.__parentId %}
        {% continue %}
      {% endunless %}

      {% for tag in line_item.product.tags %}
        {% if tags_to_copy contains tag or customer.tags contains tag %}
          {% continue %}
        {% endif %}

        {% if options.only_copy_these_tags__array != blank %}
          {% unless options.only_copy_these_tags__array contains tag %}
            {% continue %}
          {% endunless %}
        {% endif %}

        {% assign tags_to_copy[tags_to_copy.size] = tag %}
      {% endfor %}
    {% endfor %}

    {% if tags_to_copy != empty %}
      {% log customer: customer %}

      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.id | json }}
            tags: {{ tags_to_copy | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% elsif event.topic == "shopify/orders/create" %}
  {% if event.preview %}
    {% capture order_json %}
      {
        "customer": {
          "admin_graphql_api_id": "gid://shopify/Customer/1234567890"
        },
        "line_items": [
          {
            "product": {
              "tags":
                {% if options.only_copy_these_tags__array != blank %}
                  {{ options.only_copy_these_tags__array | json }}
                {% else %}
                  "club-limited, december-sale"
                {% endif  %}
            }
          }
        ]
      }
    {% endcapture %}

    {% assign order = order_json | parse_json %}
  {% endif %}

  {% assign product_tags_from_order = order.line_items | map: "product" | map: "tags" | join: ", " | split: ", " | sort | uniq %}
  {% log product_tags_from_order: product_tags_from_order %}

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

    {% assign only_copy_these_tags = options.only_copy_these_tags__array %}
    {% log only_copy_these_tags: only_copy_these_tags %}

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

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