-
Notifications
You must be signed in to change notification settings - Fork 35
/
auto-tag-customers-having-a-rolling-minimum-total-spend.json
26 lines (26 loc) · 7.52 KB
/
auto-tag-customers-having-a-rolling-minimum-total-spend.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"docs": "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.\n\nOptionally, choose to filter by customers who have a specific tag, or to run hourly instead of daily for increased tagging frequency.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers having a rolling minimum total spend",
"online_store_javascript": null,
"options": {
"minimum_total_spent__number_required": null,
"customer_tag_to_apply__required": null,
"days_of_order_history_to_consider__number_required": 30,
"only_monitor_customers_having_this_tag": null,
"run_hourly_instead_of_daily__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign minimum_total_spent = options.minimum_total_spent__number_required %}\n{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}\n{% assign days_of_order_history = options.days_of_order_history_to_consider__number_required %}\n{% assign monitor_customer_tag = options.only_monitor_customers_having_this_tag %}\n\n{% if monitor_customer_tag == customer_tag_to_apply %}\n {% error \"The two customer tag values must be different. Please change either 'Customer tag to apply' or 'Only monitor customers having this tag'.\" %}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% comment %}\n -- get IDs of all customers who meet the minimum spend criteria and have not yet been tagged\n {% endcomment %}\n\n {%- capture qualifying_search_query -%}\n 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 }}'\n {%- endcapture -%}\n\n {% comment %}\n -- if monitor tag configured, then also filter by that tag\n {% endcomment %}\n\n {% if monitor_customer_tag != blank %}\n {% capture qualifying_search_query -%}\n customer_tags CONTAINS '{{ monitor_customer_tag }}' AND {{ qualifying_search_query }}\n {%- endcapture %}\n {% endif %}\n\n {% log qualifying_search_query: qualifying_search_query %}\n\n {% assign cursor = nil %}\n {% assign customer_segment_member_ids = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customerSegmentMembers(\n first: 1000\n after: {{ cursor | json }}\n query: {{ qualifying_search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n id\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customerSegmentMembers\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/CustomerSegmentMember/1234567890\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer_segment_member_ids\n = result.data.customerSegmentMembers.edges\n | map: \"node\"\n | map: \"id\"\n | concat: customer_segment_member_ids\n %}\n\n {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless event.preview %}\n {% log count_of_customers_needing_tag_added: customer_segment_member_ids.size %}\n {% endunless %}\n\n {% for customer_segment_member_id in customer_segment_member_ids %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer_segment_member_id | remove: \"SegmentMember\" | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n\n {% comment %}\n -- get IDs of all customers with tag who no longer meet the minimum spend criteria\n {% endcomment %}\n\n {%- capture disqualifying_search_query -%}\n customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND orders_placed(since: -{{ days_of_order_history }}d, sum_amount_at_least: {{ minimum_total_spent }}) = false\n {%- endcapture -%}\n\n {% comment %}\n -- if monitor tag configured, then also find customers without the monitor tag who have the minimum spend qualifying tag\n {% endcomment %}\n\n {% if monitor_customer_tag != blank %}\n {% capture disqualifying_search_query -%}\n 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)\n {%- endcapture %}\n {% endif %}\n\n {% log disqualifying_search_query: disqualifying_search_query %}\n\n {% assign cursor = nil %}\n {% assign customer_segment_member_ids = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n customerSegmentMembers(\n first: 1000\n after: {{ cursor | json }}\n query: {{ disqualifying_search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n id\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"customerSegmentMembers\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/CustomerSegmentMember/2345678901\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer_segment_member_ids\n = result.data.customerSegmentMembers.edges\n | map: \"node\"\n | map: \"id\"\n | concat: customer_segment_member_ids\n %}\n\n {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless event.preview %}\n {% log count_of_customers_needing_tag_removal: customer_segment_member_ids.size %}\n {% endunless %}\n\n {% for customer_segment_member_id in customer_segment_member_ids %}\n {% action \"shopify\" %}\n mutation {\n tagsRemove(\n id: {{ customer_segment_member_id | remove: \"SegmentMember\" | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"mechanic/scheduler/daily",
"mechanic/user/trigger"
],
"subscriptions_template": "{% if options.run_hourly_instead_of_daily__boolean %}\n mechanic/scheduler/hourly\n{% else %}\n mechanic/scheduler/daily\n{% endif %}\nmechanic/user/trigger",
"tags": [
"Auto-Tag",
"Customers",
"Spend"
]
}