Mechanic Get Mechanic
Task library

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

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.

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Required customer tag
  • Include customers who have not placed an order in this many days
  • Email subject prefix
Show 4 more
  • Email recipient
  • Send email anyway if no customers are found
  • Send email daily
  • Send email every monday

Documentation

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.

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

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

mechanic/user/trigger
{% if options.send_email_daily__boolean %}
  mechanic/scheduler/daily
{% elsif options.send_email_every_monday__boolean %}
  mechanic/scheduler/monday
{% endif %}
How task subscriptions work

Option types and flags

Task code

{% assign required_customer_tag = options.required_customer_tag__required %}
{% assign interval_days = options.include_customers_who_have_not_placed_an_order_in_this_many_days__number_required %}
{% assign email_subject_prefix = options.email_subject_prefix__required %}
{% assign email_recipient = options.email_recipient__required_email %}
{% assign send_email_anyway_if_no_customers_are_found = options.send_email_anyway_if_no_customers_are_found__boolean %}

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

{% assign now_s = "now" | date: "%s" | times: 1 %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% comment %}
    -- get IDs of all customers with the required tag and who have placed an order over X days
  {% endcomment %}

  {%- capture customer_segment_query -%}
    customer_tags CONTAINS '{{ required_customer_tag }}' AND orders_placed(until: -{{ interval_days }}d) = true
  {%- endcapture -%}

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

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: {{ customer_segment_query | json }}
        ) {
          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_qualify: customer_ids.size %}
  {% endunless %}

  {% if event.preview %}
    {% assign customer_ids[0] = "gid://shopify/Customer/1234567890" %}
  {% endif %}

  {% comment %}
    -- get additional customer and last order data and save list item in an array for email body processing
  {% endcomment %}

  {% assign list_items = array %}

  {% for customer_id in customer_ids %}
    {% capture query %}
      query {
        customer(id: {{ customer_id | json }}) {
          legacyResourceId
          displayName
          email
          lastOrder {
            legacyResourceId
            name
            processedAt
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customer": {
              "legacyResourceId": "1234567890",
              "displayName": "Alpha Beta",
              "email": "customer@example.com",
              "lastOrder": {
                "processedAt": {{ interval_days | times: -86400 | plus: now_s | minus: 432000 | date: "%Y-%m-%dT%H:%M:%SZ" | json }},
                "legacyResourceId": "1234567890",
                "name": "#PREVIEW"
              }
            }
          }
        }
      {% endcapture %}

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

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

    {% assign lastOrder_days_ago
      = customer.lastOrder.processedAt
      | date: "%s"
      | minus: now_s
      | divided_by: -86400
    %}

    {% capture list_item %}
      <li>
        <a href="{{ shop.admin_url }}customers/{{ customer.legacyResourceId }}">{{ customer.displayName | default: "(unnamed)" }}</a>
        ({{ customer.email | default: "(no email)" }})
        <br>
        Last ordered on {{ customer.lastOrder.processedAt | date: "%Y-%m-%d" }},
        {{ lastOrder_days_ago }} {{ lastOrder_days_ago | pluralize: "day", "days" }} ago
        – <a href="{{ shop.admin_url }}orders/{{ customer.lastOrder.legacyResourceId }}">{{ customer.lastOrder.name }}</a>
      </li>
    {% endcapture %}

    {% assign list_items = list_items | push: list_item %}
  {% endfor %}

  {% comment %}
    -- generate the email subject and body with the list item data from qualifying customers
  {% endcomment %}

  {% capture email_subject %}
    {{ email_subject_prefix }} {{ list_items.size }} {{ list_items.size | pluralize: "customer", "customers" }} found
  {% endcapture %}

  {% if list_items != blank %}
    {% capture email_body %}
      <p>Hello,</p>
      <p>
        Filtering for customers tagged {{ required_customer_tag | json }},
        the following {{ list_items.size }} {{ list_items.size | pluralize: "customer was", "customers were" }}
        found to have not placed an order in the last {{ interval_days }} {{ interval_days | pluralize: "day", "days" }}:
      </p>
      <ul>
      {{ list_items | join: newline }}
      </ul>
      <p>Thanks,<br>- Mechanic, for {{ shop.name }}</p>
    {% endcapture %}

    {% action "email" %}
      {
        "to": {{ email_recipient | json }},
        "subject": {{ email_subject | strip | json }},
        "body": {{ email_body | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}

  {% elsif send_email_anyway_if_no_customers_are_found %}
    {% capture email_body %}
      Hello,

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

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

    {% action "email" %}
      {
        "to": {{ email_recipient | 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 Liquid reference