Auto-tag customers with who have accounts, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag customers with who have accounts

by Isaac Bowen (team@usemechanic.com)

This task monitors customer accounts, and applies the tag of your choice to customers that have an enabled account, with a registered email address and active password. It untags customers whose accounts are disabled. This task can be run manually to tag/untag all existing customers.

Runs when a customer is created, when a customer is updated, and when a user triggers the task. Configuration includes customer tag to apply and test mode.

15-day free trial – unlimited tasks

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.

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 a customer is created (shopify/customers/create)
when a customer is updated (shopify/customers/update)
when a user triggers the task (mechanic/user/trigger)
Options
customer tag to apply (required), test mode (boolean)
Script
{% 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 options.customer_tag_to_apply__required %}
      {% if options.test_mode__boolean %}
        {% action "echo", "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: {{ options.customer_tag_to_apply__required | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endunless %}
  {% elsif customer_tags contains options.customer_tag_to_apply__required %}
    {% if options.test_mode__boolean %}
      {% action "echo", "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: {{ options.customer_tag_to_apply__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% assign customersToTag_cursor = nil %}
  {% assign customersToUntag_cursor = nil %}

  {% assign customer_ids_to_tag = array %}
  {% assign customer_ids_to_untag = array %}

  {% for n in (0..100) %}
    {% if customersToTag_cursor == false and customersToUntag_cursor == false %}
      {% break %}
    {% endif %}

    {% capture query %}
      query {
        {% unless customersToTag_cursor == false %}
          customersToTag: customers(
            first: 250
            after: {{ customersToTag_cursor | json }}
            query: {{ options.customer_tag_to_apply__required | json | prepend: "-tag:" | append: " AND state:enabled" | json }}
          ) {
            pageInfo {
              hasNextPage
            }
            edges {
              cursor
              node {
                id
              }
            }
          }
        {% endunless %}

        {% unless customersToUntag_cursor == false %}
          customersToUntag: customers(
            first: 250
            after: {{ customersToUntag_cursor | json }}
            query: {{ options.customer_tag_to_apply__required | json | prepend: "tag:" | append: " AND -state:enabled" | json }}
          ) {
            pageInfo {
              hasNextPage
            }
            edges {
              cursor
              node {
                id
              }
            }
          }
        {% endunless %}
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% for customer_edge in result.data.customersToTag.edges %}
      {% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer_edge.node.id %}
    {% endfor %}

    {% for customer_edge in result.data.customersToUntag.edges %}
      {% assign customer_ids_to_untag[customer_ids_to_untag.size] = customer_edge.node.id %}
    {% endfor %}

    {% if result.data.customersToTag.pageInfo.hasNextPage %}
      {% assign customersToTag_cursor = result.data.customersToTag.edges.last.cursor %}
    {% else %}
      {% assign customersToTag_cursor = false %}
    {% endif %}

    {% if result.data.customersToUntag.pageInfo.hasNextPage %}
      {% assign customersToUntag_cursor = result.data.customersToUntag.edges.last.cursor %}
    {% else %}
      {% assign customersToUntag_cursor = false %}
    {% endif %}
  {% endfor %}

  {% if event.preview %}
    {% assign customer_ids_to_tag[0] = "gid://shopify/Customer/1234567890" %}
    {% assign customer_ids_to_untag[0] = "gid://shopify/Customer/1234567890" %}
  {% endif %}

  {% if options.test_mode__boolean %}
    {% action "echo", customers_to_tag: customer_ids_to_tag.size, customers_to_untag: customer_ids_to_untag.size %}
  {% else %}
    {% for customer_id in customer_ids_to_tag %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ customer_id | json }}
            tags: {{ options.customer_tag_to_apply__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}

    {% for customer_id in customer_ids_to_untag %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ customer_id | json }}
            tags: {{ options.customer_tag_to_apply__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Test mode
true