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.
Try this taskWhen it runs
- Occurs whenever an order is created, with a 2 minute delay
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
- Tag with UTM campaign
- Tag with UTM content
- Tag with UTM medium
- Tag with UTM source
- Tag with UTM term
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/orders/create+2.minutes
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Option types and flags
- 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)
Task code
{% 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 Liquid reference