forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-tag-customers-having-a-rolling-minimum-total-spend.json
28 lines (28 loc) · 10 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
27
28
{
"docs": "Use this task to tag customers who reach a certain spending threshold, by scanning a rolling period of order history. Useful for rewarding customers who keep a consistent spend total.\n\nRun this task manually to scan all of your customers. **This may take some time!**\n\nThis task will also scan all customers on a nightly basis, and will run for a single customer every time a customer's order is paid or cancelled.\n\nNote: By default, Mechanic only scans the last 60 days of order history. To change this, [enable the option in Mehanic's settings](https://learn.mechanic.dev/platform/shopify/read-all-orders#configuration).",
"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
},
"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{% assign days_in_seconds = days_of_order_history | times: 24 | times: 60 | times: 60 %}\n{% assign min_created_at = \"now\" | date: \"%s\" | times: 1 | minus: days_in_seconds %}\n{% assign min_date_created_at = min_created_at | date: \"%F\" %}\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{% capture orders_search_query -%}\n created_at:>{{ min_date_created_at }} financial_status:paid,partially_paid,partially_refunded\n{%- endcapture %}\n\n{% if event.topic == \"shopify/orders/create\" or event.topic == \"shopify/orders/updated\" %}\n {% assign customer_tags = order.customer.tags | split: \", \" %}\n\n {% if monitor_customer_tag != blank %}\n {% unless customer_tags contains monitor_customer_tag %}\n {% log \"Customer does not have the required tag for monitoring; skipping.\" %}\n {% break %}\n {% endunless %}\n {% endif %}\n\n {% log orders_search_query: orders_search_query %}\n\n {% assign customer = hash %}\n {% assign customer[\"id\"] = order.customer.admin_graphql_api_id %}\n {% assign customer[\"email\"] = order.customer.email %}\n {% assign customer[\"tags\"] = customer_tags %}\n {% assign customer[\"orders\"] = array %}\n\n {% assign cursor = nil %}\n\n {% for n in (1..10) %}\n {% capture query %}\n query {\n customer(id: {{ order.customer.admin_graphql_api_id | json }}) {\n orders(\n first: 250\n after: {{ cursor | json }}\n query: {{ orders_search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n name\n createdAt\n displayFinancialStatus\n transactions {\n status\n kind\n amountSet {\n shopMoney {\n amount\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign customer[\"orders\"]\n = result.data.customer.orders.nodes\n | default: array\n | concat: customer[\"orders\"]\n %}\n\n {% if result.data.customer.orders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.customer.orders.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% assign customers = array | push: customer %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" or event.topic == \"mechanic/scheduler/daily\" %}\n {% assign customers_search_query = \"orders_count:>0\" %}\n\n {% if monitor_customer_tag != blank %}\n {% capture customers_search_query -%}\n {{ customers_search_query }} AND tag:{{ monitor_customer_tag | json }}\n {%- endcapture %}\n {% endif %}\n\n {% log\n customers_search_query: customers_search_query,\n orders_search_query: orders_search_query\n %}\n\n {% capture bulk_operation_query %}\n query {\n customers(\n query: {{ customers_search_query | json }}\n ) {\n edges {\n node {\n __typename\n id\n displayName\n email\n tags\n orders(\n query: {{ orders_search_query | json }}\n ) {\n edges {\n node {\n __typename\n id\n name\n createdAt\n displayFinancialStatus\n transactions {\n status\n kind\n amountSet {\n shopMoney {\n amount\n }\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\n {% break %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign bulk_customers = bulkOperation.objects | where: \"__typename\", \"Customer\" %}\n {% assign bulk_orders = bulkOperation.objects | where: \"__typename\", \"Order\" %}\n\n {% assign customers = array %}\n\n {% for bulk_customer in bulk_customers %}\n {% assign customer = hash %}\n {% assign customer[\"id\"] = bulk_customer.id %}\n {% assign customer[\"email\"] = bulk_customer.email %}\n {% assign customer[\"tags\"] = bulk_customer.tags %}\n {% assign customer[\"orders\"]\n = bulk_orders\n | where: \"__parentId\", bulk_customer.id\n | default: array\n %}\n {% assign customers = customers | push: customer %}\n {% endfor %}\n{% endif %}\n\n{% if event.preview %}\n {% capture customers_json %}\n [\n {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"email\": \"[email protected]\",\n \"orders\": [\n {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"transactions\": [\n {\n \"status\": \"SUCCESS\",\n \"kind\": \"SALE\",\n \"amountSet\": {\n \"shopMoney\": {\n \"amount\": {{ minimum_total_spent | plus: 100.0 | json }}\n }\n }\n },\n {\n \"status\": \"SUCCESS\",\n \"kind\": \"REFUND\",\n \"amountSet\": {\n \"shopMoney\": {\n \"amount\": \"99.00\"\n }\n }\n }\n ]\n }\n ]\n }\n ]\n {% endcapture %}\n\n {% assign customers = customers_json | parse_json %}\n{% endif %}\n\n{% for customer in customers %}\n {% assign customer_total = 0.0 %}\n {% assign customer_orders_count = 0 %}\n\n {% for order in customer.orders %}\n {% for transaction in order.transactions %}\n {% if transaction.status != \"SUCCESS\" %}\n {% continue %}\n {% endif %}\n\n {% if transaction.kind == \"REFUND\" %}\n {% assign customer_total = customer_total | minus: transaction.amountSet.shopMoney.amount %}\n\n {% elsif transaction.kind == \"SALE\" or transaction.kind == \"CAPTURE\" %}\n {% assign customer_total = customer_total | plus: transaction.amountSet.shopMoney.amount %}\n {% endif %}\n {% endfor %}\n\n {% assign customer_orders_count = customer_orders_count | plus: 1 %}\n {% endfor %}\n\n {% assign add_tag = false %}\n {% assign remove_tag = false %}\n\n {% capture log_line_prefix %}{{ customer.email | default: customer.id }} spent {{ customer_total }} across {{ customer_orders_count }} orders, {% endcapture %}\n\n {% if customer_total >= minimum_total_spent %}\n {% if customer.tags contains customer_tag_to_apply %}\n {% log %}{{ log_line_prefix | append: \"qualifying them for the tag. They were already tagged, so there's nothing to do.\" | json }}{% endlog %}\n\n {% else %}\n {% log %}{{ log_line_prefix | append: \"qualifying them for the tag\" | json }}{% endlog %}\n {% assign add_tag = true %}\n {% endif %}\n\n {% else %}\n {% if customer.tags contains customer_tag_to_apply %}\n {% log %}{{ log_line_prefix | append: \"and did not qualify for the tag\" | json }}{% endlog %}\n {% assign remove_tag = true %}\n\n {% else %}\n {% log %}{{ log_line_prefix | append: \"and did not qualify for the tag. They were not previously tagged, so there's nothing to do.\" | json }}{% endlog %}\n {% endif %}\n {% endif %}\n\n {% if add_tag or remove_tag %}\n {% action \"shopify\" %}\n mutation {\n {% if add_tag %}tagsAdd{% elsif remove_tag %}tagsRemove{% endif %}(\n id: {{ customer.id | json }}\n tags: {{ customer_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}\n",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/scheduler/daily",
"mechanic/shopify/bulk_operation",
"shopify/orders/create",
"shopify/orders/updated"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/scheduler/daily\nmechanic/shopify/bulk_operation\nshopify/orders/create\nshopify/orders/updated",
"tags": [
"Auto-Tag",
"Customers",
"Spend"
]
}