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

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

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

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 Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. 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

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
mechanic/shopify/bulk_operation

{% if options.run_hourly__boolean %}
  mechanic/scheduler/hourly
{% elsif options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
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)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Days since last order
7
Tag customers when last order is after
true
Customer tag
recent-customer