Tag customers when their last order is before/after x days ago
Running daily or manually, this task scans all customers who have placed orders 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.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
- Days since last order
- Tag customers when last order is after
- Tag customers when last order is before
- Customer tag to apply
- Run daily
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
mechanic/shopify/bulk_operation
{% if options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
How task subscriptions work
Option types and flags
- Days since last order (required, number)
- Tag customers when last order is after (boolean)
- Tag customers when last order is before (boolean)
- Customer tag to apply (required)
- Run daily (boolean)
Task code
{% assign days_since_last_order = options.days_since_last_order__required_number %}
{% assign tag_customers_when_last_order_is_after = options.tag_customers_when_last_order_is_after__boolean %}
{% assign tag_customers_when_last_order_is_before = options.tag_customers_when_last_order_is_before__boolean %}
{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}
{% if tag_customers_when_last_order_is_after and tag_customers_when_last_order_is_before %}
{% error "Choose only one of 'Tag customers when last order is after' or 'Tag customers when last order is before' :)" %}
{% endif %}
{% unless tag_customers_when_last_order_is_after or tag_customers_when_last_order_is_before %}
{% error "Choose either 'Tag customers when last order is after' or 'Tag customers when last order is before'" %}
{% endunless %}
{% capture advance_period -%}
-{{ days_since_last_order }} days
{%- endcapture %}
{% assign order_processed_at_threshold = "now" | date: "%F", advance: advance_period %}
{% if tag_customers_when_last_order_is_after %}
{% log %}
"Threshold for tagging customers: {{ order_processed_at_threshold }} and after"
{% endlog %}
{% elsif tag_customers_when_last_order_is_before %}
{% log %}
"Threshold for tagging customers: {{ order_processed_at_threshold }} and before"
{% endlog %}
{% endif %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% comment %}
-- get IDs of all customers who have placed an order after or before the threshold, and/or have the configured tag
-- due to limited data in customer segments, the bulk op query only serves to reduce the overall count of customers being reviewed
{% endcomment %}
{% if tag_customers_when_last_order_is_after %}
{% capture customer_segment_query -%}
last_order_date >= {{ order_processed_at_threshold }} OR customer_tags CONTAINS '{{ customer_tag_to_apply }}'
{%- endcapture %}
{% elsif tag_customers_when_last_order_is_before %}
{% capture customer_segment_query -%}
last_order_date <= {{ order_processed_at_threshold }} OR customer_tags CONTAINS '{{ customer_tag_to_apply }}'
{%- endcapture %}
{% endif %}
{% capture bulk_operation_query %}
query {
customerSegmentMembers(
query: {{ customer_segment_query | json }}
) {
edges {
node {
id
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% if event.preview %}
{% capture jsonl_string %}
{"id":"gid:\/\/shopify\/CustomerSegmentMember\/1234567890"}
{"id":"gid:\/\/shopify\/CustomerSegmentMember\/2345678901"}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
{% endif %}
{% assign customer_segment_member_ids = bulkOperation.objects | map: "id" %}
{% comment %}
-- get tags and last order data from customer resource, since it's not available in customer segments
{% endcomment %}
{% for customer_segment_member_id in customer_segment_member_ids %}
{% assign customer_should_be_tagged = nil %}
{% capture query %}
query {
customer(id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}) {
id
tags
lastOrder {
processedAt
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"customer": {
"id": "gid://shopify/Customer/1234567890",
"lastOrder": {
{% if tag_customers_when_last_order_is_after %}
"processedAt": {{ "now" | date: "%F" | json }}
{% elsif tag_customers_when_last_order_is_before %}
"processedAt": "2000-01-01"
{% endif %}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign customer = result.data.customer %}
{% assign customer_last_order_processed_at = customer.lastOrder.processedAt | date: "%F" %}
{% if tag_customers_when_last_order_is_after and customer_last_order_processed_at >= order_processed_at_threshold %}
{% assign customer_should_be_tagged = true %}
{% elsif tag_customers_when_last_order_is_before and customer_last_order_processed_at <= order_processed_at_threshold %}
{% assign customer_should_be_tagged = true %}
{% endif %}
{% comment %}
-- add and remove customer tags as needed
{% endcomment %}
{% if customer_should_be_tagged %}
{% unless customer.tags contains customer_tag_to_apply %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ customer.id | json }}
tags: {{ customer_tag_to_apply | json }}
) {
node {
... on Customer {
id
defaultEmailAddress {
emailAddress
}
tags
lastOrder {
name
processedAt
}
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% else %}
{% if customer.tags contains customer_tag_to_apply %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ customer.id | json }}
tags: {{ customer_tag_to_apply | json }}
) {
node {
... on Customer {
id
defaultEmailAddress {
emailAddress
}
tags
lastOrder {
name
processedAt
}
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
Mechanic Liquid reference
Default option values
- Days since last order
7- Tag customers when last order is after
true- Run daily
false