Support Forum Support Forum GDPR Cookie Compliance Geolocation + Google Tracking Disabled on Load and then Enabled after Consent

Topic Resolution: Resolved
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avataracdesigns
    February 29, 2020 @ 11:39 am

    Hello,

    I posted a question on WordPress.org (https://wordpress.org/support/topic/google-tracking-disabled-on-load-and-then-enabled-after-gdpr-consent/#post-12492912) but was requested to redirect it here.

    I received a code snippet solution that would disable GA tracking by default when using WooCommerce Google Analytics Integration plugin and then re-enabling after receiving user consent.

    add_action( 'init', 'maybe_consent_woocommerce_google_analytics');
    function maybe_consent_woocommerce_google_analytics() {
      // Disable WooCommerce Google Analytics displaying tracking code by default
      if ( class_exists( 'WooCommerce' ) ) :
    	  $integrations = WC()->integrations->get_integrations();
    	  if ( isset( $integrations['google_analytics'] ) ) :
    		  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 ( function_exists( 'gdpr_cookie_is_accepted' ) ) :
    			  /* supported types: 'strict', 'thirdparty', 'advanced' */
    			  if ( gdpr_cookie_is_accepted( 'thirdparty' ) ) :
    		      add_action('wp_head', array( $integrations['google_analytics'], 'tracking_code_display'), 999999 );
    		    endif;
    		  endif;
    		endif;
    	endif;
    }

    My follow up question is about how this function would work with the geolocation option in the premium plugin.

    I understand if I enable the geolocation option for EU, users from the other countries will have cookies and scripts activated by default (i.e. they will not see the Cookie Banner at all).

    So will this code above still work with the geolocation option enabled?

    Since the WooCommerce Google Analytics tracking code is set to be disabled by default, and there is now no trigger (i.e. user clicking Accept) to re-enable the tracking code.

    Moove AgencyMoove Agency
    March 03, 2020 @ 1:25 pm

    Hi acdesigns,

    Thanks for your comments.

    You can use the following code snippet with GeoIP features enabled:

    add_action( 'init', 'maybe_consent_woocommerce_google_analytics');
    function maybe_consent_woocommerce_google_analytics() {
      // Disable WooCommerce Google Analytics displaying tracking code by default
      if ( class_exists( 'WooCommerce' ) ) :
    	  $integrations = WC()->integrations->get_integrations();
    	  if ( isset( $integrations['google_analytics'] ) ) :
    		  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 ( function_exists( 'gdpr_cookie_is_accepted' ) && gdpr_cookie_is_accepted( 'thirdparty' ) ) :
    				/**
    				 * Cookies accepted by user
    				 */
    		    add_action('wp_head', array( $integrations['google_analytics'], 'tracking_code_display'), 999999 );
    		  elseif ( class_exists( 'Moove_GDPR_Addon_Actions' ) ) :
    		  	// GeoIP options
    		  	$gdpr_action_cnt = new Moove_GDPR_Addon_Actions();
    				$loc_data = $gdpr_action_cnt->gdpr_extend_loc_data( array() );
    				if ( isset( $loc_data['display_cookie_banner'] ) && $loc_data['display_cookie_banner'] === 'false' ) :
    					/**
    					 * GDPR Banner not visible because the visitor's country is not restricted, all scripts will be inserted
    					 */
    					add_action('wp_head', array( $integrations['google_analytics'], 'tracking_code_display'), 999999 );
    				endif;
    		  endif;
    		endif;
    	endif;
    }

    Hope this helps.

    Avataracdesigns
    March 03, 2020 @ 10:11 pm

    Thank you!

Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.