Tag online orders by their ?ref= referral codes with Mechanic.

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

Tag online orders by their ?ref= referral codes

by Isaac Bowen (team@usemechanic.com)

Have your referral partners send customers using ?ref=referral-code-here URLs, then use this task to automatically tag each customer's order according to the ref code they arrived with. (The referral code will be saved in a browser cookie for one year.)

Runs when an order is created.

15-day free trial – unlimited tasks

Developer details

Events
when an order is created (shopify/orders/create)
Options
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "order",
          {{ order.id }}
        ],
        {
          "tags": "some-referrer-code"
        }
      ]
    }
  }
{% else %}
  {% assign tags_to_save = order.tags %}

  {% for note_attribute in order.note_attributes %}
    {% if note_attribute.name == "ref" %}
      {% capture tags_to_save %}{{ tags_to_save }},{{ note_attribute.value }}{% endcapture %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if tags_to_save != order.tags %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "order",
            {{ order.id }}
          ],
          {
            "tags": {{ tags_to_save | json }}
          }
        ]
      }
    }
  {% endif %}
{% endif %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)