Skip to content

Instantly share code, notes, and snippets.

@rickrduncan
Last active April 25, 2019 09:12
Show Gist options
  • Save rickrduncan/0958eaf3bda1d2e4afd1ce0202fdf09e to your computer and use it in GitHub Desktop.
Save rickrduncan/0958eaf3bda1d2e4afd1ce0202fdf09e to your computer and use it in GitHub Desktop.
GTM code to open external links in a new tab. Learn more at http://rickrduncan.com/pro/analytics/open-external-links-new-tab-gtm.
<script>
var links = document.links;
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
if (links[i].hostname != window.location.hostname && links[i].protocol != 'tel:' && links[i].protocol != 'mailto:' ) {
links[i].target = '_blank';
links[i].rel = 'noopener noreferrer';
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment