Auto-tag customers based on email domain, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag customers based on email domain

by Isaac Bowen (team@usemechanic.com)

Useful in a million scenarios. The merchant who requested this task has discounts set up based on customer tag, and they're using this task to guarantee that qualifying customers see their discount immediately – even if they just signed up!

Runs when a customer is created and when a user triggers the task. Configuration includes customer email domains and customer tag to apply.

15-day free trial – unlimited tasks

Documentation

This task runs when customers are created. Use the "Run task" button to scan all customers already registered.

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 user triggers the task (mechanic/user/trigger)
Options
customer email domains (required, array), customer tag to apply (required)
Script
{% for email_domain in options.customer_email_domains__required_array %}
  {% if email_domain contains "@" %}
    {% error "Do not include '@' symbols in email domains. Thanks!" %}
  {% endif %}
{% endfor %}

{% assign customer_ids_to_tag = array %}

{% if event.topic contains "shopify/customers/" %}
  {% if event.preview %}
    {% assign customer = hash %}
    {% assign customer["admin_graphql_api_id"] = "gid://shopify/Customer/1234567890" %}
    {% assign customer["email"] = "test@" | append: options.customer_email_domains__required_array.first %}
  {% endif %}

  {% assign customer_email_domain = customer.email | split: "@" | last | downcase %}
  {% assign customer_tags = customer.tags | split: ", " %}

  {% if options.customer_email_domains__required_array contains customer_email_domain %}
    {% unless customer_tags contains options.customer_tag_to_apply__required %}
      {% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer.admin_graphql_api_id %}
    {% endunless %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture customers_query -%}
    -tag:{{ options.customer_tag_to_apply__required | json }} AND ({{ options.customer_email_domains__required_array | join: " OR email:" | prepend: "email:" }})
  {%- endcapture %}

  {% assign cursor = nil %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        customers(
          first: 250
          query: {{ customers_query | json }}
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              email
              tags
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customers": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/Customer/1234567890",
                    "email": {{ "test@" | append: options.customer_email_domains__required_array.first | json }},
                    "tags": []
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% for customer_edge in result.data.customers.edges %}
      {% assign customer = customer_edge.node %}
      {% assign customer_email_domain = customer.email | split: "@" | last | downcase %}

      {% if options.customer_email_domains__required_array contains customer_email_domain %}
        {% unless customer.tags contains options.customer_tag_to_apply__required %}
          {% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer.id %}
        {% endunless %}
      {% endif %}
    {% endfor %}

    {% if result.data.customers.pageInfo.hasNextPage %}
      {% assign cursor = result.data.customers.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% for id in customer_ids_to_tag %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ id | json }}
        tags: {{ options.customer_tag_to_apply__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Customer email domains
["example.com"]