Fix Google Tag Manager Event Trigger When Clicking Links That Open In New Tab

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 workaround 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() {
  ?>
  <script> 
	Array.from(document.querySelectorAll('a[target="_blank"]'))
  	.forEach(link => link.removeAttribute('target'));
  </script>
  <?php
}
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 rickrduncan.com.

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.

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

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 integrations you can contact us.

Conclusion

Always make sure to use proper links on your website to avoid any SEO issues with Google tag manager.

Always make sure to keep your WordPress core, plugins, and themes up to date, as well as maintain strong password policies, to enhance the overall security of your website further. Additionally, consider other security measures like using a reputable web hosting provider and regular backups.

For more information on fixing Google Tag Manager event triggers, maintenance website, or hiring services for SEO, let us know your thoughts on custom plans and support services.

Written By
Adam
Founder of WP Helper.
You will also like these articles

Relax Knowing Your WordPress Site Is Secure & Running Smoothly 24/7

Let us manage your WordPress site, everything from security to updates will be taken care of. Support plans also come with dedicated support so we can do anything from adding content to customising your site for you.

Same Day Professional WordPress Support

Get WordPress Support Today

Need help with a single WordPress problem, today? We can help with anything from adding analytics tracking code to site hack recovery. Full money back guarantee on all jobs.

1. Submit a Support Request

Use our support ticket form below to send details of your problem to our developers.

2. Get a Quote

We will review your request and provide a quote within 24 hours (but usually within a few hours).

3. We fix your WordPress problem

Our team will begin fixing your WordPress problem the same day.

4. 100% Money Back Guarantee

If we can’t fix the problem for the amount quoted we will refund you 100%.

  • Do you have a screenshot of the issue or have a copy of the theme or plugin that is at fault? If you want to upload php, html or css please zip first.
    Drop files here or
    Accepted file types: jpg, jpeg, png, pdf, zip, gzip, rar, doc, txt, Max. file size: 15 MB, Max. files: 10.