Auto-tag orders by app, with Mechanic.

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

Auto-tag orders by app

Some apps are responsible for placing orders. Use this task to tag orders, both incoming and historical, based on which app was responsible for placing them.

Runs Occurs whenever an order is created, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes app titles and order tags and test mode.

15-day free trial – unlimited tasks

Documentation

Some apps are responsible for placing orders. Use this task to tag orders, both incoming and historical, based on which app was responsible for placing them.

This task scans your incoming orders, tagging them based on the Shopify app responsible for placing each one. Run this task manually to scan and tag your existing orders.

Configure this task with app titles on the left, and order tags to add on the right.

Use the "Test mode" option to see what the task intends to do, without actually having the task actually make any changes to your customers.

Please note: this task works by app, and as such does not apply to orders that are created by a sales channel. It's sometimes difficult to distinguish whether a third-party is using an app or a sales channel; if you're having trouble with this task, try its companion, Auto-tag orders by sales channel.

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/orders/create
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
app titles and order tags (keyval, required), test mode (boolean)
Code
{% if event.topic == "shopify/orders/create" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        tags
        events(
          first: 1
          sortKey: CREATED_AT
          query: "verb:placed"
        ) {
          edges {
            node {
              id
              ... on BasicEvent {
                appTitle
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "name": "#1234",
            "tags": [],
            "events": {
              "edges": [
                {
                  "node": {
                    "appTitle": {{ options.app_titles_and_order_tags__keyval_required.first.first | json }}
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign app_title = result.data.order.events.edges.first.node.appTitle %}
  {% assign tag_to_add = options.app_titles_and_order_tags__keyval_required[app_title] %}

  {% if app_title == blank %}
    {% log message: "This order was not created by an app. Skipping." %}
  {% elsif tag_to_add == blank %}
    {% log message: "No tag found for this app. Skipping.", app_title: app_title %}
  {% elsif result.data.order.tags contains tag_to_add %}
    {% log message: "The order already has the tag for this app. Skipping.", app_title: app_title, tag_to_add: tag_to_add %}
  {% elsif options.test_mode__boolean %}
    {% action "echo" order_id: result.data.order.id, order_name: result.data.order.name, order_app_title: app_title, order_tag_to_add: tag_to_add %}
  {% else %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ result.data.order.id | json }}
          tags: {{ tag_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            __typename
            id
            name
            tags
            events(
              first: 1
              sortKey: CREATED_AT
              query: "verb:placed"
            ) {
              edges {
                node {
                  __typename
                  id
                  ... on BasicEvent {
                    appTitle
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture bulkOperation_json %}
      {
        "objects": [
          {
            "__typename": "Order",
            "name": "#1234",
            "tags": [],
            "id": "gid://shopify/Order/1234567890"
          },
          {
            "__typename": "BasicEvent",
            "id":" gid://shopify/BasicEvent/1234567890",
            "appTitle": {{ options.app_titles_and_order_tags__keyval_required.first.first | json }},
            "__parentId": "gid://shopify/Order/1234567890"
          }
        ]
      }
    {% endcapture %}

    {% assign bulkOperation = bulkOperation_json | parse_json %}
  {% endif %}

  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
  {% assign events = bulkOperation.objects | where: "__typename", "BasicEvent" %}
  {% assign app_title_counts = hash %}

  {% for event in events %}
    {% assign app_title = event.appTitle %}

    {% if app_title == blank %}
      {% assign app_title = "(not placed via app)" %}
    {% endif %}

    {% assign app_title_counts[app_title] = app_title_counts[app_title] | plus: 1 %}
  {% endfor %}

  {% log app_title_counts: app_title_counts %}

  {% if options.test_mode__boolean %}
    {% assign summaries = array %}
  {% endif %}
          
  {% for order in orders %}
    {% assign order_app_title = nil %}
    {% assign order_tag_to_add = nil %}

    {% assign order_event = events | where: "__parentId", order.id | first %}
    {% assign order_app_title = order_event.appTitle %}

    {% if order_app_title != blank %}
      {% unless order.tags contains options.app_titles_and_order_tags__keyval_required[order_app_title] %}
        {% assign order_tag_to_add = options.app_titles_and_order_tags__keyval_required[order_app_title] %}
      {% endunless %}
    {% endif %}

    {% if order_app_title != blank %}
      {% if options.test_mode__boolean %}
        {% assign summary = hash %}
        {% assign summary["order_id"] = order.id %}
        {% assign summary["order_name"] = order.name %}
        {% assign summary["order_tags"] = order.tags %}
        {% assign summary["order_app_title"] = order_app_title %}
        {% assign summary["order_tag_to_add"] = order_tag_to_add %}
        {% assign summaries[summaries.size] = summary %}
      {% elsif order_tag_to_add != blank %}
        {% action "shopify" %}
          mutation {
            tagsAdd(
              id: {{ order.id | json }}
              tags: {{ order_tag_to_add | uniq | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {% if options.test_mode__boolean %}
    {% action "echo" summaries %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
App titles and order tags
{"SomeApp"=>"some_app"}