jcwd
Forum Replies Created
-
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');
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 ); } }
No problems, glad you got it sorted.
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 ); } }
@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?
+1 for this. @acdesigns if you come up with a solution can you post back here?