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

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

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.

Runs Occurs whenever a customer is created, Occurs whenever a customer is updated, and Occurs when a user manually 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, 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
shopify/customers/create
shopify/customers/update
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
customer tag to apply (required), test mode (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Test mode
true