Copy order and/or product tags to customers, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Copy order and/or product tags to customers

Run this task to scan all of your customers and their order histories, copying order and/or product tags to the relevant customer. Optionally, configure a specific set of tags to look for, when scanning. Optionally, choose to remove those tags if a qualifying source can't be found.

Runs Occurs when a user manually triggers the task. Configuration includes include order tags, include product tags, only include customers matching this query, only include orders matching this query, only copy these tags, remove those tags if a qualifying source cannot be found, and run daily.

15-day free trial – unlimited tasks

Documentation

Run this task to scan all of your customers and their order histories, copying order and/or product tags to the relevant customer. Optionally, configure a specific set of tags to look for, when scanning. Optionally, choose to remove those tags if a qualifying source can't be found.

Both the customer and order query options support Liquid. This means that you can dynamically query for orders, based on things like the current date.

For example, use these options to achieve customer tags that auto-expire a year after the newest qualifying order:

  • "Only include orders matching this query": created_at:>={{ "now - 1 year" | date: "%Y-%m-%d" }}
  • "Only copy these tags": (use whatever order or product tag(s) you want to copy)
  • "Remove those tags if a qualifying source cannot be found": yes
  • "Run daily": yes

Important: The customers query must use the exact casing and syntax as a query that is run from the customer segments admin screen. More information on the the syntax for these can be found here.

For example, to only include customers that have the "subscriber", use this query: customer_tags CONTAINS 'subscriber'

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
{% if options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
include order tags (boolean), include product tags (boolean), only include customers matching this query, only include orders matching this query, only copy these tags (array), remove those tags if a qualifying source cannot be found (boolean), run daily (boolean)
Code
{% assign include_order_tags = options.include_order_tags__boolean %}
{% assign include_product_tags = options.include_product_tags__boolean %}
{% assign customer_segment_query = options.only_include_customers_matching_this_query %}
{% assign only_include_orders_matching_this_query = options.only_include_orders_matching_this_query %}
{% assign only_copy_these_tags = options.only_copy_these_tags__array %}
{% assign remove_those_tags_if_a_qualifying_source_cannot_be_found = options.remove_those_tags_if_a_qualifying_source_cannot_be_found__boolean %}

{% unless include_order_tags or include_product_tags %}
  {% error "Choose at least one of 'Include order tags' and 'Include product tags'. :)" %}
{% endunless %}

{% if remove_those_tags_if_a_qualifying_source_cannot_be_found and only_copy_these_tags == blank %}
  {% error "Mechanic can only remove tags it knows about. If you choose 'Remove those tags [...]', you must also fill in 'Only copy these tags'." %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% comment %}
    -- get IDs of all customers who match the segment query
    -- Note: a segment query cannot be null, so if one has not been configured in the task then send an empty string
  {% endcomment %}

  {% assign cursor = nil %}
  {% assign customer_ids = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: {{ customer_segment_query | default: "" | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          edges {
            node {
              id
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% assign customer_segment_members = result.data.customerSegmentMembers.edges | map: "node" %}

    {% comment %}
      -- remove the "SegmentMember" portion from IDs for easier use in querying each customer for additional data not available in the segment resource
    {% endcomment %}

    {% for customer_segment_member in customer_segment_members %}
      {% assign customer_ids[customer_ids.size] = customer_segment_member.id | remove: "SegmentMember" %}
    {% endfor %}

    {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
      {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% unless event.preview %}
    {% log count_of_customers_matching_query: customer_ids.size %}
  {% endunless %}

  {% if event.preview %}
    {% assign customer_ids[0] = "gid://shopify/Customer/1234567890" %}
  {% endif %}

  {% for customer_id in customer_ids %}
    {% comment %}
      -- get all relevant order data for this customer
    {% endcomment %}

    {% assign cursor = nil %}
    {% assign tags_should_have = array %}

    {% for n in (1..10) %}
      {% capture query %}
        query {
          customer(id: {{ customer_id | json }}) {
            id
            tags
            orders(
              first: 250
              after: {{ cursor | json }}
            ) {
              pageInfo {
                hasNextPage
                endCursor
              }
              nodes {
                id
                {% if include_order_tags %}tags{% endif %}
                {% if include_product_tags %}
                  lineItems(first: 250) {
                    nodes {
                      product {
                        tags
                      }
                    }
                  }
                {% endif %}
              }
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}

      {% if event.preview %}
        {% capture result_json %}
          {
            "data": {
              "customer": {
                "id": "gid://shopify/Customer/1234567890",
                "orders": {
                  "nodes": [
                    {
                      "id": "gid://shopify/Order/1234567890",
                      "tags": {{ only_copy_these_tags.first | default: "order preview tag" | json }},
                      "lineItems": {
                        "nodes": [
                          {
                            "product": {
                              "tags": {{ only_copy_these_tags.first | default: "product preview tag" | json }}
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            }
          }
        {% endcapture %}

        {% assign result = result_json | parse_json %}
      {% endif %}

      {% assign customer = result.data.customer %}

      {% comment %}
        -- process order tags and/or product tags to see which a customer should have based on task configuration
      {% endcomment %}

      {% for order in customer.orders.nodes %}
        {% if include_order_tags %}
          {% for tag in order.tags %}
            {% if only_copy_these_tags != blank %}
              {% unless only_copy_these_tags contains tag %}
                {% continue %}
              {% endunless %}
            {% endif %}

            {% assign tags_should_have = tags_should_have | push: tag %}
          {% endfor %}
        {% endif %}

        {% if include_product_tags %}
          {% for line_item in order.lineItems.nodes %}
            {% for tag in line_item.product.tags %}
              {% if only_copy_these_tags != blank %}
                {% unless only_copy_these_tags contains tag %}
                  {% continue %}
                {% endunless %}
              {% endif %}

              {% assign tags_should_have = tags_should_have | push: tag %}
            {% endfor %}
          {% endfor %}
        {% endif %}
      {% endfor %}

      {% if result.data.customer.orders.pageInfo.hasNextPage %}
        {% assign cursor = result.data.customer.orders.pageInfo.endCursor %}
      {% else %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% comment %}
      -- determine which tags should be added or removed from the customer
    {% endcomment %}

    {% assign tags_to_add = array %}
    {% assign tags_to_remove = array %}

    {% for tag in tags_should_have %}
      {% unless customer.tags contains tag or tags_to_add contains tag %}
        {% assign tags_to_add = tags_to_add | push: tag %}
      {% endunless %}
    {% endfor %}

    {% if remove_those_tags_if_a_qualifying_source_cannot_be_found %}
      {% for tag in only_copy_these_tags %}
        {% if customer.tags contains tag %}
          {% unless tags_should_have contains tag %}
            {% assign tags_to_remove = tags_to_remove | push: tag %}
          {% endunless %}
        {% endif %}
      {% endfor %}
    {% endif %}

    {% if tags_to_add != blank %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.id | json }}
            tags: {{ tags_to_add | sort_natural | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}

    {% if tags_to_remove != blank %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ customer.id | json }}
            tags: {{ tags_to_remove | sort_natural | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Include order tags
true