forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag-and-segment-customers-by-days-since-last-order.json
31 lines (31 loc) · 8.59 KB
/
tag-and-segment-customers-by-days-since-last-order.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
{
"docs": "This task will run daily to query all customers in your shop who have placed an order, tagging each with whichever configured tag is paired with the closest \"days since last order\" threshold. This is great way to keep tabs on who is active in your store, and who you might need to reconnect with.\n\nOptionally, you can select \"Create customer segments by tag\" to have the task create new [customer segments](https://help.shopify.com/en/manual/customers/customer-segmentation/customer-segments) for each tag configured in this task.\n\nThe task comes preconfigured with a \"Burn\" tag set at 365 or more days since last order, a \"Churn\" tag set at 180 days or but less than 365 days since last order, an \"At Risk\" tag set at 90 days or more but less than 180 days since last order, and an \"Active\" tag for anyone who has placed an order more recently than 90 days. Change these configuration values to whatever makes the most sense for your business.\n\nNote: the order in which you enter the tags and days since pairs does not matter; at run time, the task will first sort by the largest days since value, and then make tagging decisions accordingly.",
"halt_action_run_sequence_on_error": false,
"name": "Tag and segment customers by days since last order",
"online_store_javascript": null,
"options": {
"customer_tags_and_days_since_last_order__keyval_number_required": {
"Burn": "365",
"Churn": "180",
"At Risk": "90",
"Active": "0"
},
"create_customer_segments_by_tag__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign customer_tags_and_days_since_last_order = options.customer_tags_and_days_since_last_order__keyval_number_required %}\n{% assign create_customer_segments_by_tag = options.create_customer_segments_by_tag__boolean %}\n{% assign now_s = \"now\" | date: \"%s\" %}\n\n{% assign tag_rules = array %}\n\n{% for keyval in customer_tags_and_days_since_last_order %}\n {% assign customer_tag = keyval[0] %}\n {% assign days_since_last_order = keyval[1] %}\n\n {% if customer_tag == blank or days_since_last_order == blank or days_since_last_order < 0 %}\n {% continue %}\n {% endif %}\n\n {% assign tag_rule = hash %}\n {% assign tag_rule[\"days\"] = days_since_last_order %}\n {% assign tag_rule[\"tag\"] = customer_tag %}\n {% assign tag_rule[\"threshold_s\"]\n = days_since_last_order\n | times: -86400\n | plus: now_s\n %}\n {% assign tag_rules = tag_rules | push: tag_rule %}\n{% endfor %}\n\n{% assign tag_rules = tag_rules | sort: \"days\" | reverse %}\n{% assign configured_tags = tag_rules | map: \"tag\" %}\n\n{% if tag_rules == blank %}\n {% error \"Please enter at least one customer tag and days since last order combo.\" %}\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(\n query: \"orders_count:>0\"\n ) {\n edges {\n node {\n id\n tags\n lastOrder {\n processedAt\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{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% log tag_rules: tag_rules %}\n\n {% if event.preview %}\n {% capture bulkOperation_objects_jsonl %}\n {\"id\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\",\"lastOrder\":{\"processedAt\":{{ tag_rules[0].threshold_s | json }}}}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = bulkOperation_objects_jsonl | parse_jsonl %}\n {% endif %}\n\n {% assign customers = bulkOperation.objects %}\n\n {% for customer in customers %}\n {% assign tag_should_have = nil %}\n\n {% assign customer_last_order_processed_at_s = customer.lastOrder.processedAt | date: \"%s\" | times: 1 %}\n\n {% for tag_rule in tag_rules %}\n {% if customer_last_order_processed_at_s <= tag_rule.threshold_s %}\n {% assign tag_should_have = tag_rule.tag %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% unless tag_should_have == blank or customer.tags contains tag_should_have %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tag_should_have | json }}\n ) {\n node {\n ... on Customer {\n id\n email\n tags\n lastOrder {\n name\n processedAt\n }\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n\n {% assign tags_to_remove = array %}\n\n {% for customer_tag in customer.tags %}\n {% if configured_tags contains customer_tag and tag_should_have != customer_tag %}\n {% assign tags_to_remove = tags_to_remove | push: customer_tag %}\n {% endif %}\n {% endfor %}\n\n {% if tags_to_remove != blank %}\n {% action \"shopify\" %}\n mutation {\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 lastOrder {\n name\n processedAt\n }\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n\n {% if create_customer_segments_by_tag %}\n {% assign cursor = nil %}\n {% assign segments = array %}\n\n {% for n in (1..100) %}\n {% capture query %}\n query {\n segments(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n name\n query\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"segments\": {\n \"nodes\": []\n }\n }\n } \n {% endcapture %}\n \n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign segments = segemnts | concat: result.data.segments.nodes %}\n {% comment %}\n \n {% assign segments\n = result.data.segments.nodes\n | default: array\n | concat: segments\n %}\n {% endcomment %}\n\n {% if result.data.segments.pageInfo.hasNextPage %}\n {% assign cursor = result.data.segments.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% for tag_rule in tag_rules %}\n {%- capture segment_rule -%}\n customer_tags CONTAINS '{{ tag_rule.tag }}'\n {%- endcapture -%}\n\n {% assign matched_segment\n = segments\n | where: \"name\", tag_rule.tag\n | where: \"query\", segment_rule\n %}\n\n {% if matched_segment != blank %}\n {% log\n message: \"This customer segment already exists.\",\n matched_segment: matched_segment\n %}\n {% continue %}\n {% endif %}\n\n {% action \"shopify\" %}\n mutation {\n segmentCreate(\n name: {{ tag_rule.tag | json }}\n query: {{ segment_rule | json }}\n ) {\n segment {\n id\n name\n query\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n{% endif %}\n",
"subscriptions": [
"mechanic/shopify/bulk_operation",
"mechanic/scheduler/daily",
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/shopify/bulk_operation\nmechanic/scheduler/daily\nmechanic/user/trigger",
"tags": [
"Customers",
"Loyalty",
"Segment",
"Tag"
]
}