Tag customers when their last order is before/after x days ago, with Mechanic.

Mechanic is a development platform for Shopify. :)

Tag customers when their last order is before/after x days ago

by Isaac Bowen (team@usemechanic.com)

Running daily, hourly, or manually, this task scans all customers and tags them based on the date of their last order. Choose between tagging customers whose orders are before x days ago, or after x days ago.

Runs when a user triggers the task and when a bulk operation finishes. Configuration includes days since last order, tag customers when last order is after, tag customers when last order is before, customer tag, run hourly, and run daily.

15-day free trial – unlimited tasks

Documentation

Running daily, hourly, or manually, this task scans all customers and tags them based on the date of their last order. Choose between tagging customers whose orders are before x days ago, or after x days ago.

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 a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
days since last order (required, number), tag customers when last order is after (boolean), tag customers when last order is before (boolean), customer tag (required), run hourly (boolean), run daily (boolean)
Script
{% comment %}
  Preferred option order:

  {{ options.days_since_last_order__required_number }}
  {{ options.tag_customers_when_last_order_is_after__boolean }}
  {{ options.tag_customers_when_last_order_is_before__boolean }}
  {{ options.customer_tag__required }}
{% endcomment %}

{% if options.tag_customers_when_last_order_is_after__boolean and options.tag_customers_when_last_order_is_before__boolean %}
  {% error "Choose only one of 'Tag customers when last order is after' or 'Tag customers when last order is before' :)" %}
{% elsif options.tag_customers_when_last_order_is_after__boolean == false and options.tag_customers_when_last_order_is_before__boolean == false %}
  {% error "Choose either 'Tag customers when last order is after' or 'Tag customers when last order is before'" %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% capture bulk_operation_query %}
    query {
      customers(
        sortKey: LAST_ORDER_DATE
        query: "orders_count:>0"
      ) {
        edges {
          node {
            id
            tags
            lastOrder {
              processedAt
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign order_processed_at_interval_s = options.days_since_last_order__required_number | times: 24 | times: 60 | times: 60 %}
  {% assign order_processed_at_threshold_s = "now" | date: "%s" | minus: order_processed_at_interval_s %}
  {% assign order_processed_at_threshold_human = order_processed_at_threshold_s | date: "%Y-%m-%d %H:%M %:z" %}

  {% log %}{{ "Threshold for tagging customers: " | append: order_processed_at_threshold_human | json }}{% endlog %}

  {% if event.preview %}
    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = array %}
    {% assign bulkOperation["objects"][0] = hash %}
    {% assign bulkOperation["objects"][0]["id"] = "gid://shopify/Customer/1234567890" %}
    {% assign bulkOperation["objects"][0]["tags"] = "" %}
    {% assign bulkOperation["objects"][0]["lastOrder"] = hash %}

    {% if options.tag_customers_when_last_order_is_after__boolean %}
      {% assign bulkOperation["objects"][0]["lastOrder"]["processedAt"] = order_processed_at_threshold_s | plus: 1 | date: "%FT%T%:z" %}
    {% elsif options.tag_customers_when_last_order_is_before__boolean %}
      {% assign bulkOperation["objects"][0]["lastOrder"]["processedAt"] = order_processed_at_threshold_s | minus: 1 | date: "%FT%T%:z" %}
    {% endif %}
  {% endif %}

  {% assign customers = bulkOperation.objects %}

  {% for customer in customers %}
    {% if customer.lastOrder == nil %}
      {% continue %}
    {% endif %}

    {% assign customer_should_be_tagged = false %}

    {% assign customer_last_order_processed_at_s = customer.lastOrder.processedAt | date: "%s" | times: 1 %}

    {% if options.tag_customers_when_last_order_is_after__boolean and customer_last_order_processed_at_s >= order_processed_at_threshold_s %}
      {% assign customer_should_be_tagged = true %}
    {% elsif options.tag_customers_when_last_order_is_before__boolean and customer_last_order_processed_at_s <= order_processed_at_threshold_s %}
      {% assign customer_should_be_tagged = true %}
    {% endif %}

    {% assign customer_tags = customer.tags %}
    {% assign customer_is_tagged = false %}
    {% if customer_tags contains options.customer_tag__required %}
      {% assign customer_is_tagged = true %}
    {% endif %}

    {% if customer_should_be_tagged and customer_is_tagged %}
      {% comment %}no-op{% endcomment %}
    {% elsif customer_should_be_tagged and customer_is_tagged == false %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer.id | json }}
            tags: {{ options.customer_tag__required | json }}
          ) {
            node {
              ... on Customer {
                id
                email
                tags
                lastOrder {
                  name
                  processedAt
                }
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% elsif customer_should_be_tagged == false and customer_is_tagged %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ customer.id | json }}
            tags: {{ options.customer_tag__required | json }}
          ) {
            node {
              ... on Customer {
                id
                email
                tags
                lastOrder {
                  name
                  processedAt
                }
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% elsif customer_should_be_tagged == false and customer_is_tagged == false %}
      {% comment %}no-op{% endcomment %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Days since last order
7
Tag customers when last order is after
true
Customer tag
recent-customer