-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-tag-customers-with-the-location-of-their-purchase.json
24 lines (24 loc) · 4.64 KB
/
auto-tag-customers-with-the-location-of-their-purchase.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
{
"docs": "When an order is created, this task adds the location of the purchase to the customer's tags. Useful for stores with multiple Shopify-powered locations.\n\nThis task will run for each new order that's created, applying the order location as a customer tag. Optionally, define a tag to be used for orders that have no physical location.\r\n\r\nRun this task manually to have Mechanic scan your entire customer base, and each customer's order history.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers with the location of their purchase",
"online_store_javascript": null,
"options": {
"tag_for_online_orders": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.topic contains \"shopify/orders\" %}\n {% if event.preview %}\n {% capture order_json %}\n {\n \"location\": {\n \"name\": \"Storefront\"\n },\n \"customer\": {\n \"admin_graphql_api_id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": \"\"\n }\n }\n {% endcapture %}\n\n {% assign order = order_json | parse_json %}\n {% endif %}\n\n {% assign customer_tags = order.customer.tags | split: \", \" %}\n\n {% assign location = order.location.name | default: options.tag_for_online_orders %}\n\n {% unless location == blank or customer_tags contains location %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.customer.admin_graphql_api_id | json }}\n tags: [{{ location | json }}]\n ) {\n node {\n ... on Customer {\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n customers(query: \"orders_count:>0\") {\n edges {\n node {\n __typename\n id\n tags\n orders {\n edges {\n node {\n __typename\n id\n physicalLocation {\n name\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 objects_jsonl %}\n {\"__typename\":\"Customer\",\"id\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\",\"tags\":[]}\n {\"__typename\":\"Order\",\"id\":\"gid:\\/\\/shopify\\/Order\\/1234567890\",\"physicalLocation\":{\"name\":\"Storefront\"},\"__parentId\":\"gid:\\/\\/shopify\\/Customer\\/1234567890\"}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = 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 tags_to_add = array %}\n\n {% for order in customer_orders %}\n {% assign location = order.physicalLocation.name | default: options.tag_for_online_orders %}\n\n {% if location == blank or customer.tags contains location %}\n {% continue %}\n {% endif %}\n\n {% assign tags_to_add[tags_to_add.size] = location %}\n {% endfor %}\n\n {% if tags_to_add != empty %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tags_to_add | uniq | json }}\n ) {\n node {\n ... on Customer {\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Customers",
"Location",
"Orders"
]
}