Auto-tag customers having a rolling minimum total spend, with Mechanic.

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

Auto-tag customers having a rolling minimum total spend

This task runs daily to maintain tags for any customers that have a certain spending threshold within a rolling period of order history. Useful for rewarding customers who keep a consistent spend total.

Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task. Configuration includes minimum total spent, customer tag to apply, days of order history to consider, only monitor customers having this tag, and run hourly instead of daily.

15-day free trial – unlimited tasks

Documentation

This task runs daily to maintain tags for any customers that have a certain spending threshold within a rolling period of order history. Useful for rewarding customers who keep a consistent spend total.

Optionally, choose to filter by customers who have a specific tag, or to run hourly instead of daily for increased tagging frequency.

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
{% if options.run_hourly_instead_of_daily__boolean %}
  mechanic/scheduler/hourly
{% else %}
  mechanic/scheduler/daily
{% endif %}
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
minimum total spent (number, required) , customer tag to apply (required) , days of order history to consider (number, required) , only monitor customers having this tag , run hourly instead of daily (boolean)
Code
{% assign minimum_total_spent = options.minimum_total_spent__number_required %}
{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}
{% assign days_of_order_history = options.days_of_order_history_to_consider__number_required %}
{% assign monitor_customer_tag = options.only_monitor_customers_having_this_tag %}

{% if monitor_customer_tag == customer_tag_to_apply %}
  {% error "The two customer tag values must be different. Please change either 'Customer tag to apply' or 'Only monitor customers having this tag'." %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% comment %}
    -- get IDs of all customers who meet the minimum spend criteria and have not yet been tagged
  {% endcomment %}

  {%- capture qualifying_search_query -%}
    orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = true AND customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}'
  {%- endcapture -%}

  {% comment %}
    -- if monitor tag configured, then also filter by that tag
  {% endcomment %}

  {% if monitor_customer_tag != blank %}
    {% capture qualifying_search_query -%}
      customer_tags CONTAINS '{{ monitor_customer_tag }}' AND {{ qualifying_search_query }}
    {%- endcapture %}
  {% endif %}

  {% log qualifying_search_query: qualifying_search_query %}

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

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: {{ qualifying_search_query | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          edges {
            node {
              id
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customerSegmentMembers": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/CustomerSegmentMember/1234567890"
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign customer_segment_member_ids
      = result.data.customerSegmentMembers.edges
      | map: "node"
      | map: "id"
      | concat: customer_segment_member_ids
    %}

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

  {% unless event.preview %}
    {% log count_of_customers_needing_tag_added: customer_segment_member_ids.size %}
  {% endunless %}

  {% for customer_segment_member_id in customer_segment_member_ids %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}
          tags: {{ customer_tag_to_apply | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}

  {% comment %}
    -- get IDs of all customers with tag who no longer meet the minimum spend criteria
  {% endcomment %}

  {%- capture disqualifying_search_query -%}
    customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = false
  {%- endcapture -%}

  {% comment %}
    -- if monitor tag configured, then also find customers without the monitor tag who have the minimum spend qualifying tag
  {% endcomment %}

  {% if monitor_customer_tag != blank %}
    {% capture disqualifying_search_query -%}
      customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND (customer_tags NOT CONTAINS '{{ monitor_customer_tag }}' OR orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = false)
    {%- endcapture %}
  {% endif %}

  {% log disqualifying_search_query: disqualifying_search_query %}

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

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: {{ disqualifying_search_query | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          edges {
            node {
              id
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customerSegmentMembers": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/CustomerSegmentMember/2345678901"
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign customer_segment_member_ids
      = result.data.customerSegmentMembers.edges
      | map: "node"
      | map: "id"
      | concat: customer_segment_member_ids
    %}

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

  {% unless event.preview %}
    {% log count_of_customers_needing_tag_removal: customer_segment_member_ids.size %}
  {% endunless %}

  {% for customer_segment_member_id in customer_segment_member_ids %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}
          tags: {{ customer_tag_to_apply | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Days of order history to consider
30