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.
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 v4.2.0 CAOS’ native Google Optimize support has been moved to a separate plugin, which can be downloaded for free from Github. It’ll add a configuration field to the Extensions tab, making it much easier to configure.
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.
Hire me if you’re not comfortable writing code and/or you need another custom integration.
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.