Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this this task to monitor for the addition of a specific customer tag, and to schedule the customer to be untagged a configurable 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 specific customer tag, and to schedule the customer to be untagged a configurable number of days later. Useful for granting temporary access to discounts, or other resources.
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).
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
{% assign tag_to_monitor = options.tag_to_monitor__required %} {% assign tag_removal_interval_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 | round %} {% assign untag_flag_tag = tag_to_monitor | append: " - will be auto-removed by Mechanic" %} {% assign now_s = "now" | date: "%s" | times: 1 %} {% assign metafield_key = task.id | sha256 | slice: 0, 7 %} {% if event.topic == "user/task/untag_customer" %} {% assign customer_id = event.data.customer_id %} {% else %} {% assign customer_id = customer.admin_graphql_api_id %} {% endif %} {% 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_to_monitor | json }} } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign customer = result.data.customer %} {% if customer.tags contains tag_to_monitor %} {% assign time_to_remove_s = customer.metafield.value | times: 1 %} {% if customer.metafield == blank 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 } } metafieldsSet( metafields: [ { ownerId: {{ customer.id | json }} namespace: "mechanic" key: {{ metafield_key | json }} value: {{ now_s | plus: tag_removal_interval_s | append: "" | json }} type: "number_integer" } ] ) { metafields { id namespace key type value owner { ... on Customer { id displayName } } } userErrors { code 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_to_monitor: tag_to_monitor, 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_to_monitor: tag_to_monitor, 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: {{ array | push: tag_to_monitor, untag_flag_tag | json }} ) { node { ... on Customer { tags } } userErrors { field message } } metafieldsDelete( metafields: [ { ownerId: {{ customer.id | json }} namespace: "mechanic" key: {{ metafield_key | json }} } ] ) { deletedMetafields { ownerId namespace key } 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_to_monitor: tag_to_monitor, 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_to_monitor: tag_to_monitor, customer_tags: customer.tags %} {% endif %}