How to integrate Google Optimize into CAOS

Recently I added possibilities for WordPress developers to modify CAOS’ tracking snippet using filters. Shortly afterwards I received an inquiry on the support forum asking if I could integrate Google Optimize in CAOS.

Instead of continuously releasing updates, I figured I’d just explain, how you can write your own WordPress plugin, and integrate it into CAOS yourself.

What is Google Optimize?

Google Optimize is a tool for A/B-testing. In other words, it allows you to test different versions of your web pages to see how they perform against an objective you’ve specified.

But a proper test contains measurements. How did your visitors behave when served version A of page X compared to version B? That’s why Google invented Optimize as a plugin for Google Analytics.

  • The Wait is Over.

    Get the Newsletter you've always wanted, now!

    Sign up to receive Biweekly, Free Optimization Tips for WordPress.

    No spam. I promise.

  • Use CAOS with Google Optimize

    You’re running your online business and like any other business you want to tweak it to maximize its performance. Duh, you’re smart. That’s why you’re using CAOS.

    But to squeeze more performance out of your online business, you figured you need A/B tests. Well, you must’ve been the World’s Happiest Camper© once you realized you could easily combine the two!

    Since v3.4.0 CAOS natively supports Google Optimize integration. Simply go to the plugin’s Extensions tab, enable the option and enter your Google Optimize Container ID. Enjoy!

    The snippet of code is really straight forward. It hooks into the recently added caos_process_settings action and adds the specified $optimizeId to its configuration.

    Request my CAOS Expert Configuration service if you’re not comfortable writing code and/or you need another custom integration. Read more

    Mind you that this snippet will only work with gtag.js enabled in CAOS. (Why gtag.js?)

    <?php
    /**
    * @formatter:off
    * Plugin Name: Google Optimize for CAOS
    * Plugin URI: https://daan.dev/google-optimize-caos/
    * Description: Add Google Optimize for CAOS in gtag.js
    * Version: 1.0.0
    * Author: Daan van den Bergh
    * Author URI: https://daan.dev
    * License: GPL2v2 or later
    * @formatter:on
    */
    if (!defined('ABSPATH')) {
    exit;
    }
    /**
    * Replace 'YOUR-OPTIMIZE-CONTAINER-ID' with, well, your optimize container id.
    */
    function caos_google_optimize()
    {
    $optimizeId = 'YOUR-OPTIMIZE-CONTAINER-ID';
    if (CAOS_OPT_REMOTE_JS_FILE == 'gtag.js') {
    add_filter(
    'caos_gtag_config', function ($config, $trackingId) use ($optimizeId) {
    return $config + array('optimize_id' => $optimizeId);
    }, 10, 2
    );
    }
    }
    add_action('caos_process_settings', 'caos_google_optimize');

    Simply copy the above code into a file called caos-google-optimize.php, insert your Optimize ID in the position of the placeholder (make sure to place it in between the quotation marks to prevent Syntax Errors) and save it inside WordPress’ plugins-folder, e.g. /path/to/wordpress/wp-content/plugins/caos-google-optimize.php. Or, if you want to save yourself the hassle, download this mini plugin for WordPress from Github!

    Enable your mini Plugin for CAOS

    Now, visit the Plugins screen inside your WordPress Administrator area. You’ll notice a new plugin, called Google Optimize for CAOS. Activate it like you normally would and off you go! If you’re using any caching plugins, make sure to flush all caches.

    And that’s it! You have now successfully integrated Google Optimize into CAOS! I hope this tutorial was clear enough. If you have any remarks or questions, please leave a comment.

    Daan van den Bergh

    Daan van den Bergh is a carefully seasoned web developer. His methods consist of thinly slicing your website’s beef and serve you with a platter of the best performance carpaccio on a bed of rocket — the only thing he’s more passionate about is food. Hire him at ffwp.dev.

    Leave a Comment

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