Mechanic Get Mechanic
Task library

Auto-tag customers with the location of their purchase

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

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs when a user manually triggers the task

What you choose

Tag for online orders

Documentation

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

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

Run this task manually to have Mechanic scan each customer with an order history.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/create
mechanic/user/trigger
How task subscriptions work

Task code

{% assign tag_for_online_orders = options.tag_for_online_orders %}

{% if event.topic contains "shopify/orders" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        customer {
          id
          tags
        }
        retailLocation {
          name
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "customer": {
              "id": "gid://shopify/Customer/1234567890"
            },
            "retailLocation": {
              "name": "Storefront"
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign order = result.data.order %}
  {% assign customer = order.customer %}
  {% assign location_name = order.retailLocation.name | default: tag_for_online_orders %}

  {% unless customer == blank or location_name == blank or customer.tags contains location_name %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ customer.id | json }}
          tags: {{ location_name | json }}
        ) {
          node {
            ... on Customer {
              id
              displayName
              tags
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endunless %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- get IDs of all customers who have placed an order
  {% endcomment %}

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

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: "number_of_orders > 0"
        ) {
          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_who_have_placed_an_order: 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_to_add = 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
                name
                retailLocation {
                  name
                }
              }
            }
          }
        }
      {% 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",
                      "retailLocation": {
                        "name": "Storefront"
                      }
                    }
                  ]
                }
              }
            }
          }
        {% endcapture %}

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

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

      {% comment %}
        -- process orders to see which location names should be set as tags
      {% endcomment %}

      {% for order in customer.orders.nodes %}
        {% assign location_name = order.retailLocation.name | default: tag_for_online_orders %}

        {% unless location_name == blank or customer.tags contains location_name or tags_to_add contains location_name %}
          {% assign tags_to_add = tags_to_add | push: location_name %}
        {% endunless %}
      {% endfor %}

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

    {% 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 %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference