Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this this task to monitor for the addition of a new customer tag, and to schedule the customer to be untagged some number of days later. Useful for granting temporary access to discounts, or other resources.
Runs Occurs whenever a customer is updated and Occurs whenever user/task/untag_customer is triggered. Configuration includes tag to monitor and days to wait before untagging.
Use this this task to monitor for the addition of a new customer tag, and to schedule the customer to be untagged some number of days later. Useful for granting temporary access to discounts, or other resources.
This task monitors new and updated customers, watching for the configured customer tag.
As soon as that tag is detected, the task will add a second tag, indicating that the customer is scheduled to be untagged. (For example, if the task is configured to watch for the tag "Approved", the task will add the tag "Approved - will be auto-removed by Mechanic".) The task will then schedule a followup event for the future, according to the configured number of days to wait. At that time, the task will remove both tags.
Important note: To prevent the task from untagging the customer later, manually remove the task's additional tag (i.e. the "will be auto-removed by Mechanic" tag). If the additional tag is found missing, the task will leave the tag in place instead of auto-removing it.
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/customers/update user/task/untag_customer
{% if event.topic == "user/task/untag_customer" %} {% assign customer_id = event.data.customer_id %} {% else %} {% assign customer_id = customer.admin_graphql_api_id %} {% endif %} {% assign tag = options.tag_to_monitor__required %} {% assign untag_flag_tag = tag | append: " - will be auto-removed by Mechanic" %} {% assign tag_removal_interval_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 | round %} {% assign now_s = "now" | date: "%s" | times: 1 %} {% assign metafield_key = task.id | sha256 | slice: 0, 7 %} {% capture query %} query { customer(id: {{ customer_id | json }}) { id tags metafield( namespace: "mechanic" key: {{ metafield_key | json }} ) { id value } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "customer": { "id": "gid://shopify/Customer/1234567890", "tags": [{{ tag | json }}], "metafield": null } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign customer = result.data.customer %} {% if customer.tags contains tag %} {% assign time_to_remove_s = customer.metafield.value | times: 1 %} {% if customer.metafield == nil or time_to_remove_s == 0 %} {% action "shopify" %} mutation { tagsAdd( id: {{ customer.id | json }} tags: {{ untag_flag_tag | json }} ) { node { ... on Customer { tags } } userErrors { field message } } customerUpdate( input: { id: {{ customer.id | json }} metafields: [ { namespace: "mechanic" key: {{ metafield_key | json }} value: {{ now_s | plus: tag_removal_interval_s | append: "" | json }} type: "number_integer" } ] } ) { customer { metafield( namespace: "mechanic" key: {{ metafield_key | json }} ) { id } } userErrors { field message } } } {% endaction %} {% action "event" %} { "topic": "user/task/untag_customer", "data": { "customer_id": {{ customer.id | json }} }, "run_at": {{ now_s | plus: tag_removal_interval_s | json }}, "task_id": {{ task.id | json }} } {% endaction %} {% elsif customer.tags contains untag_flag_tag %} {% if now_s < time_to_remove_s %} {% log message: "This customer is scheduled to be untagged, but it's not time yet. Skipping.", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %} {% else %} {% log message: "This customer is scheduled to be untagged, and that time is now.", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %} {% action "shopify" %} mutation { tagsRemove( id: {{ customer.id | json }} tags: [{{ tag | json }}, {{ untag_flag_tag | json }}] ) { node { ... on Customer { tags } } userErrors { field message } } metafieldDelete( input: { id: {{ customer.metafield.id | json }} } ) { userErrors { field message } } } {% endaction %} {% endif %} {% else %} {% log message: "This customer has a tag removal time recorded, but the 'untag flag tag' has been removed. Skipping.", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %} {% endif %} {% elsif event.topic == "user/task/untag_customer" %} {% log message: "The tag auto-removal event has arrived, but the customer has already been untagged by someone/something else. Skipping.", tag: tag, customer_tags: customer.tags %} {% endif %}