Back to Community

Troubleshooting Common GTM4WP Click and Navigation Issues in WooCommerce

Content

If you're using the 'GTM4WP – A Google Tag Manager (GTM) plugin for WordPress' with WooCommerce, you might encounter some tricky issues where product links stop working, browser back buttons behave strangely, or JavaScript errors appear. These problems are often related to the plugin's enhanced ecommerce tracking scripts and can be browser-specific. This guide compiles the most common issues and their solutions based on community reports.

Common Symptoms

Users typically report one or more of the following problems:

  • Clicking on a WooCommerce product in an archive (shop/category page) does nothing.
  • After navigating to a product and clicking the browser's back button, the previously clicked product link is no longer functional.
  • Clicking the back button in Safari (and sometimes other browsers) causes an unexpected redirect back to the product page instead of staying on the archive.
  • JavaScript errors, particularly in Safari, related to variables like gtm4wp_last_selected_product_variation.
  • Conflicts with other JavaScript functions, such as quick view plugins or custom carousels.

Why These Issues Happen

The root cause is often found in the gtm4wp-woocommerce-enhanced.js file. This script adds event listeners to product links to send tracking data to Google Tag Manager before the link is followed. To ensure the tracking data is sent, the script sometimes uses event.preventDefault() to stop the browser's default navigation, then programmatically triggers the navigation after the tracking event is complete. While this is effective for tracking, it can interfere with the browser's history management and other JavaScript events, especially in Webkit-based browsers like Safari.

How to Troubleshoot and Resolve These Issues

1. Check for a Plugin Update

Many of these issues have been addressed in newer versions of the plugin. The first and most important step is to ensure you are running the latest stable version of GTM4WP. The 'GTM4WP – A Google Tag Manager (GTM) plugin for WordPress' team has released several fixes for these specific problems.

2. Test with a Beta Version

If you are experiencing an issue that is not resolved in the current stable release, a fix may already be available in a beta version. For example, a fix for a critical preventDefault issue was included in version 1.15 beta. You can find beta releases on the plugin's GitHub page. Always test beta versions on a staging site before deploying to production.

3. Modify the Event Callback (Advanced)

One specific fix involved modifying the eventCallback function within the gtm4wp-woocommerce-enhanced.js file to better handle container IDs. If you are comfortable editing JavaScript files, you can try this change.

Locate this code around line 767:

'eventCallback': function( container_id ) {
    if (window.gtm4wp_first_container_id != container_id) {
     // only call this for the first loaded container
     return true;
    }

Change it to:

'eventCallback': function( container_id ) {
    if ( "undefined" !== typeof container_id && window.gtm4wp_first_container_id != container_id) {
     // only call this for the first loaded container
     return true;
    }

This change adds a type check to prevent errors if the container_id variable is undefined.

4. Check for JavaScript Conflicts

The plugin's event listener can conflict with other scripts that also listen for clicks on product links, such as quick view plugins or custom AJAX functions. To troubleshoot:

  • Temporarily disable other plugins to see if the conflict resolves.
  • Check your browser's JavaScript console for any errors that might point to a specific conflict.
  • If you have custom code that uses event.preventDefault() on product links, you may need to adjust its order of execution or use the event.defaultPrevented property to check if GTM4WP has already handled the event.

5. Disable Enhanced Ecommerce Tracking (Temporary Workaround)

If the issue is critical and you need an immediate fix, you can temporarily disable the problematic feature. In your WordPress admin, go to GTM4WP -> Integration -> WooCommerce and uncheck the option for Track enhanced e-commerce. Be aware that this will disable product impression and click tracking until a permanent solution is found.

Conclusion

Click and navigation issues with GTM4WP are often complex and browser-specific, but they are usually solvable. The most reliable solution is to keep the plugin updated, as the development team is actively working on fixes for these known problems. For persistent issues, checking the plugin's GitHub repository for ongoing discussions and potential patches is highly recommended.

Related Support Threads Support