Support » Plugin: WooCommerce Google Analytics Integration » Disabled on Load and then Enabled after GDPR Consent

  • Resolved acdesigns

    (@acdesigns)


    Hi Support,

    I’m trying to use this plugin with the GDPR Cookie Compliance plugin (https://en-au.wordpress.org/plugins/gdpr-cookie-compliance/) that can load scripts after the user has consented to having cookies on the site.

    I understand there is a filter (woocommerce_ga_disable_tracking) I can use to enable and disable the GA tracking for your plugin.

    Is there a way to disable the GA tracking on page load and then re-enable it after the user has given consent (using the GDPR Cookie Compliance plugin which can load additional scripts after consent)?

    Thanks!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Support John Coy

    (@johndcoy)

    Automattic Happiness Engineer

    Hi @acdesigns

    The data that is tracked isn’t specific to a user until a purchase is made, where a customer would have to agree to terms before completing the checkout process.

    Either way, there isn’t a way to allow the user to control when the script for Google Analytics is run.

    acdesigns

    (@acdesigns)

    Thanks for confirming John.

    +1 for this. @acdesigns if you come up with a solution can you post back here?

    @acdesigns could you tell me how we can do somethign like this:
    remove_action( 'wp_head', array( WC_Google_Analytics, 'tracking_code_display', 999999 ) );

    So I can conditionally load the script?

    • This reply was modified 7 months, 4 weeks ago by jcwd.
    • This reply was modified 7 months, 4 weeks ago by jcwd.

    Don’t worry. I think I sorted it.

    This function will block this plugin from displaying the tracking code, by default.

    If the user has accepted the use of third party cookies via the GDPR Cookie Consent plugin, then it will enable the display of the tracking code.

    add_action( 'init', 'maybe_consent_woocommerce_google_analytics');
    function maybe_consent_woocommerce_google_analytics() {
        
        // Disable WooCommerce Google Analytics displaying tracking code by default
        $integrations = WC()->integrations->get_integrations();
        remove_action('wp_head', array($integrations['google_analytics'], 'tracking_code_display'), 999999 );
    
        // Display the tracking code if the user has accepted the GDPR Cookie Consent policy
        if ($_COOKIE['viewed_cookie_policy']=='yes' && $_COOKIE["cookielawinfo-checkbox-non-necessary"] =='yes') {
            add_action('wp_head', array($integrations['google_analytics'], 'tracking_code_display'), 999999 );
        }
    
    }

    @jcwd – Thanks heaps for posting your solution. I’m not technical so it’s definitely something I couldn’t have figured out myself.

    I was testing out your function but couldn’t get it working, but then realised it was for GDPR Cookie Consent while I was trying to get it work with GDPR Cookie Compliance.

    I was also trying out the GDPR Cookie Consent plugin but liked GDPR Cookie Compliance as the paid plug-in has better control over the geolocations the banner is displayed in.

    As I said, I’m not coder, but I did manage to hack together something for GDPR Cookie Compliance based on your solution. Thanks heaps again for your solution.

    add_action( 'init', 'maybe_consent_woocommerce_google_analytics');
    function maybe_consent_woocommerce_google_analytics() {
        
        // Disable WooCommerce Google Analytics displaying tracking code by default
        $integrations = WC()->integrations->get_integrations();
        remove_action('wp_head', array($integrations['google_analytics'], 'tracking_code_display'), 999999 );
    
        // Display the tracking code if the user has accepted the GDPR Cookie Compliance policy
    	if ($_COOKIE['moove_gdpr_popup'] != null) {
            add_action('wp_head', array($integrations['google_analytics'], 'tracking_code_display'), 999999 );
        }
    
    }
    • This reply was modified 7 months, 4 weeks ago by acdesigns.

    No problems, glad you got it sorted.

    Nice guys what you make happened with the cookie plugins.
    But how to adapt it for https://en-au.wordpress.org/plugins/cookie-notice/?

    Hi @pictibe

    This should do the trick:

    
    add_action( 'init', 'maybe_consent_woocommerce_google_analytics');
    function maybe_consent_woocommerce_google_analytics() {
        
        // Disable WooCommerce Google Analytics displaying tracking code by default
        $integrations = WC()->integrations->get_integrations();
        remove_action('wp_head', array($integrations['google_analytics'], 'tracking_code_display'), 999999 );
    
        // Display the tracking code if the user has accepted the cookie policy
    	
        if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() ) {
             add_action('wp_head', array($integrations['google_analytics'], 'tracking_code_display'), 999999 );
        }
    }
    

    Wow thanks for fast reply. After activation of the function i get this error:
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /www/htdocs/XXXX/web/wp-includes/class-wp-hook.php on line 288

    Hmmm, not sure about that. I am guessing in your case you might need to call this somewhere else other than 'init'. i.e.

    add_action( 'somehwere_else', 'maybe_consent_woocommerce_google_analytics');

    Where can i find it out? Plugincode of cookie notice?

    I stumbled upon your thread with the same problem, solved it after several hours of trying differens solutions with using the WP plugin borlabs cookie consent. I am not in any way affiliated to it, just found it to be the best solution for my problem of opt-in consent for full e-commerce facebook pixel tracking & google analytics tracking via external plugins.

    Integrated is a script blocker, where you can disable selective javascripts (for example Google Analytics from another plugin) until consent, tutorial for the script blocker can be found here (in German, use google translate): https://de.borlabs.io/kb/monsterinsights-nutzen-script-blocker/

    Hi!
    I was looking exactly for this thread.
    I’m also using GDPR Cookie Compliance and I have already configured the 3rd Party Cookie script blocker and works perfectly for both GA and GTM.
    Now I would like to also start using Google Analytics Ecommerce but I don’t understand where I should put the code you mentioned above. Is it in the plug in or directly in the code of the page?
    I really like the GDRP Cookie compliance Plugin as its free version is quite robust.
    thanks for the help!

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.