-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-tag-customers-by-order-app.json
24 lines (24 loc) · 7.81 KB
/
auto-tag-customers-by-order-app.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": "This task scans your incoming orders, tagging customers based on the Shopify app responsible for creating each order. Run this task manually to tag your customers based on their historical orders.\r\n\r\nConfigure this task with app titles on the left, and customer tags to add on the right.\r\n\r\nUse the \"Test mode\" option to see what the task intends to do, without actually having the task actually make any changes to your customers.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers by order app",
"online_store_javascript": null,
"options": {
"app_titles_and_customer_tags__keyval_required": null,
"test_mode__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if event.topic == \"shopify/orders/create\" %}\n {% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n customer {\n id\n tags\n }\n events(\n first: 1\n sortKey: CREATED_AT\n query: \"verb:placed\"\n ) {\n edges {\n node {\n id\n ... on BasicEvent {\n appTitle\n }\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 \"order\": {\n \"customer\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": []\n },\n \"events\": {\n \"edges\": [\n {\n \"node\": {\n \"appTitle\": {{ options.app_titles_and_customer_tags__keyval_required.first.first | json }}\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% assign app_title = result.data.order.events.edges.first.node.appTitle %}\n {% assign tag_to_add = options.app_titles_and_customer_tags__keyval_required[app_title] %}\n\n {% if app_title == blank %}\n {% log message: \"This order was not created by an app. Skipping.\" %}\n {% elsif tag_to_add == blank %}\n {% log message: \"No tag found for this app. Skipping.\", app_title: app_title %}\n {% elsif result.data.order.customer.tags contains tag_to_add %}\n {% log message: \"The customer already has the tag for this app. Skipping.\", app_title: app_title, tag_to_add: tag_to_add %}\n {% elsif options.test_mode__boolean %}\n {% action \"echo\" customer_id: result.data.order.customer.id, order_app_title: app_title, customer_tag_to_add: tag_to_add %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ result.data.order.customer.id | json }}\n tags: {{ tag_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n customers {\n edges {\n node {\n __typename\n id\n tags\n orders {\n edges {\n node {\n __typename\n id\n events(\n first: 1\n sortKey: CREATED_AT\n query: \"verb:placed\"\n ) {\n edges {\n node {\n __typename\n id\n ... on BasicEvent {\n appTitle\n }\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{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% if event.preview %}\n {% capture bulkOperation_json %}\n {\n \"objects\": [\n {\n \"__typename\": \"Customer\",\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"tags\": []\n },\n {\n \"__typename\": \"Order\",\n \"id\": \"gid://shopify/Order/1234567890\",\n \"__parentId\": \"gid://shopify/Customer/1234567890\"\n },\n {\n \"__typename\": \"BasicEvent\",\n \"id\":\" gid://shopify/BasicEvent/1234567890\",\n \"appTitle\": {{ options.app_titles_and_customer_tags__keyval_required.first.first | json }},\n \"__parentId\": \"gid://shopify/Order/1234567890\"\n }\n ]\n }\n {% endcapture %}\n\n {% assign bulkOperation = bulkOperation_json | parse_json %}\n {% endif %}\n\n {% assign customers = bulkOperation.objects | where: \"__typename\", \"Customer\" %}\n {% assign orders = bulkOperation.objects | where: \"__typename\", \"Order\" %}\n {% assign events = bulkOperation.objects | where: \"__typename\", \"BasicEvent\" %}\n {% assign app_title_counts = hash %}\n\n {% for event in events %}\n {% assign app_title = event.appTitle %}\n\n {% if app_title == blank %}\n {% assign app_title = \"(not placed via app)\" %}\n {% endif %}\n\n {% assign app_title_counts[app_title] = app_title_counts[app_title] | plus: 1 %}\n {% endfor %}\n\n {% log app_title_counts: app_title_counts %}\n\n {% if options.test_mode__boolean %}\n {% assign summaries = array %}\n {% endif %}\n \n {% for customer in customers %}\n {% assign customer_order_ids = orders | where: \"__parentId\", customer.id | map: \"id\" %}\n {% assign customer_app_titles = array %}\n {% for event in events %}\n {% if event.appTitle != blank and customer_order_ids contains event.__parentId %}\n {% assign customer_app_titles[customer_app_titles.size] = event.appTitle %}\n {% endif %}\n {% endfor %}\n\n {% assign tags_to_add = array %}\n {% for app_title in customer_app_titles %}\n {% assign tag = options.app_titles_and_customer_tags__keyval_required[app_title] %}\n\n {% if tag == blank or customer.tags contains tag %}\n {% continue %}\n {% endif %}\n\n {% assign tags_to_add[tags_to_add.size] = tag %}\n {% endfor %}\n\n {% if tags_to_add != empty %}\n {% if options.test_mode__boolean %}\n {% assign summary = hash %}\n {% assign summary[\"customer_id\"] = customer.id %}\n {% assign summary[\"customer_app_titles\"] = customer_app_titles %}\n {% assign summary[\"customer_tags_to_add\"] = tags_to_add %}\n {% assign summaries[summaries.size] = summary %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer.id | json }}\n tags: {{ tags_to_add | uniq | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if options.test_mode__boolean %}\n {% action \"echo\" summaries %}\n {% endif %}\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",
"Orders"
]
}