Auto-tag customers based on discount codes used with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-tag customers based on discount codes used

by Mechanic Team (team@usemechanic.com)

Use this task to easily identify customers who used certain discount codes. This task runs automatically on incoming orders, and can be run manually to tag customers based on historical orders.

Runs when an order is created and when a user triggers the task. Configuration includes discount codes and tags.

15-day free trial – unlimited tasks

Documentation

This task runs automatically on incoming orders. Run this task manually to scan your store's order history, tagging customers based on historical orders. (To scan orders older than 60 days, enable "Read all orders".)

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 an order is created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
Options
discount codes and tags (keyval, required)
Script
{% if event.preview %}
  {% assign tags = array %}
  {% for keyval in options.discount_codes_and_tags__keyval_required %}
    {% assign tags[tags.size] = keyval[1] %}
    {% if forloop.index > 3 %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% capture mutation %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Customer/1234567890"
        tags: [
          {% for tag in tags %}
            {{ tag | json }}
          {% endfor %}
        ]
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endcapture %}

  {% action "shopify" mutation %}
{% elsif event.topic contains "shopify/orders/" %}
  {% comment %}
    Remember: only ever one discount code per order
  {% endcomment %}
  {% assign discount_code = order.discount_applications | where: "type", "discount_code" | map: "code" | first %}

  {% if discount_code == blank %}
    {"log": "No discount code for this order; nothing to do."}
  {% else %}
    {% assign tag = options.discount_codes_and_tags__keyval_required[discount_code] %}

    {% if tag == blank %}
      {"log": "No tags for discount code {{ discount_code }} - skipping tagging."}
    {% else %}
      {% capture mutation %}
        mutation {
          tagsAdd(
            id: {{ order.customer.admin_graphql_api_id | json }}
            tags: {{ tag | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endcapture %}

      {% action "shopify" mutation %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign query_parts = array %}
  {% for keyval in options.discount_codes_and_tags__keyval_required %}
    {% assign query_parts[query_parts.size] = "discount_code:" | append: keyval[0] %}
  {% endfor %}
  {% assign query = query_parts | join: " " %}

  {"log": {{ query | json | prepend: "Searching for orders matching " | json }}}

  {% assign cursor = nil %}
  {% for n in (0..100) %}
    {% capture orders_query %}
      query {
        orders(
          first: 250
          after: {{ cursor | json }}
          query: {{ query | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              discountCode
              customer {
                id
                tags
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign orders_result = orders_query | shopify %}

    {% for edge in orders_result.data.orders.edges %}
      {% assign order_node = edge.node %}
      {% assign discount_code = order_node.discountCode %}
      {% assign tag = options.discount_codes_and_tags__keyval_required[discount_code] %}
      {% if tag %}
        {% unless order_node.customer.tags contains tag %}
          {% capture mutation %}
            mutation {
              tagsAdd(
                id: {{ order_node.customer.id | json }}
                tags: {{ tag | json }}
              ) {
                userErrors {
                  field
                  message
                }
              }
            }
          {% endcapture %}

          {% action "shopify" mutation %}
        {% endunless %}
      {% endif %}
    {% endfor %}

    {% if orders_result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = orders_result.data.orders.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.