wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
701 stars 219 forks source link

Add Delay JS exclusion related to Google Tag Manager for WordPress #4071

Closed GeekPress closed 3 years ago

GeekPress commented 3 years ago

Before submitting an issue please check that you’ve completed the following steps:

Describe the bug Google Tag Manager for WordPress plugin is adding its JS variables without the usage of wp_enqueuescript. We might want to exclude the following pattern: var gtm4wp

To Reproduce Steps to reproduce the behavior:

  1. Install and configure Google Tag Manager for WordPress
  2. Turn on Delay JS and keep our default exclusion list
  3. Visit the page and check the console
  4. See error related to: gtm4wp_id_prefix isn't defined

Expected behavior Variable definitions should not be delayed.

Additional context

Google Tag Manager for WordPress is installed on 500,000+ active websites.

The whole script:

<script type="text/javascript">//<![CDATA[
    var gtm4wp_datalayer_name = "dataLayer";
    var dataLayer = dataLayer || [];
    var gtm4wp_use_sku_instead        = 0;
    var gtm4wp_id_prefix              = '';
    var gtm4wp_remarketing            = 1;
    var gtm4wp_eec                    = 1;
    var gtm4wp_classicec              = false;
    var gtm4wp_currency               = 'HKD';
    var gtm4wp_product_per_impression = 10;
    var gtm4wp_needs_shipping_address = false;
    var gtm4wp_business_vertical      = 'retail';
    var gtm4wp_business_vertical_id   = 'id';
//]]>
</script>

Related ticket: https://secure.helpscout.net/conversation/1547254446/272681?folderId=273768

Backlog Grooming (for WP Media dev team use only)

remyperona commented 3 years ago

Reproduce the issue ✅

Reproduced the issue on local installation with GTM4WP + WooCommerce

Identify the root cause ✅

The issue happens because the inline script is delayed, but the external script for WooCommerce tracking is not with the default exclusion list.

The external script is executed immediately, but can't find the inline variable it depends on, and throws the error.

Scope a solution ✅

Estimate the effort ✅

Effort [XS]

Mai-Saad commented 3 years ago

Note: For retest, In google tag manager -> integration -> woocommerce => enable Track enhanced e-commerce