Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs every day at midnight (in local time)
  • Occurs when a user manually triggers the task

What you choose

  • Minimum total spent
  • Customer tag to apply
  • Days of order history to consider
  • Only monitor customers having this tag
  • Run hourly instead of daily

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.

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

{% if options.run_hourly_instead_of_daily__boolean %}
  mechanic/scheduler/hourly
{% else %}
  mechanic/scheduler/daily
{% endif %}
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Days of order history to consider
30