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

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)
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 %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Online store JavaScript
// https://gist.github.com/bronson/6707533
var cookie = function(name, value, ms) {
    if(arguments.length < 2) {
        // read cookie
        var cookies = document.cookie.split(';')
        for(var i=0; i < cookies.length; i++) {
            var c = cookies[i].replace(/^\s+/, '')
            if(c.indexOf(name+'=') == 0) {
                return decodeURIComponent(c.substring(name.length+1).split('+').join(' '))    
            }
        }
        return null
    }

    // write cookie
    var date = new Date()
    date.setTime(date.getTime()+ms)
    document.cookie = name+"=" + encodeURIComponent(value) + (ms ? ";expires="+date.toGMTString() : '') + ";path=/"
}

var refKeyval = window.location.search.split(/[?&]/).filter(function (keyval) { return keyval.match(/^ref/) })[0];
var refFromCookie = cookie('ref');

if (refFromCookie || refKeyval) {
  var ref = refFromCookie || refKeyval.split('=')[1];

  if (!refFromCookie) {
    cookie('ref', ref, 1000*60*60*24*365);
  }

  var xhr = new XMLHttpRequest();
  xhr.open('POST', '/cart/update.json', true);

  //Send the proper header information along with the request
  xhr.setRequestHeader('Content-Type', 'application/json');

  xhr.send(JSON.stringify({ attributes: { ref: ref } }));
}
When this task is installed, this code is loaded into the online storefront using a ScriptTag.