Skip to content

Instantly share code, notes, and snippets.

@sahava
Created June 4, 2020 16:42
Show Gist options
  • Save sahava/4d6629230503b23d1c327d7b6cbea9ad to your computer and use it in GitHub Desktop.
Save sahava/4d6629230503b23d1c327d7b6cbea9ad to your computer and use it in GitHub Desktop.
// 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