Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs every day at midnight (in local time)

What you choose

  • Customer tag to apply
  • Customer search query
  • Days to wait before tagging
  • Use account creation date instead of first order date

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".

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/scheduler/daily
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Use account creation date instead of first order date
false