Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

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

What you choose

  • Customer email domains
  • Customer tag to apply

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.

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
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Customer email domains
["example.com"]