Setting cookies from the UTM_ parameter

Hi,
I currently use jQuery to set cookies from the URL parameters of campaigns like utm_source=twitter&utm_term=xyz etc… below is the snippet that works great.


function url(e) {
    "use strict";
    var t = "[\\\\?&]" + e + "=([^&#]*)",
        n = new RegExp(t),
        r = n.exec(window.location.href);
    return r === null ? "" : r[1]
}

function _getCampaign() {
    "use strict";
    var e = $.cookie("utm_campaign"),
        t = unescape(url("utm_campaign"));
    if (e === null || e === "No Campaign ID") {
        if (t === "" || t === null) t = "No Campaign ID";
        $.cookie("utm_campaign", t, {
            expires: 90,
            path: "/"
        })
    }
    t !== e && t !== "" && t !== null && $.cookie("utm_campaign", t, {
        expires: 90,
        path: "/"
    })
}(function(e, t, n) {
    function i(e) {
        return e
    }

    function s(e) {
        return decodeURIComponent(e.replace(r, " "))
    }
    var r = /\\+/g,
        o = e.cookie = function(r, u, a) {
            if (u !== n) {
                a = e.extend({}, o.defaults, a);
                u === null && (a.expires = -1);
                if (typeof a.expires == "number") {
                    var f = a.expires,
                        l = a.expires = new Date;
                    l.setDate(l.getDate() + f)
                }
                u = o.json ? JSON.stringify(u) : String(u);
                return t.cookie = [encodeURIComponent(r), "=", o.raw ? u : encodeURIComponent(u), a.expires ? "; expires=" + a.expires.toUTCString() : "", a.path ? "; path=" + a.path : "", a.domain ? "; domain=" + a.domain : "", a.secure ? "; secure" : ""].join("")
            }
            var c = o.raw ? i : s,
                h = t.cookie.split("; ");
            for (var p = 0, d = h.length; p < d; p++) {
                var v = h[p].split("=");
                if (c(v.shift()) === r) {
                    var m = c(v.join("="));
                    return o.json ? JSON.parse(m) : m
                }
            }
            return null
        };
    o.defaults = {};
    e.removeCookie = function(t, n) {
        if (e.cookie(t) !== null) {
            e.cookie(t, null, n);
            return !0
        }
        return !1
    }
})(jQuery, document);
$(function() {
    _getCampaign()
});

However - that works only for one parameter. If I want to add 5 more, it is tedious - unless there is a clean way to do it without repeating. I was looking into the jQuery cookie plugin at https://github.com/panzi/jQuery-Cookies/ and am able to set multiple cookies like this:


$.cookie({
    utm_source: "utm_source",
    utm_medium: "utm_medium",
    utm_term: "utm_term",
    utm_content: "utm_content",
    utm_campaign: "utm_campaign"
});

However that is hardcoded. So here are my two questions.

  1. Is there a way to clean up and set multiple cookies with the first snippet
  2. If I go with the jQuery cookie plugin, how do I set the values from the URL parameters?

Thanks!

URL parameters can be retrieved in vanilla JS by putting all into one string, then splitting the string by &, then further splitting by =.


var thisURL = document.URL;
var thisQS = thisURL.split('?')[1]; // gets everything after the ?
var thisNVP = thisQS.split('&'); // puts individual name/value pairs into an array ('uvm_=this', 'a=2')

Now you can just loop through the array (or do a for…each) and split using =, then use the key ([0]) as the variable name and the value ([1]) as the key value.

UPDATE: You should probably (just as a matter of redundancy) check to make sure the var doesn’t already exist before setting it. I’ve had some really messed up troubleshooting after accidentally declaring the same var twice (preceeded by “var=”.)

HTH,