Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Call with decorateUrl('https://www.domain.com/url', 'UA-12345-1');
// Outputs the URL string with the Universal Analytics cross-domain linker in place.
// Tracking ID is optional. If you leave it out, the linker will be built
// with the settings of the first tracker found on the page.
function decorateUrl(urlString, trackingId) {
var ga = window[window['GoogleAnalyticsObject']];
if (ga && ga.getAll) {
var trackers = ga.getAll().filter(function(t) {
return trackingId ? t.get('trackingId') === trackingId : true;
});
var tracker = trackers.length ? trackers[0] : undefined;
if (tracker) {
urlString = new window.gaplugins.Linker(tracker).decorate(urlString);
}
}
return urlString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.