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

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

Auto-tag customers based on email domain

This task runs when customers are created, and it applies a tag if they have one of the configured email domains.

Runs Occurs whenever a customer is created and Occurs when a user manually 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, and it applies a tag if they have one of the configured email domains.

Use the "Run task" button to scan all customers already registered.

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
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
customer email domains (required, array) , customer tag to apply (required)
Code
{% assign customer_email_domains = options.customer_email_domains__required_array %}
{% assign customer_tag_to_apply = options.customer_tag_to_apply__required %}

{% for email_domain in customer_email_domains %}
  {% 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: customer_email_domains.first %}
  {% endif %}

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

  {% if customer_email_domains contains customer_email_domain %}
    {% unless customer_tags contains customer_tag_to_apply %}
      {% assign customer_ids_to_tag[customer_ids_to_tag.size] = customer.admin_graphql_api_id %}
    {% endunless %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- customer segment queries support up to 10 filters, so group the email domains by 9 to avoid API errors
  {% endcomment %}

  {% assign groups_of_email_domains = customer_email_domains | in_groups_of: 9, fill_with: false %}

  {% for group_of_email_domains in groups_of_email_domains %}
    {% assign domain_query_parts = array %}

    {% for email_domain in group_of_email_domains %}
      {% assign domain_query_parts[domain_query_parts.size]
        = "customer_email_domain = '"
        | append: email_domain
        | append: "'"
      %}
    {% endfor %}

    {% capture search_query -%}
      customer_tags NOT CONTAINS '{{ customer_tag_to_apply }}' AND ({{ domain_query_parts | join: " OR " }})
    {%- endcapture %}

    {% unless event.preview %}
      {% log
        search_query: search_query,
        groups_of_email_domains_loop: forloop.index
      %}
    {% endunless %}

    {% assign cursor = nil %}

    {% comment %}
      -- start with the customerSegmentMembers resource since it supports a domain filter
    {% endcomment %}

    {% for n in (0..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_segment_member_ids
        = result.data.customerSegmentMembers.edges
        | map: "node"
        | map: "id"
      %}

      {% for customer_segment_member_id in customer_segment_member_ids %}
        {% comment %}
          -- query the customer record in order to verify the domain and absence of the tag, in case the search query filter has "seepage"
        {% endcomment %}

        {% capture query %}
          query {
            customer(id: {{ customer_segment_member_id | remove: "SegmentMember" | json }}) {
              id
              defaultEmailAddress {
                emailAddress
              }
              tags
            }
          }
        {% endcapture %}

        {% assign result = query | shopify %}

        {% if event.preview %}
          {% capture result_json %}
            {
              "data": {
                "customer": {
                  "id": "gid://shopify/Customer/1234567890",
                  "defaultEmailAddress": {
                    "emailAddress": {{ "test@" | append: customer_email_domains.first | json }}
                  }
                }
              }
            }
          {% endcapture %}

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

        {% assign customer = result.data.customer %}
        {% assign customer_email_domain = customer.defaultEmailAddress.emailAddress | split: "@" | last | downcase %}

        {% if customer_email_domains contains customer_email_domain %}
          {% unless customer.tags contains customer_tag_to_apply %}
            {% assign customer_ids_to_tag = customer_ids_to_tag | push: customer.id %}
          {% endunless %}
        {% endif %}
      {% endfor %}

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

{% for customer_id in customer_ids_to_tag %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ customer_id | json }}
        tags: {{ customer_tag_to_apply | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Customer email domains
["example.com"]