Mechanic Get Mechanic
Task library

Email customers when tagged

Automatically send out an email to your customers when a tag is added to their account. Useful for sending approval emails, welcome emails if the customer was created with a certain tag, or any other emails related to an update to the customer's status. The most common use of this task is to send out emails to customers when their accounts are approved, but you can use this task to email customers any kind of alert you like, triggered by a simple tag. For example, some themes include an email subscription form that creates a customer record, pre-tagged with "email subscribe", or something to that effect.

Try this task

When it runs

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

What you choose

  • Tag to watch for
  • Ignore tag case
  • Autotag customers after emailing
  • Email subject
  • Email body

Documentation

Automatically send out an email to your customers when a tag is added to their account. Useful for sending approval emails, welcome emails if the customer was created with a certain tag, or any other emails related to an update to the customer's status. The most common use of this task is to send out emails to customers when their accounts are approved, but you can use this task to email customers any kind of alert you like, triggered by a simple tag. For example, some themes include an email subscription form that creates a customer record, pre-tagged with "email subscribe", or something to that effect.

Important note: After saving this task for the first time (and every time you change the tag to watch for), click the "Run task" button. Mechanic will then scan your customers to find those who already have the desired tag, and will make a note of them, so as to not email them in the future. (We require this to avoid suddenly sending a flood of emails to customers that were tagged before the Mechanic task was created.)

Enable the "Autotag customers after emailing" option to have Mechanic add "YOURTAGHERE-email-sent" to the customer's tags, when their email is sent out.

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

Option types and flags

Task code

{% assign approval_tag = options.tag_to_watch_for__required %}

{% if options.ignore_tag_case__boolean %}
  {% assign approval_tag = options.tag_to_watch_for__required | downcase %}
{% endif %}

{% capture email_sent_tag %}{{ approval_tag }}-email-sent{% endcapture %}
{% assign tag_processed_metafield_key = task.created_at | date: "%s" | append: approval_tag | append: "tag-processed" | sha256 | slice: 0, 5 | prepend: "-" | prepend: approval_tag %}
{% assign customers_scanned_metafield_key = task.created_at | date: "%s" | append: approval_tag | append: "customers-scanned" | sha256 | slice: 0, 5 | prepend: "-" | prepend: approval_tag %}

{% if event.topic contains "shopify/customers/" %}
  {% assign customer_tags = customer.tags | split: ", " %}

  {% if options.ignore_tag_case__boolean %}
    {% assign customer_tags = customer.tags | downcase | split: ", " %}
  {% endif %}

  {% assign customer_qualifies = true %}
  {% unless customer_tags contains approval_tag %}
    {% assign customer_qualifies = false %}
    {% log "The customer does not have the required tag." %}
  {% endunless %}

  {% if options.autotag_customers_after_emailing__boolean %}
    {% if customer_tags contains email_sent_tag %}
      {% assign customer_qualifies = false %}
      {% log "According to the customer's tags, this customer has already received this email." %}
    {% endif %}
  {% endif %}

  {% if customer_qualifies %}
    {% capture query %}
      query {
        customer(id: {{ customer.admin_graphql_api_id | json }}) {
          metafield(namespace: "mechanic", key: {{ tag_processed_metafield_key | json }}) {
            value
          }
        }
      }
    {% endcapture %}
    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customer": {
              "metafield": {
                "value": "1"
              }
            }
          }
        }
      {% endcapture %}
      {% assign result = result_json | parse_json %}
    {% endif %}

    {% if result.data.customer.metafield %}
      {% assign customer_qualifies = false %}
      {% log "According to a stored metafield, this customer has already received this email, or was already tagged when this task was initially set up." %}
    {% endif %}
  {% endif %}

  {% if customer_qualifies %}
    {% capture query %}
      query {
        shop {
          metafield(namespace: "mechanic", key: {{ customers_scanned_metafield_key | json }}) {
            value
          }
        }
      }
    {% endcapture %}
    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "shop": {
              "metafield": {
                "value": "1"
              }
            }
          }
        }
      {% endcapture %}
      {% assign result = result_json | parse_json %}
    {% endif %}

    {% if result.data.shop.metafield == nil %}
      {% error "Please use this task's 'Run task' button to manually scan your existing customers. Mechanic will take note of everyone who's already tagged, so as to *not* send them an email in the future." %}
    {% endif %}
  {% endif %}

  {% if event.preview or customer_qualifies %}
    {% action "email" %}
      {
        "to": {{ customer.email | json }},
        "subject": {{ options.email_subject__required | json }},
        "body": {{ options.email_body__required_multiline | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}

    {% action "shopify" %}
      mutation {
        {% if options.autotag_customers_after_emailing__boolean %}
          tagsAdd(
            id: {{ customer.admin_graphql_api_id | json }}
            tags: {{ email_sent_tag | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}

        customerUpdate(input: {
          id: {{ customer.admin_graphql_api_id | json }}
          metafields: [
            {
              namespace: "mechanic"
              key: {{ tag_processed_metafield_key | json }}
              type: "number_integer"
              value: "1"
            }
          ]
        }) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign metafield_inputs = array %}
  {% assign cursor = nil %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        customerSegmentMembers(
          first: 1000
          query: "customer_tags CONTAINS '{{ approval_tag }}'"
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          edges {
            node {
              id
              metafield(
                namespace: "mechanic"
                key: {{ tag_processed_metafield_key | json }}
              ) {
                value
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "customerSegmentMembers": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/CustomerSegmentMember/1234567890",
                    "metafield": null
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign customers = result.data.customerSegmentMembers.edges | map: "node" %}

    {% comment %}
      -- Note: Shopify API accepts CustomerSegmentMember IDs without needing replacement, at least for the metafieldsSet mutation
    {% endcomment %}

    {% for customer in customers %}
      {% if customer.metafield == blank %}
        {% assign metafield_input = hash %}
        {% assign metafield_input["ownerId"] = customer.id %}
        {% assign metafield_input["namespace"] = "mechanic" %}
        {% assign metafield_input["key"] = tag_processed_metafield_key %}
        {% assign metafield_input["type"] = "number_integer" %}
        {% assign metafield_input["value"] = "1" %}
        {% assign metafield_inputs = metafield_inputs | push: metafield_input %}
      {% endif %}
    {% endfor %}

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

  {% if metafield_inputs != blank %}
    {% assign groups_of_metafield_inputs = metafield_inputs | in_groups_of: 25, fill_with: false %}

    {% for group_of_metafield_inputs in groups_of_metafield_inputs %}
      {% action "shopify" %}
        mutation {
          metafieldsSet(
            metafields: {{ group_of_metafield_inputs | graphql_arguments }}
          ) {
            metafields {
              id
              namespace
              key
              type
              value
              owner {
                ... on Customer {
                  id
                }
              }
            }
            userErrors {
              code
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}

  {% action "shopify" %}
    mutation {
      metafieldsSet(
        metafields: [
          {
            ownerId: {{ "gid://shopify/Shop/" | append: shop.id | json }}
            namespace: "mechanic"
            key: {{ customers_scanned_metafield_key | json }}
            type: "number_integer"
            value: "1"
          }
        ]
      ) {
        metafields {
          id
          namespace
          key
          type
          value
          owner {
            ... on Shop {
              id
            }
          }
        }
        userErrors {
          code
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference

Default option values

Tag to watch for
approved
Ignore tag case
true
Email subject
Your account has been approved!
Email body
Hi {{ customer.first_name | default: "there" }},

Your account for {{ shop.name }} has been approved! Thanks for registering, and we'll see you soon.

Thanks,
The team at {{ shop.name }}