Auto-tag orders with UTM parameters, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders with UTM parameters

by Isaac Bowen (team@usemechanic.com)

This task automatically tags incoming orders with the UTM campaign, content, medium, source, and/or term associated with the customer's visit. Run this task manually to tag your existing orders.

Runs when an order is created, when a user triggers the task, and when a bulk operation finishes. Configuration includes tag with utm campaign, tag with utm content, tag with utm medium, tag with utm source, and tag with utm term.

15-day free trial – unlimited tasks

Documentation

This task automatically tags incoming orders with the UTM campaign, content, medium, source, and/or term associated with the customer's visit. Run this task manually to tag your existing 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)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
tag with utm campaign (boolean), tag with utm content (boolean), tag with utm medium (boolean), tag with utm source (boolean), tag with utm term (boolean)
Script
{% comment %}
  {{ options.tag_with_utm_campaign__boolean }}
  {{ options.tag_with_utm_content__boolean }}
  {{ options.tag_with_utm_medium__boolean }}
  {{ options.tag_with_utm_source__boolean }}
  {{ options.tag_with_utm_term__boolean }}
{% endcomment %}

{% assign orders = array %}

{% if event.topic contains "shopify/orders/" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        name
        tags
        customerJourney {
          moments {
            ... on CustomerVisit {
              utmParameters {
                campaign
                content
                medium
                source
                term
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "name": "#1234",
            "tags": [],
            "customerJourney": {
              "moments": [
                {
                  "utmParameters": {
                    "campaign": "Google Shopping",
                    "content": "shoppingcontent",
                    "medium": "cpc",
                    "source": "google",
                    "term": "shoppingterm"
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign orders[orders.size] = result.data.order %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      orders {
        edges {
          node {
            __typename
            id
            name
            tags
            customerJourney {
              moments {
                ... on CustomerVisit {
                  utmParameters {
                    campaign
                    content
                    medium
                    source
                    term
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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_objects_json %}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","name":"#1234","tags":[],"customerJourney":{"moments":[{"utmParameters":{"campaign":"Google Shopping","content":"shoppingcontent","medium":"cpc","source":"google","term":"shoppingterm"}}]}}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = bulkOperation_objects_json | parse_jsonl %}
  {% endif %}

  {% assign orders = bulkOperation.objects %}
{% endif %}

{% assign parameter_names = "campaign,content,medium,source,term" | split: "," %}

{% for order in orders %}
  {% assign tags_to_add = array %}

  {% assign parameter_sets = order.customerJourney.moments | map: "utmParameters" %}
  {% for parameter_set in parameter_sets %}
    {% for parameter_name in parameter_names %}
      {% assign key = "tag_with_utm_" | append: parameter_name | append: "__boolean" %}

      {% if options[key] == false or parameter_set[parameter_name] == blank %}
        {% continue %}
      {% elsif order.tags contains parameter_set[parameter_name] %}
        {% log message: "Order already has this parameter value as a tag", order_id: order.id, order_name: order.name, order_tags: order.tags, parameter_name: parameter_name, parameter_value: parameter_set[parameter_name] %}
        {% continue %}
      {% endif %}
      
      {% assign tags_to_add[tags_to_add.size] = parameter_set[parameter_name] %}
    {% endfor %}
  {% endfor %}

  {% if tags_to_add != blank %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.