-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathauto-tag-customers-with-the-location-of-their-purchase.json
23 lines (23 loc) · 6.82 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
{
"docs": "When an order is created, this task adds the location name of the purchase to the customer's tags. Useful for stores with multiple Shopify-powered POS locations.\n\nThis task will run for each new order that's created, applying the POS location name as a customer tag. Optionally, define a tag to be used for online orders.\n\nRun this task manually to have Mechanic scan each customer with an 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": "{% assign tag_for_online_orders = options.tag_for_online_orders %}\n\n{% if event.topic contains \"shopify/orders\" %}\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n customer {\n id\n tags\n }\n retailLocation {\n name\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\"\n },\n \"retailLocation\": {\n \"name\": \"Storefront\"\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign order = result.data.order %}\n {% assign customer = order.customer %}\n {% assign location_name = order.retailLocation.name | default: tag_for_online_orders %}\n\n {% unless customer == blank or location_name == blank or customer.tags contains location_name %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ location_name | json }}\n ) {\n node {\n ... on Customer {\n id\n displayName\n tags\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endunless %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% comment %}\n -- get IDs of all customers who have placed an order\n {% endcomment %}\n\n {% assign cursor = nil %}\n {% assign customer_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: \"number_of_orders > 0\"\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 {% assign customer_segment_members = result.data.customerSegmentMembers.edges | map: \"node\" %}\n\n {% comment %}\n -- remove the \"SegmentMember\" portion from IDs for easier use in querying each customer for additional data not available in the segment resource\n {% endcomment %}\n\n {% for customer_segment_member in customer_segment_members %}\n {% assign customer_ids[customer_ids.size] = customer_segment_member.id | remove: \"SegmentMember\" %}\n {% endfor %}\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_who_have_placed_an_order: customer_ids.size %}\n {% endunless %}\n\n {% if event.preview %}\n {% assign customer_ids[0] = \"gid://shopify/Customer/1234567890\" %}\n {% endif %}\n\n {% for customer_id in customer_ids %}\n {% comment %}\n -- get all relevant order data for this customer\n {% endcomment %}\n\n {% assign cursor = nil %}\n {% assign tags_to_add = array %}\n\n {% for n in (1..10) %}\n {% capture query %}\n query {\n customer(id: {{ customer_id | json }}) {\n id\n tags\n orders(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n name\n retailLocation {\n name\n }\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 \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"orders\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"retailLocation\": {\n \"name\": \"Storefront\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign customer = result.data.customer %}\n\n {% comment %}\n -- process orders to see which location names should be set as tags\n {% endcomment %}\n\n {% for order in customer.orders.nodes %}\n {% assign location_name = order.retailLocation.name | default: tag_for_online_orders %}\n\n {% unless location_name == blank or customer.tags contains location_name or tags_to_add contains location_name %}\n {% assign tags_to_add = tags_to_add | push: location_name %}\n {% endunless %}\n {% endfor %}\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 {% if tags_to_add != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tags_to_add | sort_natural | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger",
"tags": [
"Auto-Tag",
"Customers",
"Location",
"Orders"
]
}