in ,

How to Integrate Bookeo into Google Analytics using CAOS for WordPress

Travel vector created by pikisuperstar - www.freepik.com

In previous posts I explained how to modify CAOS’ tracking code when you’re using gtag.js. Today we’re going to integrate Bookeo into CAOS using analytics.js.

Gtag.js always uses the asynchronous tracking code. Analytics.js, however, can also be used synchronously. Regardless, the implementation of analytics.js looks different:

<!-- This site is running CAOS for Wordpress -->
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments);
}, i[r].l = 1 * new Date();
a = s.createElement(o), m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
})(window, document, 'script', 'http://path.to/wp-content/cache/caos-analytics/analytics.js', 'ga');
ga('create', 'UA-XXXXXXXX-X', {
'cookieName': 'caosLocalGa',
'cookieDomain': 'path.to',
'cookieExpires': 2592000
/**
* Additional configuration can either go here.
*/
});
/**
* And/or here.
*/
ga('send', 'pageview');
view raw caos-analytics.js hosted with ❤ by GitHub

Honestly, modifying gtag.js requires less coding. Still, modifying analytics.js‘ tracking code in CAOS isn’t difficult at all. Today we’re going to integrate Bookeo into CAOS.

If you’re looking for examples to modify the gtag.js tracking code, try CAOS’ Google Optimize integration or Google Ads Conversion Tracking.

Modifying CAOS’ Tracking Code for analytics.js

Since v3.1.3 CAOS sports a set of filters enabling you to freely modify the generated tracking codes using plugins (or mini plugins as I like to call them).

Integrating Bookeo into CAOS is a great way to demonstrate CAOS’ flexibility, because it requires dynamically modifying Google Analytics’ configuration (within the ga('create')-function) and loading a 3rd party plugin (with ga('require')).

This requires hooking into two of CAOS’ filters. We’ll modify Google Analytics’ configuration using caos_analytics_ga_create_config and load and trigger the plugin using caos_analytics_before_send.

Integrating Bookeo into Google Analytics by extending CAOS with a Mini Plugin

Whether you’re a WordPress developer or not, the snippet below is all you need to integrate Bookeo into CAOS. The comments at the top form the base of the plugin and the two functions underneath provide the plugins functionalities.

Copy the below code into a file called caos-bookeo.php and place into your WordPress install’s wp-content folder. Or download it here.

<?php
/**
* Plugin Name: Bookeo for CAOS
* Description: Add Bookeo compatibility to CAOS
* Version: 1.0.0
* Author: Daan van den Bergh
* Author URI: https://woosh.dev
* License: GPL2v2 or later
*/
define('BOOKEO_PLUGIN_FILE', __FILE__);
/**
* Add allowLinker: true to ga('create') configuration.
*
* @param $config
*
* @return array
*/
function caos_add_bookeo_to_ga_config($config)
{
return $config + array('allowLinker' => true);
}
add_filter('caos_analytics_ga_create_config', 'caos_add_bookeo_to_ga_config');
/**
* Add 'require': 'linker' and 'linker:autoLink', ['bookeo.com'] before send.
*
* @param $config
* @param $trackingId
*
* @return array
*/
function caos_add_bookeo($config, $trackingId)
{
return $config + array(
"ga('require', 'linker');",
"ga('linker:autoLink', ['bookeo.com']);"
);
}
add_filter('caos_analytics_before_send', 'caos_add_bookeo', 10, 2);
view raw caos-bookeo.php hosted with ❤ by GitHub

After installing the above plugin, go to your Administrator Area and open the ‘Plugins’-screen. You should notice a new plugin called Bookeo for CAOS. Activate it and you’re all set.

If you’re using any caching plugins, make sure you flush its cache.

Conclusion

Using a mini plugin it’s very easy to modify CAOS’ tracking code for analytics.js. While integrating Bookeo into CAOS for WordPress we’ve shown one of CAOS’ unlimited possiblities to use Google Analytics on your website and tweak it to your needs without losing performance.

Written by Daan van den Bergh

Magento 2 Back-end Developer with a passion for trainlifting, airplane-gliding, hunting trees and creating fake hobbies.

Leave a Reply

Your email address will not be published. Required fields are marked *

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