Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to tag customers by tier, based on how many orders they've placed or by the sum of all their order totals (i.e. total spend). Optionally, configure a customers query, limiting the set of customers that are processed. You may also configure an order query, specifying for things like a rolling time period, or fulfillment status. This task is useful for rewarding customers who establish or maintain a specific spend level.
Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes order minimums and customer tags, only keep the customer tag for the highest order minimum, tag customers by number of orders, tag customers by sum of order totals, only process customers matching this query, only count orders matching this query, run hourly, and run daily.
Use this task to tag customers by tier, based on how many orders they've placed or by the sum of all their order totals (i.e. total spend). Optionally, configure a customers query, limiting the set of customers that are processed. You may also configure an order query, specifying for things like a rolling time period, or fulfillment status. This task is useful for rewarding customers who establish or maintain a specific spend level.
The options for querying customers and orders use the same query syntax as the "Customers" and "Orders" section of the Shopify admin area. For example, to only count customers with enabled online accounts who are tagged with "qualifies", use this customers query:
state:enabled tag:qualifies
To count paid orders from the last 365 days, use this orders query:
financial_status:paid created_at:>={{ "now" | date: "%s" | minus: 31536000 | date: "%Y-%m-%d" }}
Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.
That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.
(By the way, have you seen our documentation? Have you joined the Slack community?)
mechanic/user/trigger {% if options.run_hourly__boolean %} mechanic/scheduler/hourly {% elsif options.run_daily__boolean %} mechanic/scheduler/daily {% endif %} mechanic/shopify/bulk_operation
{% comment %} Preferred option order: {{ options.order_minimums_and_customer_tags__keyval_required }} {{ options.only_keep_the_customer_tag_for_the_highest_order_minimum__boolean }} {{ options.tag_customers_by_number_of_orders__boolean }} {{ options.tag_customers_by_sum_of_order_totals__boolean }} {{ options.only_process_customers_matching_this_query }} {{ options.only_count_orders_matching_this_query }} {{ options.run_hourly__boolean }} {{ options.run_daily__boolean }} {% endcomment %} {% if options.tag_customers_by_number_of_orders__boolean and options.tag_customers_by_sum_of_order_totals__boolean %} {% error "Choose only one of 'Tag customers by number of orders' and 'Tag customers by sum of order totals'. :)" %} {% elsif options.tag_customers_by_number_of_orders__boolean == false and options.tag_customers_by_sum_of_order_totals__boolean == false %} {% error "Choose one of 'Tag customers by number of orders' and 'Tag customers by sum of order totals'. :)" %} {% endif %} {% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} {% capture bulk_operation_query %} query { customers(query: {{ options.only_process_customers_matching_this_query | json }}) { edges { node { __typename id email tags state orders(query: {{ options.only_count_orders_matching_this_query | json }}) { edges { node { __typename id name totalPriceSet { shopMoney { amount } } } } } } } } } {% endcapture %} {% action "shopify" %} mutation { bulkOperationRunQuery( query: {{ bulk_operation_query | json }} ) { bulkOperation { id status } userErrors { field message } } } {% endaction %} {% elsif event.topic == "mechanic/shopify/bulk_operation" %} {% if event.preview %} {% capture bulkOperation_objects_jsonl %} {"__typename":"Customer","id":"gid:\/\/shopify\/Customer\/1234567890","tags":[],"state":"ENABLED"} {% if options.tag_customers_by_number_of_orders__boolean -%} {% assign count = options.order_minimums_and_customer_tags__keyval_required.first.first | times: 1 -%} {% for n in (0..count) -%} {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","totalPriceSet":{"shopMoney":{"amount":"10.0"}},"__parentId":"gid:\/\/shopify\/Customer\/1234567890"} {% endfor -%} {% elsif options.tag_customers_by_sum_of_order_totals__boolean -%} {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","totalPriceSet":{"shopMoney":{"amount":{{ options.order_minimums_and_customer_tags__keyval_required.first.first | json}}}},"__parentId":"gid:\/\/shopify\/Customer\/1234567890"} {% endif %} {% endcapture %} {% assign bulkOperation = hash %} {% assign bulkOperation["objects"] = bulkOperation_objects_jsonl | parse_jsonl %} {% endif %} {% assign customers = bulkOperation.objects | where: "__typename", "Customer" %} {% assign orders = bulkOperation.objects | where: "__typename", "Order" %} {% for customer in customers %} {% assign customer_orders = orders | where: "__parentId", customer.id %} {% assign customer_orders_value = nil %} {% if options.tag_customers_by_number_of_orders__boolean %} {% assign customer_orders_value = customer_orders.size %} {% elsif options.tag_customers_by_sum_of_order_totals__boolean %} {% assign customer_orders_value = 0 %} {% for order in customer_orders %} {% assign customer_orders_value = customer_orders_value | plus: order.totalPriceSet.shopMoney.amount %} {% endfor %} {% endif %} {% if customer_orders != empty %} {% assign customer_orders_names = customer_orders | map: "name" %} {% log customer_id: customer.id, customer_email: customer.email, customer_tags: customer.tags, customer_orders_names: customer_orders_names, customer_orders_value: customer_orders_value %} {% endif %} {% assign best_fitting_tag_minimum = 0 %} {% assign best_fitting_tag = nil %} {% assign all_fitting_tags = array %} {% assign all_possible_tags = array %} {% for pair in options.order_minimums_and_customer_tags__keyval_required %} {% assign tag_minimum = pair[0] | times: 1 %} {% assign tag = pair[1] %} {% assign all_possible_tags[all_possible_tags.size] = tag %} {% if customer_orders_value >= tag_minimum %} {% assign all_fitting_tags[all_fitting_tags.size] = tag %} {% if tag_minimum >= best_fitting_tag_minimum %} {% assign best_fitting_tag = tag %} {% assign best_fitting_tag_minimum = tag_minimum %} {% endif %} {% endif %} {% endfor %} {% assign tags_to_add = array %} {% assign tags_to_remove = array %} {% if best_fitting_tag %} {% unless customer.tags contains best_fitting_tag %} {% assign tags_to_add[tags_to_add.size] = best_fitting_tag %} {% endunless %} {% endif %} {% if options.only_keep_the_customer_tag_for_the_highest_order_minimum__boolean %} {% for tag in all_possible_tags %} {% if tag != best_fitting_tag and customer.tags contains tag %} {% assign tags_to_remove[tags_to_remove.size] = tag %} {% endif %} {% endfor %} {% else %} {% for tag in all_fitting_tags %} {% unless tag == best_fitting_tag or customer.tags contains tag %} {% assign tags_to_add[tags_to_add.size] = tag %} {% endunless %} {% endfor %} {% endif %} {% if tags_to_add != empty or tags_to_remove != empty %} {% action "shopify" %} mutation { {% if tags_to_add != empty %} tagsAdd( id: {{ customer.id | json }} tags: {{ tags_to_add | json }} ) { node { ... on Customer { id email tags } } userErrors { field message } } {% endif %} {% if tags_to_remove != empty %} tagsRemove( id: {{ customer.id | json }} tags: {{ tags_to_remove | json }} ) { node { ... on Customer { id email tags } } userErrors { field message } } {% endif %} } {% endaction %} {% endif %} {% endfor %} {% endif %}
{"10"=>"10-orders", "100"=>"100-orders"}
true
true
state:enabled tag:qualifies
financial_status:paid created_at:>={{ "now" | date: "%s" | minus: 31536000 | date: "%Y-%m-%d" }}