Schedule customer auto-tagging after a purchase with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Schedule customer auto-tagging after a purchase

by Isaac Bowen (team@usemechanic.com)

Selling scheduled events? Using this task, you can automatically tag customers after the event has been completed. Simply tag a product with "autotag Authorized May 15", for example, and Mechanic will tag purchasers of that product with "Authorized", on May 15th.

Runs when an order is paid and when user/autotag/customer is triggered. Configuration includes autotag prefix.

15-day free trial – unlimited tasks

Documentation

This task comes with a single option: "Autotag prefix". Mechanic will watch your incoming orders for any products that have a tag beginning with that prefix. It will then consider the next word in the product tag to be the customer tag you wish to apply. The rest of the tag will be treated as a date, for the purposes of scheduling the customer tagging.

For example, purchases of a product tagged with "autotag Authorized May 15" will result in customers being auto-tagged with "Authorized", on May 15th.

Products may have multiple tags containing autotag information, and Mechanic will check every product in a customer's order, resulting in as many auto-taggings as are appropriate. autotag events as appropriate.

Note: The customer tag to apply must not contain any spaces!

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when an order is paid (shopify/orders/paid)
when user/autotag/customer is triggered (user/autotag/customer)
Options
autotag prefix (required)
Script
{% if event.topic contains "shopify/orders/" %}
  {% if event.preview %}
    {% action "event" %}
      {
        "topic": "user/autotag/customer",
        "data": {
          "order_id": 1234567890,
          "customer_tag": "sample"
        },
        "run_at": {{ "now" | date: "%s" | plus: 2592000 | json }}
      }
    {% endaction %}
  {% else %}
    {% for line_item in order.line_items %}
      {% assign product_tags = line_item.product.tags | split: ", " %}
      {% for product_tag in product_tags %}
        {% assign product_tag_parts = product_tag | split: " " %}
        {% unless product_tag_parts[0] == options.autotag_prefix__required %}
          {% continue %}
        {% endunless %}

        {% assign tag = product_tag_parts[1] %}
        
        {% assign tag_at_string = product_tag | remove_first: options.autotag_prefix__required | remove_first: " " | remove_first: tag | remove_first: " " %}
        {% assign tag_at = tag_at_string | date: "%s" | times: 1 %}

        {% assign tag_at_month = ends_at_string | date: "%-m" | times: 1 %}
        {% assign now_month = "now" | date: "%-m" | times: 1 %}
        {% if now_month > ends_at_month %}
          {% assign next_year = "now" | date: "%Y" | plus: 1 %}
          {% assign tag_at = tag_at | date: "-%m-%dT%T%:z" | prepend: next_year | date: "%s" | times: 1 %}
        {% endif %}

        {% action "event" %}
          {
            "topic": "user/autotag/customer",
            "data": {
              "order_id": {{ order.id | json }},
              "customer_tag": {{ tag | json }}
            },
            "run_at": {{ tag_at | json }}
          }
        {% endaction %}
      {% endfor %}
    {% endfor %}
  {% endif %}
{% elsif event.topic == "user/autotag/customer" %}
  {% assign order = shop.orders[event.data.order_id] %}
  {% if event.preview or order.financial_status == "paid" %}
    {% capture query %}
      mutation {
        tagsAdd(
          id: {% if event.preview %}"gid://shopify/Customer/1234567890"{% else %}{{ order.customer.admin_graphql_api_id | json }}{% endif %}
          tags: {{ event.data.customer_tag | json }}
        ) {
          node {
            id
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endcapture %}

    {% action "shopify" query %}
  {% else %}
    {"log": "Order is {{ order.financial_status }}; skipping tagging"}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Autotag prefix
autotag