Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to tag customers, as their orders come in, based on which sales channel they used for their purchase. Run this task manually to backfill tags for customers based on their historical orders. Use test mode to see what this task would do, if test mode wasn't enabled.
Runs Occurs whenever an order is created, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes sales channel names and tags and test mode.
Use this task to tag customers, as their orders come in, based on which sales channel they used for their purchase. Run this task manually to backfill tags for customers based on their historical orders. Use test mode to see what this task would do, if test mode wasn't enabled.
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?)
shopify/orders/create mechanic/user/trigger mechanic/shopify/bulk_operation
{% assign sales_channel_names_and_tags = options.sales_channel_names_and_tags__keyval_required %} {% assign test_mode = options.test_mode__boolean %} {% assign sales_channel_names = sales_channel_names_and_tags | keys %} {% comment %} -- get all of the sales channel names (i.e. publications aka app catalogs) in the shop {% endcomment %} {% capture query %} query { publications( first: 250 catalogType:APP ) { nodes { id catalog { ... on AppCatalog { apps(first: 1) { nodes { title } } } } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "publications": { "nodes": [ { "id": "gid://shopify/Publication/1234567890", "catalog": { "apps": { "nodes": [ { "title": {{ sales_channel_names.first | json }} } ] } } } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% comment %} -- add the configured publication names into a hash for use in lookups {% endcomment %} {% assign publication_ids = array %} {% assign publication_names_by_id = hash %} {% for publication in result.data.publications.nodes %} {% assign publication_name = publication.catalog.apps.nodes.first.title %} {% if sales_channel_names contains publication_name %} {% assign publication_ids = publication_ids | push: publication.id %} {% assign publication_names_by_id[publication.id] = publication_name %} {% endif %} {% endfor %} {% if event.topic contains "shopify/orders/" %} {% comment %} -- get the publication ID (if any) that the order originated from -- Note: publication.name is deprecated; publication IDs should be matched with app catalog titles instead {% endcomment %} {% capture query %} query { order(id: {{ order.admin_graphql_api_id | json }}) { customer { id tags } publication { id } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "order": { "customer": { "id": "gid://shopify/Customer/1234567890", "tags": [] }, "publication": { "id": {{ publication_ids.first | json }} } } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign order_publication_id = result.data.order.publication.id %} {% if order_publication_id == blank %} {% log message: "This order did not originate from an app sales channel. Skipping.", order: result.data.order %} {% break %} {% endif %} {% comment %} -- tag the customer if the order's sales channel matches one configured in this task {% endcomment %} {% assign publication_name = publication_names_by_id[order_publication_id] %} {% assign tag_to_add = sales_channel_names_and_tags[publication_name] %} {% if tag_to_add == blank %} {% log message: "No tag found for this channel. Skipping.", publication_name: publication_name %} {% elsif result.data.order.customer.tags contains tag_to_add %} {% log message: "The customer already has the tag for this channel. Skipping.", publication_name: publication_name, tag_to_add: tag_to_add %} {% elsif test_mode %} {% log customer_id: result.data.order.customer.id, publication_name: publication_name, customer_tag_to_add: tag_to_add %} {% else %} {% action "shopify" %} mutation { tagsAdd( id: {{ result.data.order.customer.id | json }} tags: {{ tag_to_add | json }} ) { userErrors { field message } } } {% endaction %} {% endif %} {% elsif event.topic == "mechanic/user/trigger" %} {% comment %} -- get all customers and their orders {% endcomment %} {% capture bulk_operation_query %} query { customers { edges { node { __typename id tags orders { edges { node { __typename publication { id } } } } } } } } {% 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 jsonl_string %} {"__typename":"Customer","id":"gid://shopify/Customer/1234567890"} {"__typename":"Order","publication":{"id":"gid://shopify/Publication/1234567890"},"__parentId":"gid://shopify/Customer/1234567890"} {% endcapture %} {% assign bulkOperation = hash %} {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %} {% endif %} {% assign customers = bulkOperation.objects | where: "__typename", "Customer" %} {% assign orders = bulkOperation.objects | where: "__typename", "Order" %} {% assign publication_counts = hash %} {% for order in orders %} {% assign order_publication_id = order.publication.id %} {% assign publication_name = publication_names_by_id[order_publication_id] %} {% if publication_name == blank %} {% assign publication_name = "(not placed via a configured sales channel)" %} {% endif %} {% assign publication_counts[publication_name] = publication_counts[publication_name] | plus: 1 %} {% endfor %} {% log publication_counts: publication_counts %} {% if test_mode %} {% assign summaries = array %} {% endif %} {% comment %} -- process customers to see which should be tagged {% endcomment %} {% for customer in customers %} {% assign customer_orders = orders | where: "__parentId", customer.id %} {% comment %} -- determine all sales channels this customer has ordered through {% endcomment %} {% assign customer_publication_names = array %} {% for order in customer_orders %} {% assign order_publication_id = order.publication.id %} {% assign publication_name = publication_names_by_id[order_publication_id] %} {% if publication_name == blank %} {% continue %} {% endif %} {% unless customer_publication_names contains publication_name %} {% assign customer_publication_names = customer_publication_names | push: publication_name %} {% endunless %} {% endfor %} {% comment %} -- assign tags by configured sales channels {% endcomment %} {% assign tags_to_add = array %} {% for publication_name in customer_publication_names %} {% assign tag = sales_channel_names_and_tags[publication_name] %} {% if tag == blank or customer.tags contains tag %} {% continue %} {% endif %} {% assign tags_to_add = tags_to_add | push: tag %} {% endfor %} {% if tags_to_add != blank %} {% if test_mode %} {% assign summary = hash %} {% assign summary["customer_id"] = customer.id %} {% assign summary["customer_publication_names"] = customer_publication_names %} {% assign summary["customer_tags_to_add"] = tags_to_add %} {% assign summaries = summaries | push: summary %} {% else %} {% action "shopify" %} mutation { tagsAdd( id: {{ customer.id | json }} tags: {{ tags_to_add | uniq | json }} ) { userErrors { field message } } } {% endaction %} {% endif %} {% endif %} {% endfor %} {% if test_mode %} {% log summaries %} {% endif %} {% endif %}
{"Online Store" => "online-customer", "Buy Button" => "buy-button-customer"}
true