Title: Cookie consent plugin block scripts until consent
Last modified: January 11, 2024

---

# Cookie consent plugin block scripts until consent

 *  Resolved [Jonas Bjørn Pedersen](https://wordpress.org/support/users/joped/)
 * (@joped)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/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.
 *     ```wp-block-code
       <?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?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [David](https://wordpress.org/support/users/dlim_vernier/)
 * (@dlim_vernier)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17339137)
 * I’m also dealing with this same problem where we’re interested in using this 
   plugin, but since I can’t disable cookies until cookie consent is given (custom
   integration), I can’t use this plugin.
 * I tried the filter you’ve found ‘woocommerce_ga_disable_tracking’ and still had
   cookies loaded. It may be that the ecommerce tracking is no longer tracked, but
   the cookies are still loaded.
 * I also saw a load_opt_out() static function in class-wc-abstract-google-analytics-
   js.php and tried that code as well. But three cookies are still loaded without
   consent.
 *  Plugin Support [Shameem – a11n](https://wordpress.org/support/users/shameemreza/)
 * (@shameemreza)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17341499)
 * Hi [@joped](https://wordpress.org/support/users/joped/),
 * Looking at your code, it looks like you’re on the right track with the `woocommerce_ga_disable_tracking`
   filter. However, a few things could be causing the issues you’re experiencing.
    1. **Timing:** The filter may be applied after the Google Analytics script has 
       already been loaded. This could be why you’re seeing cookies before the user
       has given consent. You might need to check the execution order of your scripts.
    2. **Page Reloads:** It’s normal to require a page reload for the changes to take
       effect. This is because the Google Analytics script is loaded when the page 
       is initially loaded, and any changes to the cookies won’t apply until the next
       page loads.
    3. **Cookie Existence:** The `_ga` and `_ga_{ID}` cookies are set by Google Analytics,
       not by the WooCommerce Google Analytics Integration plugin. Therefore, these
       cookies might be set by other scripts on your site or by Google Analytics itself
       if it’s been loaded elsewhere on your site.
 * Hi [@dlim_vernier](https://wordpress.org/support/users/dlim_vernier/),
 * The `load_opt_out()` function intended to provide an opt-out for users who have
   already given consent. It’s not designed to prevent the initial loading of cookies.
 * Please note that these particular forums are meant for general support with the
   core functionality of WooCommerce Google Analytics Integration itself. For development
   and custom coding questions, it’s best to ask for insight related to those on
   either the [WooCommerce Advanced Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the [WooCommerce Community Slack](https://woo.com/community-slack/). Many 
   of our developers hang out there and will be able to offer insights into your
   question.
 * I wish I could help more, but hopefully, this gets you going in the right direction
   to get some further insight/information.
 *  Thread Starter [Jonas Bjørn Pedersen](https://wordpress.org/support/users/joped/)
 * (@joped)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17341671)
 * [@dlim_vernier](https://wordpress.org/support/users/dlim_vernier/), I’m kind 
   of relived to not be the only one struggling with this. 
   [@shameemreza](https://wordpress.org/support/users/shameemreza/)
   thank you for that explaination, I will look into this. The site is using Goolge
   Tag Manager that is coupled together with Google Analytics.
 *  [anastas10s](https://wordpress.org/support/users/anastas10s/)
 * (@anastas10s)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17342571)
 * Hi there [@joped](https://wordpress.org/support/users/joped/) 👋
 * > I will look into this. The site is using Goolge Tag Manager that is coupled
   > together with Google Analytics.
 * That’s great to hear! Please let us know how it goes. We’re happy to help, so
   please feel free to reach out to us again if you have any other questions.
 *  Thread Starter [Jonas Bjørn Pedersen](https://wordpress.org/support/users/joped/)
 * (@joped)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17344592)
 * [@anastas10s](https://wordpress.org/support/users/anastas10s/) I will let you
   know what I find out. However just looking at the response from [@shameemreza](https://wordpress.org/support/users/shameemreza/)
   makes me think that the problem might be with our setup of google tag manager.
 *  Plugin Support [Shameem – a11n](https://wordpress.org/support/users/shameemreza/)
 * (@shameemreza)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17344738)
 * Hi [@joped](https://wordpress.org/support/users/joped/),
 * Yes, the issue may lies with your setup. Google Tag Manager and Google Analytics
   work together, but if not set up correctly, it can cause problems like the one
   you’re experiencing.
 * Please feel free to ask if you have any more questions or need further assistance.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Cookie consent plugin block scripts until consent’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce-google-analytics-integration/assets/icon-256x256.
   png?rev=3234358)
 * [Google Analytics for WooCommerce](https://wordpress.org/plugins/woocommerce-google-analytics-integration/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce-google-analytics-integration/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce-google-analytics-integration/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce-google-analytics-integration/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce-google-analytics-integration/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce-google-analytics-integration/reviews/)

## Tags

 * [cookie consent](https://wordpress.org/support/topic-tag/cookie-consent/)

 * 6 replies
 * 4 participants
 * Last reply from: [Shameem – a11n](https://wordpress.org/support/users/shameemreza/)
 * Last activity: [2 years, 3 months ago](https://wordpress.org/support/topic/cookie-consent-plugin-block-scripts-until-consent/#post-17344738)
 * Status: resolved