Created
June 4, 2020 16:42
-
-
Save sahava/4d6629230503b23d1c327d7b6cbea9ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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