Email a report of customers who haven't ordered in X days, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Email a report of customers who haven't ordered in X days

by Isaac Bowen (team@usemechanic.com)

Use this task to request or schedule an email digest of customers, having a certain tag, who haven't placed an order in a certain number of days.

Runs when a bulk operation finishes and when a user triggers the task. Configuration includes required customer tag, include customers who have not placed an order in this many days, email subject prefix, email recipient, send email anyway if no customers are found, send email daily, and send email every monday.

15-day free trial – unlimited tasks

Documentation

Run this task manually to request a report immediately, or configure the task to run automatically on a schedule.

YouTube: Watch the development video!

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 bulk operation finishes (mechanic/shopify/bulk_operation)
when a user triggers the task (mechanic/user/trigger)
Options
required customer tag (required), include customers who have not placed an order in this many days (number, required), email subject prefix (required), email recipient (required, email), send email anyway if no customers are found (boolean), send email daily (boolean), send email every monday (boolean)
Script
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% capture bulk_operation_query %}
    query {
      customers(
        query: {{ options.required_customer_tag__required | json | prepend: "orders_count:>=1 AND tag:" | json }}
      ) {
        edges {
          node {
            displayName
            email
            id
            legacyResourceId

            lastOrder {
              createdAt
              legacyResourceId
              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" %}
  {% assign now_s = "now" | date: "%s" | times: 1 %}
  {% assign interval_d = options.include_customers_who_have_not_placed_an_order_in_this_many_days__number_required %}
  {% assign interval_s = interval_d | times: 24 | times: 60 | times: 60 %}
  {% assign order_time_threshold_s = now_s | minus: interval_s %}

  {% if interval_d <= 0 %}
    {% error "The number of days must be greater than 0. :)" %}
  {% endif %}

  {% if event.preview %}
    {% capture objects_json %}
      [
        {
          "displayName": "Alpha Beta",
          "email": "customer@example.com",
          "id": "gid://shopify/Customer/1234567890",
          "legacyResourceId": "1234567890",
          "lastOrder": {
            "createdAt": {{ now_s | minus: interval_s | minus: 432000 | date: "%Y-%m-%dT%H:%M:%SZ" | json }},
            "legacyResourceId": "1234567890",
            "name": "#1000"
          }
        }
      ]
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_json | parse_json %}
  {% endif %}

  {% assign qualifying_customers = array %}

  {% for customer in bulkOperation.objects %}
    {% assign order_time_s = customer.lastOrder.createdAt | date: "%s" | times: 1 %}
    {% if order_time_s < order_time_threshold_s %}
      {% assign qualifying_customers[qualifying_customers.size] = customer %}
    {% endif %}
  {% endfor %}

  {% capture email_subject %}
    {{ options.email_subject_prefix__required }} {{ qualifying_customers.size }} {{ qualifying_customers.size | pluralize: "customer", "customers" }} found
  {% endcapture %}

  {% if qualifying_customers != empty %}
    {% capture email_body %}
      <p>Hello,</p>
      <p>
        Filtering for customers tagged {{ options.required_customer_tag__required | json }},
        the following {{ qualifying_customers.size }} {{ qualifying_customers.size | pluralize: "customer was", "customers were" }}
        found to have not placed an order in the last {{ interval_d }} {{ interval_d | pluralize: "day", "days" }}:
      </p>
      <ul>
        {% for customer in qualifying_customers %}
          {% assign lastOrder_days_ago = customer.lastOrder.createdAt | date: "%s" | times: 1 | minus: now_s | times: -1 | divided_by: 60 | divided_by: 60 | divided_by: 24 %}
          <li>
            <a href="https://{{ shop.domain }}/admin/customers/{{ customer.legacyResourceId }}">{{ customer.displayName | default: "(unnamed)" }}</a>
            ({{ customer.email | default: "(no email)" }})
            <br>
            Last ordered on {{ customer.lastOrder.createdAt | date: "%Y-%m-%d" }},
            {{ lastOrder_days_ago }} {{ lastOrder_days_ago | pluralize: "day", "days" }} ago
            – <a href="https://{{ shop.domain }}/admin/orders/{{ customer.lastOrder.legacyResourceId }}">{{ customer.lastOrder.name }}</a>
          </li>
        {% endfor %}
      </ul>
      <p>Thanks,<br>- Mechanic, for {{ shop.name }}</p>
    {% endcapture %}

    {% action "email" %}
      {
        "to": {{ options.email_recipient__required_email | json }},
        "subject": {{ email_subject | strip | json }},
        "body": {{ email_body | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}
  {% elsif options.send_email_anyway_if_no_customers_are_found__boolean %}
    {% capture email_body %}
      Hello,

      We didn't find any customers (tagged {{ options.required_customer_tag__required | json }}) having zero orders placed in the last {{ interval_d }} day(s).

      Thanks,
      - Mechanic, for {{ shop.name }}
    {% endcapture %}

    {% action "email" %}
      {
        "to": {{ options.email_recipient__required_email | json }},
        "subject": {{ email_subject | strip | json }},
        "body": {{ email_body | unindent | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.