Cookie consent plugin block scripts until consent
-
Hi,
I’m working on a cookie consent plugin for WordPress (for my employer), and they requested that I make support for your plugin since a lot of our customers are WooCommerce webshops using this plugin to send data to their GA4.
The cookie plugin changes consent state using ajax, when user clicks on any of the consent buttons in the cookie popup window. As of right now our cookie plugin are not using a cookie scanner, but instead the tracking scripts are saved in the plugin options, and then added to head/body elements upon consent.
I found the filter woocommerce_ga_disable_tracking, but I have experienced a few problems. However I’m unsure if the problem lies with my implementation.
The code below, shows what I have done to disable tracking until consent. The function is in a separate file located in my plugin’s functions folder.<?php add_filter( 'woocommerce_ga_disable_tracking', 'cookie_care_woocommerce_ga_compatibility', 20 ); function cookie_care_woocommerce_ga_compatibility( $disable_tracking ) { ? ? // cookie name, to make code easier readable ? ? $cookie_name = 'cookie-care-consent'; ? ? if ( class_exists( 'WC_Google_Analytics' ) && class_exists( 'Cookie_Care_Settings' ) ) { ? ? // return true for disabling tracking, return false for not disabling tracking ? ? ? ? $disable_tracking = ( isset( $_COOKIE[ $cookie_name ] ) && $_COOKIE[ $cookie_name ] === 'necessary_analytics' || $_COOKIE[ $cookie_name ] === 'necessary_marketing_analytics' ) ? false : true; ? ? } ? ? // always return disable tracking, even if it has not been modified. ? ? return $disable_tracking; }
The problems I experience is that sometimes, either the _ga, the _ga_{ID} or both cookie(s) will exist before user consent or when the cookie-care-consent value does not include ‘analytics’. And I have checked the $disable_tracking using error_log(), which shows the correct value depending on the consent.
Also when changing consent from no consent it requires a page reload (actually two page reloads) before being able to see the google analytics cookies in the developer console window.
Am I doing something completely wrong, lacking functionality or is there something else wrong?
- The topic ‘Cookie consent plugin block scripts until consent’ is closed to new replies.