Tag customers on the anniversary of their first order, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Tag customers on the anniversary of their first order

Running daily, or manually, this task query for all customers if their first order was a year or more ago and they do not yet have the configured tag to apply. Alternatively, you may choose to tag customers if their account was created at least a year ago.

Runs Occurs when a user manually triggers the task and Occurs every day at midnight (in local time). Configuration includes customer tag to apply, customer search query, days to wait before tagging, and use account creation date instead of first order date.

15-day free trial – unlimited tasks

Documentation

Running daily, or manually, this task query for all customers if their first order was a year or more ago and they do not yet have the configured tag to apply. Alternatively, you may choose to tag customers if their account was created at least a year ago.

Optionally, you may choose whether to wait a certain amount of days before tagging (i.e. a year + X days afterwards), and to configure a "Customer search query" with the exact same syntax used in the Customers segment search dialog.

For example:
* Use customer_tags CONTAINS 'VIP' for customers with the VIP tag
* Use email_subscription_status = 'SUBSCRIBED' for customers subscribed to email marketing
* Use amount_spent >= 100 for customers who have spent more than 100 in the local shop currency

Important: The task will already include the appropriate search terms to filter customers by either their first order date or account creation date, so do not include that criteria if using the optional "Customer search query".

Developer details

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?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
mechanic/scheduler/daily
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
customer tag to apply (required), customer search query, days to wait before tagging (number), use account creation date instead of first order date (boolean)
Code
{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}
{% assign customer_search_query = options.customer_search_query %}
{% assign days_to_wait_before_tagging = options.days_to_wait_before_tagging__number %}
{% assign use_account_creation_date = options.use_account_creation_date_instead_of_first_order_date__boolean %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% assign origin_threshold = "now - 1 year" | date: "%Y-%m-%d" %}

  {% if days_to_wait_before_tagging > 0 %}
    {% assign seconds_to_wait_before_tagging = days_to_wait_before_tagging | times: 86400 %}
    {% assign origin_threshold = origin_threshold | date: "%s" | minus: seconds_to_wait_before_tagging | date: "%Y-%m-%d" %}
  {% endif %}

  {%- capture search_query -%}
    customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}' AND
    {% if use_account_creation_date %}customer_added_date{% else %}first_order_date{% endif %} <= {{ origin_threshold }}
    {% if customer_search_query != blank %} AND ({{ customer_search_query }}){% endif %}
  {%- endcapture -%}

  {% log search_query: search_query %}

  {% assign cursor = nil %}
  {% assign customer_ids = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          query: {{ search_query | json }}
        ) {
          edges {
            node {
              id
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customerSegmentMembers": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/CustomerSegmentMember/1234567890"
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

      {% assign result = result_json | parse_json %}
    {% endif %}

    {% for edge in result.data.customerSegmentMembers.edges %}
      {% assign customer_ids[customer_ids.size] = edge.node.id | remove: "SegmentMember" %}
    {% endfor %}

    {% if result.data.customerSegmentMembers.pageInfo.hasNextPage %}
      {% assign cursor = result.data.customerSegmentMembers.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% comment %}
    -- tag all customers who were returned by the customer segment query
  {% endcomment %}

  {% for customer_id in customer_ids %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ customer_id | json }}
          tags: {{ customer_tag_to_apply | json }}
        ) {
          node {
            ... on Customer {
              email
              displayName
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

  {% else %}
    {% log "No customers qualified to have the configured tag added on this task run." %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more