forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag-customers-by-order-tier.json
33 lines (33 loc) · 9.27 KB
/
tag-customers-by-order-tier.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
27
28
29
30
31
32
33
{
"docs": "Use this task to tag customers by tier, based on how many orders they've placed or by the sum of all their order totals (i.e. total spend). Optionally, configure a customers query, limiting the set of customers that are processed. You may also configure an order query, specifying for things like a rolling time period, or fulfillment status. This task is useful for rewarding customers who establish or maintain a specific spend level.\n\nThe options for querying customers and orders use the same query syntax as the \"Customers\" and \"Orders\" section of the Shopify admin area. For example, to only count customers with enabled online accounts who are tagged with \"qualifies\", use this customers query:\n\n```\nstate:enabled tag:qualifies\n```\n\nTo count paid orders from the last 365 days, use this orders query:\n\n```\nfinancial_status:paid created_at:>={{ \"now\" | date: \"%s\" | minus: 31536000 | date: \"%Y-%m-%d\" }}\n```",
"halt_action_run_sequence_on_error": false,
"name": "Tag customers by order tier",
"online_store_javascript": null,
"options": {
"order_minimums_and_customer_tags__keyval_required": {
"10": "10-orders",
"100": "100-orders"
},
"only_keep_the_customer_tag_for_the_highest_order_minimum__boolean": true,
"tag_customers_by_number_of_orders__boolean": true,
"tag_customers_by_sum_of_order_totals__boolean": null,
"only_process_customers_matching_this_query": "state:enabled tag:qualifies",
"only_count_orders_matching_this_query": "financial_status:paid created_at:>={{ \"now\" | date: \"%s\" | minus: 31536000 | date: \"%Y-%m-%d\" }}",
"run_hourly__boolean": null,
"run_daily__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Preferred option order:\n\n {{ options.order_minimums_and_customer_tags__keyval_required }}\n {{ options.only_keep_the_customer_tag_for_the_highest_order_minimum__boolean }}\n {{ options.tag_customers_by_number_of_orders__boolean }}\n {{ options.tag_customers_by_sum_of_order_totals__boolean }}\n {{ options.only_process_customers_matching_this_query }}\n {{ options.only_count_orders_matching_this_query }}\n {{ options.run_hourly__boolean }}\n {{ options.run_daily__boolean }}\n{% endcomment %}\n\n{% if options.tag_customers_by_number_of_orders__boolean and options.tag_customers_by_sum_of_order_totals__boolean %}\n {% error \"Choose only one of 'Tag customers by number of orders' and 'Tag customers by sum of order totals'. :)\" %}\n{% elsif options.tag_customers_by_number_of_orders__boolean == false and options.tag_customers_by_sum_of_order_totals__boolean == false %}\n {% error \"Choose one of 'Tag customers by number of orders' and 'Tag customers by sum of order totals'. :)\" %}\n{% endif %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% capture bulk_operation_query %}\n query {\n customers(query: {{ options.only_process_customers_matching_this_query | json }}) {\n edges {\n node {\n __typename\n id\n email\n tags\n state\n orders(query: {{ options.only_count_orders_matching_this_query | json }}) {\n edges {\n node {\n __typename\n id\n name\n totalPriceSet {\n shopMoney {\n amount\n }\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture bulkOperation_objects_jsonl %}\n {\"__typename\":\"Customer\",\"id\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\",\"tags\":[],\"state\":\"ENABLED\"}\n {% if options.tag_customers_by_number_of_orders__boolean -%}\n {% assign count = options.order_minimums_and_customer_tags__keyval_required.first.first | times: 1 -%}\n {% for n in (0..count) -%}\n {\"__typename\":\"Order\",\"id\":\"gid:\\/\\/shopify\\/Order\\/1234567890\",\"totalPriceSet\":{\"shopMoney\":{\"amount\":\"10.0\"}},\"__parentId\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\"}\n {% endfor -%}\n {% elsif options.tag_customers_by_sum_of_order_totals__boolean -%}\n {\"__typename\":\"Order\",\"id\":\"gid:\\/\\/shopify\\/Order\\/1234567890\",\"totalPriceSet\":{\"shopMoney\":{\"amount\":{{ options.order_minimums_and_customer_tags__keyval_required.first.first | json}}}},\"__parentId\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\"}\n {% endif %}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = bulkOperation_objects_jsonl | parse_jsonl %}\n {% endif %}\n\n {% assign customers = bulkOperation.objects | where: \"__typename\", \"Customer\" %}\n {% assign orders = bulkOperation.objects | where: \"__typename\", \"Order\" %}\n \n {% for customer in customers %}\n {% assign customer_orders = orders | where: \"__parentId\", customer.id %}\n {% assign customer_orders_value = nil %}\n\n {% if options.tag_customers_by_number_of_orders__boolean %}\n {% assign customer_orders_value = customer_orders.size %}\n {% elsif options.tag_customers_by_sum_of_order_totals__boolean %}\n {% assign customer_orders_value = 0 %}\n {% for order in customer_orders %}\n {% assign customer_orders_value = customer_orders_value | plus: order.totalPriceSet.shopMoney.amount %}\n {% endfor %}\n {% endif %}\n\n {% if customer_orders != empty %}\n {% assign customer_orders_names = customer_orders | map: \"name\" %}\n {% log customer_id: customer.id, customer_email: customer.email, customer_tags: customer.tags, customer_orders_names: customer_orders_names, customer_orders_value: customer_orders_value %}\n {% endif %}\n\n {% assign best_fitting_tag_minimum = 0 %}\n {% assign best_fitting_tag = nil %}\n {% assign all_fitting_tags = array %}\n {% assign all_possible_tags = array %}\n\n {% for pair in options.order_minimums_and_customer_tags__keyval_required %}\n {% assign tag_minimum = pair[0] | times: 1 %}\n {% assign tag = pair[1] %}\n\n {% assign all_possible_tags[all_possible_tags.size] = tag %}\n\n {% if customer_orders_value >= tag_minimum %}\n {% assign all_fitting_tags[all_fitting_tags.size] = tag %}\n\n {% if tag_minimum >= best_fitting_tag_minimum %}\n {% assign best_fitting_tag = tag %}\n {% assign best_fitting_tag_minimum = tag_minimum %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% assign tags_to_add = array %}\n {% assign tags_to_remove = array %}\n\n {% if best_fitting_tag %}\n {% unless customer.tags contains best_fitting_tag %}\n {% assign tags_to_add[tags_to_add.size] = best_fitting_tag %}\n {% endunless %}\n {% endif %}\n\n {% if options.only_keep_the_customer_tag_for_the_highest_order_minimum__boolean %}\n {% for tag in all_possible_tags %}\n {% if tag != best_fitting_tag and customer.tags contains tag %}\n {% assign tags_to_remove[tags_to_remove.size] = tag %}\n {% endif %}\n {% endfor %}\n {% else %}\n {% for tag in all_fitting_tags %}\n {% unless tag == best_fitting_tag or customer.tags contains tag %}\n {% assign tags_to_add[tags_to_add.size] = tag %}\n {% endunless %}\n {% endfor %}\n {% endif %}\n\n {% if tags_to_add != empty or tags_to_remove != empty %}\n {% action \"shopify\" %}\n mutation {\n {% if tags_to_add != empty %}\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n node {\n ... on Customer {\n id\n email\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n {% endif %}\n\n {% if tags_to_remove != empty %}\n tagsRemove(\n id: {{ customer.id | json }}\n tags: {{ tags_to_remove | json }}\n ) {\n node {\n ... on Customer {\n id\n email\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n {% endif %}\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\n\n{% if options.run_hourly__boolean %}\n mechanic/scheduler/hourly\n{% elsif options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}\n\nmechanic/shopify/bulk_operation",
"tags": [
"Customers",
"Loyalty",
"Retention",
"Tag"
]
}