Skip to content

Instantly share code, notes, and snippets.

@ryanbattles
Created November 16, 2015 16:51
Show Gist options
  • Save ryanbattles/0aceeffc1538a85923f7 to your computer and use it in GitHub Desktop.
Save ryanbattles/0aceeffc1538a85923f7 to your computer and use it in GitHub Desktop.
Tracking Referrers
/* --------------------------------------------------
:: Track Referrers
-------------------------------------------------- */
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(function() {
if ($.cookie('myapp_referrer') == null || $.cookie('myapp_referrer') == "" ) {$.cookie('myapp_referrer', document.referrer, { expires: 180, path: '/', domain: 'myapp.com', secure: true });
}
if (getParameterByName('utm_source') !== "") { $.cookie('utm_source', getParameterByName('utm_source'), { expires: 180, path: '/', domain: 'myapp.com', secure: true });
}
if (getParameterByName('utm_medium') !== "") { $.cookie('utm_medium', getParameterByName('utm_medium'), { expires: 180, path: '/', domain: 'myapp.com', secure: true });
}
if (getParameterByName('utm_term') !== "") { $.cookie('utm_term', getParameterByName('utm_term'), { expires: 180, path: '/', domain: 'myapp.com', secure: true });
}
if (getParameterByName('utm_content') !== "") { $.cookie('utm_content', getParameterByName('utm_content'), { expires: 180, path: '/', domain: 'myapp.com', secure: true });
}
if (getParameterByName('utm_campaign') !== "") { $.cookie('utm_campaign', getParameterByName('utm_campaign'), { expires: 180, path: '/', domain: 'myapp.com', secure: true });
}
});
@TimJohns
Copy link

Ryan,

First of all, thanks so much for the great article at https://ryanbattles.com/post/intercom-tracking-conversions, which led me to this repo.

For clarity, it appears that the referrer implementation is consistent with the paragraph "On the Application", which describes first-link attribution, but the code for the UTM parameters appears to implement last-link attribution. Specifically the corresponding UTM parameter cookie appears to be updated with whatever non-empty, non-null value that is in the most recently provided UTM parameter.

Is that accurate, and if so, is that intentional? Either approach is valid, and I believe the default for GA is last-link attribution, but I keep circling around the steps in the application-site paragraph, and it's causing me a bit of cognitive dissonance.

Thanks again for the great blog post, and any further clarity on this one issue is appreciated.

  • Tim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment