This is a common issue with Google Tag Manager when tracking outbound links. When the outbound link has target=”_blank” attribute GTM doesn’t trigger the event tag to record the outbound click. An easy way to fix this is to remove the target=”_blank” attribute from your links, that said if you have hundreds of posts with external links that already open in a new tab this could take forever.

We came up with a quick work around that allows you to still open external links in a new tab but you don’t have to set ‘open in a new tab’ in WordPress.

This method uses a short script to remove all target=”_blank” attributes from external links and we will create a tag in GTM to then add the attribute again using GTM.

Remove target=”_blank” Attributes

You can do this by adding a snippet to your site footer, we do this by adding the following function to your child theme functions.php.

function external_link_attribute() {
  	.forEach(link => link.removeAttribute('target'));
add_action( 'wp_footer', 'external_link_attribute', 20 );

This function will simply look for all links that contain target=”_blank” and remove the attribute.

Create a Tag in GTM to open outbound links in a new tab

This step requires creating a new tag in GTM that will then open outbound links in a new tab. This method follows the guide created

This involves adding the following script to a new tag that will open all external links in a new tab. This is done from GTM so it will trigger after the outbound event click.

Go into GTM and create a new tag named HTML – LinkOpener – All Pages. Set the Tag Type to Custom HTML. Paste the code below into the source code window. Set the Firing Trigger to All Pages.

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';

Your tag should look like this.

Gtm Outbound Link New Tab Tag

Test Your Events

Once you have completed the steps above you can publish your tags and then test your site again. You should now see your events in Google Analytics. If you need help with your Google Tag Manager intergrations you can contact us.