Mechanic Get Mechanic
Task library

Auto-tag customers with who have accounts

This task runs automatically for individual customers, as they are created and updated. It will tag customers when they have an enabled account (i.e. have a registered email address and active password), and it will untag customers whose accounts are disabled.

Try this task

When it runs

  • Occurs whenever a customer is created
  • Occurs whenever a customer is updated
  • Occurs when a user manually triggers the task

What you choose

  • Customer tag to apply
  • Test mode

Documentation

This task runs automatically for individual customers, as they are created and updated. It will tag customers when they have an enabled account (i.e. have a registered email address and active password), and it will untag customers whose accounts are disabled.

Run this task manually to scan all existing customers, tagging and untagging as appropriate.

Use test mode to have this task return information about what actions it would normally take.

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

shopify/customers/create
shopify/customers/update
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}
{% assign test_mode = options.test_mode__boolean %}

{% if event.topic contains "shopify/customers/" %}
  {% if event.preview %}
    {% assign customer = hash %}
    {% assign customer["state"] = "enabled" %}
    {% assign customer["tags"] = "" %}
    {% assign customer["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
  {% endif %}

  {% assign customer_tags = customer.tags | split: ", " %}

  {% if customer.state == "enabled" %}
    {% unless customer_tags contains customer_tag_to_apply %}
      {% if test_mode %}
        {% log "This customer should be tagged. (Doing nothing, because test mode is enabled." %}

      {% else %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ customer.admin_graphql_api_id | json }}
              tags: {{ customer_tag_to_apply | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endunless %}

  {% elsif customer_tags contains customer_tag_to_apply %}
    {% if test_mode %}
      {% log "This customer should be untagged. (Doing nothing, because test mode is enabled." %}

    {% else %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ customer.admin_graphql_api_id | json }}
            tags: {{ customer_tag_to_apply | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% comment %}
    -- get IDs of all customers who have accounts and have not yet been tagged
  {% endcomment %}

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

  {%- capture search_query -%}
    customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}' AND customer_account_status = 'ENABLED'
  {%- endcapture -%}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          after: {{ cursor | json }}
          query: {{ search_query | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          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 %}

    {% assign customer_ids_to_tag
      = result.data.customerSegmentMembers.edges
      | map: "node"
      | map: "id"
      | concat: customer_ids_to_tag
    %}

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

  {% comment %}
    -- get IDs of all customers with tag who no longer have accounts
  {% endcomment %}

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

  {%- capture search_query -%}
    customer_tags CONTAINS '{{ customer_tag_to_apply }}' AND customer_account_status != 'ENABLED'
  {%- endcapture -%}

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

    {% assign result = query | shopify %}

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

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

    {% assign customer_ids_to_untag
      = result.data.customerSegmentMembers.edges
      | map: "node"
      | map: "id"
      | concat: customer_ids_to_untag
    %}

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

  {% log
    count_customers_to_tag: customer_ids_to_tag.size,
    count_customers_to_untag: customer_ids_to_untag.size
  %}

  {% unless test_mode %}
    {% for customer_id in customer_ids_to_tag %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer_id | remove: "SegmentMember" | json }}
            tags: {{ customer_tag_to_apply | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}

    {% for customer_id in customer_ids_to_untag %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ customer_id | remove: "SegmentMember" | json }}
            tags: {{ customer_tag_to_apply | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endunless %}
{% endif %}
Mechanic Liquid reference

Default option values

Test mode
true