Auto-tag orders with UTM parameters, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders with UTM parameters

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

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.

Open source
View on GitHub to contribute to this task
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
        customerJourneySummary {
          moments(first: 250) {
            edges {
              node {
                ... 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": [],
            "customerJourneySummary": {
              "moments": {
                "edges": [
                  {
                    "node": {
                      "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
            customerJourneySummary {
              moments {
                edges {
                  node {
                    __typename
                    ... 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_jsonl %}
      {"__typename":"Order","id":"gid:\/\/shopify\/Order\/1234567890","name":"#1234","tags":[],"customerJourneySummary":{}}
      {"__typename":"CustomerVisit","utmParameters":{"campaign":"New Campaign 001","content":null,"medium":"email","source":"newsletter","term":null},"__parentId":"gid:\/\/shopify\/Order\/1234567890"}
      {"__typename":"CustomerVisit","utmParameters":{"campaign":"New Campaign 001","content":"cta_link","medium":"email","source":"drip","term":"automation+shopify"},"__parentId":"gid:\/\/shopify\/Order\/1234567890"}
    {% endcapture %}

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

  {% assign orders = bulkOperation.objects | where: "__typename", "Order" %}
{% endif %}

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

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

  {% if event.topic contains "shopify/orders/" %}
    {% assign parameter_sets = order.customerJourneySummary.moments.edges | map: "node" | map: "utmParameters" %}
  {% elsif event.topic == "mechanic/shopify/bulk_operation" %}
    {% assign parameter_sets = bulkOperation.objects | where: "__typename", "CustomerVisit" | where: "__parentId", order.id | map: "utmParameters" %}
  {% endif %}

  {% 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 | uniq | 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.