Mechanic is a development and ecommerce automation platform for Shopify. :)
Running daily or manually, this task scans all customers who have placed orders and tags them based on the date of their last order. Choose between tagging customers whose orders are before x days ago, or after x days ago.
Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes days since last order, tag customers when last order is after, tag customers when last order is before, customer tag to apply, and run daily.
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 mechanic/shopify/bulk_operation {% if options.run_daily__boolean %} mechanic/scheduler/daily {% endif %}
{% assign days_since_last_order = options.days_since_last_order__required_number %} {% assign tag_customers_when_last_order_is_after = options.tag_customers_when_last_order_is_after__boolean %} {% assign tag_customers_when_last_order_is_before = options.tag_customers_when_last_order_is_before__boolean %} {% assign customer_tag_to_apply = options.customer_tag_to_apply__required %} {% if tag_customers_when_last_order_is_after and tag_customers_when_last_order_is_before %} {% error "Choose only one of 'Tag customers when last order is after' or 'Tag customers when last order is before' :)" %} {% endif %} {% unless tag_customers_when_last_order_is_after or tag_customers_when_last_order_is_before %} {% error "Choose either 'Tag customers when last order is after' or 'Tag customers when last order is before'" %} {% endunless %} {% capture advance_period -%} -{{ days_since_last_order }} days {%- endcapture %} {% assign order_processed_at_threshold = "now" | date: "%F", advance: advance_period %} {% if tag_customers_when_last_order_is_after %} {% log %} "Threshold for tagging customers: {{ order_processed_at_threshold }} and after" {% endlog %} {% elsif tag_customers_when_last_order_is_before %} {% log %} "Threshold for tagging customers: {{ order_processed_at_threshold }} and before" {% endlog %} {% endif %} {% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} {% comment %} -- get IDs of all customers who have placed an order after or before the threshold, and/or have the configured tag -- due to limited data in customer segments, the bulk op query only serves to reduce the overall count of customers being reviewed {% endcomment %} {% if tag_customers_when_last_order_is_after %} {% capture customer_segment_query -%} last_order_date >= {{ order_processed_at_threshold }} OR customer_tags CONTAINS '{{ customer_tag_to_apply }}' {%- endcapture %} {% elsif tag_customers_when_last_order_is_before %} {% capture customer_segment_query -%} last_order_date <= {{ order_processed_at_threshold }} OR customer_tags CONTAINS '{{ customer_tag_to_apply }}' {%- endcapture %} {% endif %} {% capture bulk_operation_query %} query { customerSegmentMembers( query: {{ customer_segment_query | json }} ) { edges { node { 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 %} {"id":"gid:\/\/shopify\/CustomerSegmentMember\/1234567890"} {"id":"gid:\/\/shopify\/CustomerSegmentMember\/2345678901"} {% endcapture %} {% assign bulkOperation = hash %} {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %} {% endif %} {% assign customer_segment_member_ids = bulkOperation.objects | map: "id" %} {% comment %} -- get tags and last order data from customer resource, since it's not available in customer segments {% endcomment %} {% for customer_segment_member_id in customer_segment_member_ids %} {% assign customer_should_be_tagged = nil %} {% capture query %} query { customer(id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}) { id tags lastOrder { processedAt } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "customer": { "id": "gid://shopify/Customer/1234567890", "lastOrder": { {% if tag_customers_when_last_order_is_after %} "processedAt": {{ "now" | date: "%F" | json }} {% elsif tag_customers_when_last_order_is_before %} "processedAt": "2000-01-01" {% endif %} } } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign customer = result.data.customer %} {% assign customer_last_order_processed_at = customer.lastOrder.processedAt | date: "%F" %} {% if tag_customers_when_last_order_is_after and customer_last_order_processed_at >= order_processed_at_threshold %} {% assign customer_should_be_tagged = true %} {% elsif tag_customers_when_last_order_is_before and customer_last_order_processed_at <= order_processed_at_threshold %} {% assign customer_should_be_tagged = true %} {% endif %} {% comment %} -- add and remove customer tags as needed {% endcomment %} {% if customer_should_be_tagged %} {% unless customer.tags contains customer_tag_to_apply %} {% action "shopify" %} mutation { tagsAdd( id: {{ customer.id | json }} tags: {{ customer_tag_to_apply | json }} ) { node { ... on Customer { id email tags lastOrder { name processedAt } } } userErrors { field message } } } {% endaction %} {% endunless %} {% else %} {% if customer.tags contains customer_tag_to_apply %} {% action "shopify" %} mutation { tagsRemove( id: {{ customer.id | json }} tags: {{ customer_tag_to_apply | json }} ) { node { ... on Customer { id email tags lastOrder { name processedAt } } } userErrors { field message } } } {% endaction %} {% endif %} {% endif %} {% endfor %} {% endif %}
7
true