How to integrate WooCommerce with Google Optimize

As of April 2017, Google Optimize is now available for free to the public.

The limitation to the free public version is you can only run 3 tests at the same time.

To integrate with a WooCommerce installation you need to have Google Analytics in use with WooCommerce.

The code for Google Optimize piggy-backs on the tracking code for Google Analytics.

In this example I modify the code of WooCommerce’s Google Analytics plugin and the header.php file in my WordPress installation to use Google Optimize.

First, we are going to edit the plugin and add are Google Optimize ID (GTM-0X0X0X) to the plugin’s code.

Navigate to your plugins folder insider your wp-content folder:


The code at line 236 reads

$ga_id = self::get( 'ga_id' );
        $ga_snippet_create = self::tracker_var() . "( 'create', '" . esc_js( $ga_id ) . "', '" . $set_domain_name . "' );”;

You are going to extend this code to add your Google Optimize tracking ID:

$ga_id = self::get( 'ga_id' );
        $ga_snippet_create = self::tracker_var() . "( 'create', '" . esc_js( $ga_id ) . "', '" . $set_domain_name . "' );”.
        self::tracker_var() . "( 'require', 'GTM-0X0X0X' );";

Next, we are going to add Google Optimize’s page hiding snippet to our header.php file and add the following code above the head tag:

<!-- Page hiding snippet (recommended) --> <style>.async-hide { opacity: 0 !important} </style><script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date; h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')}; (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c; })(window,document.documentElement,'async-hide','dataLayer',4000, {'GTM-0X0X0X':true});</script>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.