Auto-tag customers with the location of their purchase, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag customers with the location of their purchase

by Isaac Bowen (team@usemechanic.com)

When an order is created, this task adds the location of the purchase to the customer's tags. Useful for stores with multiple Shopify-powered locations.

Runs when an order is created, when a user triggers the task, and when a bulk operation finishes. Configuration includes tag for online orders.

15-day free trial – unlimited tasks

Documentation

This task will run for each new order that's created, applying the order location as a customer tag. Optionally, define a tag to be used for orders that have no physical location.

Run this task manually to have Mechanic scan your entire customer base, and each customer's order history.

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)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
tag for online orders
Script
{% if event.topic contains "shopify/orders" %}
  {% if event.preview %}
    {% capture order_json %}
      {
        "location": {
          "name": "Storefront"
        },
        "customer": {
          "admin_graphql_api_id": "gid://shopify/Customer/1234567890",
          "tags": ""
        }
      }
    {% endcapture %}

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

  {% assign customer_tags = order.customer.tags | split: ", " %}

  {% assign location = order.location.name | default: options.tag_for_online_orders %}

  {% unless location == blank or customer_tags contains location %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.customer.admin_graphql_api_id | json }}
          tags: [{{ location | json }}]
        ) {
          node {
            ... on Customer {
              tags
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      customers(query: "orders_count:>0") {
        edges {
          node {
            __typename
            id
            tags
            orders {
              edges {
                node {
                  __typename
                  id
                  physicalLocation {
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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 objects_jsonl %}
      {"__typename":"Customer","id":"gid:\/\/shopify\/Customer\/1234567890","tags":[]}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","physicalLocation":{"name":"Storefront"},"__parentId":"gid:\/\/shopify\/Customer\/1234567890"}
    {% endcapture %}

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

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

  {% for customer in customers %}
    {% assign customer_orders = orders | where: "__parentId", customer.id %}
    {% assign tags_to_add = array %}

    {% for order in customer_orders %}
      {% assign location = order.physicalLocation.name | default: options.tag_for_online_orders %}

      {% if location == blank or customer.tags contains location %}
        {% continue %}
      {% endif %}

      {% assign tags_to_add[tags_to_add.size] = location %}
    {% endfor %}

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