-
Notifications
You must be signed in to change notification settings - Fork 35
/
auto-tag-customers-upon-product-purchase.json
28 lines (28 loc) · 10.4 KB
/
auto-tag-customers-upon-product-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
25
26
27
28
{
"docs": "Configure this task with search terms that describe certain products, and this task will apply the tags of your choice to every customer who purchases a matching product or product variant. Run this task manually to tag all customers who have a qualifying order already on file.\n\nThis task auto-tags customers who have paid orders on file for products or product variants that match the search query you add.\n\nSample searches:\n\n* A specific SKU: search product variants for `sku:ABC123`\n* All products with a certain tag: search products for `tag:holiday`\n* All products with a certain type: search products for `product_type:\"Gift Card\"`\n* A product with a certain title: search products for `title:\"Short sleeve t-shirt\"`\n\nTo ensure expected results, use this task with test mode enabled, before disabling test mode.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-tag customers when they purchase a matching product",
"online_store_javascript": null,
"options": {
"search_query__required": "sku:ABC123",
"search_for_products__boolean": false,
"search_for_product_variants__boolean": true,
"customer_tags_to_apply__required_array": [],
"test_mode__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Ordering the task options:\n\n {{ options.search_query__required }}\n {{ options.search_for_products__boolean }}\n {{ options.search_for_product_variants__boolean }}\n {{ options.customer_tags_to_apply__required_array }}\n {{ options.test_mode__boolean }}\n{% endcomment %}\n\n{% if options.search_for_products__boolean and options.search_for_product_variants__boolean %}\n {% error \"Choose only one of 'Search for products' and 'Search for product variants'. :)\" %}\n{% elsif options.search_for_products__boolean == false and options.search_for_product_variants__boolean == false %}\n {% error \"Choose either 'Search for products' and 'Search for product variants'.\" %}\n{% endif %}\n\n{% assign cursor = nil %}\n{% assign qualifier_resource_ids = array %}\n{% assign qualifier_resource_summaries = array %}\n{% assign jobs = array %}\n\n{% for n in (0..100) %}\n {% capture query %}\n query {\n {% if options.search_for_products__boolean %}\n resources: products(\n {% else %}\n resources: productVariants(\n {% endif %}\n first: 250\n after: {{ cursor | json }}\n query: {{ options.search_query__required | json }}\n ) {\n edges {\n node {\n id\n legacyResourceId\n {% if options.search_for_products__boolean %}\n displayName: title\n {% else %}\n displayName\n {% endif %}\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 \"resources\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"displayName\": \"Bundle\",\n \"legacyResourceId\": \"1234567890\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% log %}\n {\n \"qualifier_resources\": {{ result.data.resources.edges | map: \"node\" | json }}\n }\n {% endlog %}\n\n {% assign qualifier_resource_ids = result.data.resources.edges | map: \"node\" | map: \"legacyResourceId\" | concat: qualifier_resource_ids %}\n\n {% if result.data.resources.pageInfo.hasNextPage %}\n {% assign cursor = result.data.resources.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{% if event.topic contains \"shopify/orders/\" %}\n {% if event.preview %}\n {% capture order_json %}\n {\n \"financial_status\": \"paid\",\n \"name\": \"#1234\",\n \"customer\": {\n \"email\": \"[email protected]\",\n \"tags\": \"\",\n \"admin_graphql_api_id\": \"gid://shopify/Customer/1234567890\"\n },\n \"line_items\": [\n {\n \"product_id\": 1234567890,\n \"variant_id\": 1234567890,\n \"title\": \"IPod Nano - 8GB\"\n }\n ]\n }\n {% endcapture %}\n\n {% assign order = order_json | parse_json %}\n {% endif %}\n\n {% assign order_qualifies = false %}\n {% assign order_qualifying_resource = nil %}\n\n {% if order.financial_status == \"paid\" and order.customer %}\n {% for line_item in order.line_items %}\n {% if options.search_for_products__boolean %}\n {% assign id = line_item.product_id | append: \"\" %}\n {% else %}\n {% assign id = line_item.variant_id | append: \"\" %}\n {% endif %}\n\n {% if qualifier_resource_ids contains id %}\n {% assign order_qualifies = true %}\n {% assign order_qualifying_resource = line_item.title %}\n {% break %}\n {% endif %}\n {% endfor %}\n {% endif %}\n\n {% if order_qualifies %}\n {% assign job = hash %}\n {% assign job[\"customer_id\"] = order.customer.admin_graphql_api_id %}\n {% assign job[\"customer_email\"] = order.customer.email %}\n {% assign job[\"customer_existing_tags\"] = order.customer.tags | split: \", \" %}\n {% assign job[\"reason\"] = order_qualifying_resource | append: \", in order \" | append: order.name %}\n {% assign jobs[jobs.size] = job %}\n {% endif %}\n\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 email\n tags\n orders(\n query: \"financial_status:paid\"\n ) {\n edges {\n node {\n __typename\n id\n name\n lineItems {\n edges {\n node {\n __typename\n id\n {% if options.search_for_products__boolean %}\n resource: product {\n legacyResourceId\n displayName: title\n }\n {% else %}\n resource: variant {\n legacyResourceId\n displayName\n }\n {% endif %}\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\": \"LineItem\",\n \"resource\": {\n \"legacyResourceId\": \"1234567890\",\n \"displayName\": \"IPod Nano - 8GB\"\n },\n \"__parent\": {\n \"name\": \"#1234\",\n \"__parent\": {\n \"id\": \"gid://shopify/Customer/1234567890\",\n \"email\": \"[email protected]\",\n \"tags\": []\n }\n }\n }\n ]\n }\n {% endcapture %}\n {% assign bulkOperation = bulkOperation_json | parse_json %}\n {% endif %}\n\n {% assign lineItems = bulkOperation.objects | where: \"__typename\", \"LineItem\" | where: \"resource\" %}\n {% for lineItem in lineItems %}\n {% assign resource = lineItem.resource %}\n {% assign order = lineItem.__parent %}\n {% assign customer = order.__parent %}\n\n {% if qualifier_resource_ids contains resource.legacyResourceId %}\n {% assign job = hash %}\n {% assign job[\"customer_id\"] = customer.id %}\n {% assign job[\"customer_email\"] = customer.email %}\n {% assign job[\"customer_existing_tags\"] = customer.tags %}\n {% assign job[\"reason\"] = resource.displayName | append: \", in order \" | append: order.name %}\n {% assign jobs[jobs.size] = job %}\n {% endif %}\n {% endfor %}\n{% endif %}\n\n{% assign customer_ids_with_jobs = jobs | map: \"customer_id\" | uniq %}\n{% for customer_id in customer_ids_with_jobs %}\n {% assign customer_jobs = jobs | where: \"customer_id\", customer_id %}\n {% assign customer_existing_tags = customer_jobs[0].customer_existing_tags %}\n {% assign customer_email = customer_jobs[0].customer_email %}\n {% assign reasons = customer_jobs | map: \"reason\" %}\n\n {% assign tags_to_add = array %}\n {% for tag in options.customer_tags_to_apply__required_array %}\n {% unless customer_existing_tags contains tag %}\n {% assign tags_to_add[tags_to_add.size] = tag %}\n {% endunless %}\n {% endfor %}\n\n {% if options.test_mode__boolean %}\n {% action \"echo\" %}\n {\n \"customer_id\": {{ customer_id | json }},\n \"customer_email\": {{ customer_email | json }},\n \"customer_purchases\": {{ reasons | json }},\n \"customer_existing_tags\": {{ customer_existing_tags | json }},\n \"customer_tags_to_add\": {{ tags_to_add | json }}\n }\n {% endaction %}\n {% elsif tags_to_add == empty %}\n {% capture log_message %}Customer {{ customer.id }} already has all applicable tags ({{ tags_applicable | join: \", \" }}); nothing to do.{% endcapture %}\n {% log log_message %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ customer_id | json }}\n tags: {{ tags_to_add | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}\n",
"subscriptions": [
"shopify/orders/paid",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/orders/paid\nmechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Auto-Tag",
"Customers",
"Orders",
"Products"
]
}