Support » Plugin: GDPR Cookie Compliance » Google Tracking Disabled on Load and then Enabled after GDPR Consent

  • Resolved acdesigns

    (@acdesigns)


    Hi Support,

    I posted a question about disabling GA tracking by default when using WooCommerce Google Analytics Integration plugin and then re-enabling after receiving user consent.

    I did manage to cobble together a solution based on another member’s suggestion. But this solution only assumes that the user has clicked ‘Accept’ to close the banner. I noticed that your plugin has some hooks which may give a better outcome.

    Do you have any suggestions on how to improve the function below so that is works only when someone has given explicit consent rather than the default action of closing the banner?

    Thanks!

    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 );
    }

    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Moove Agency

    (@mooveagency)

    Hi, @acdesigns,

    Thanks for using our plugins.

    Please find the improved code snippet below:

    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;
    }

    We have built-in PHP cookie checker, the code snippet above checks if the third party cookies are accepted by your visitor, and if so, the GA script will be inserted to your site.

    Hope this helps.

    acdesigns

    (@acdesigns)

    @mooveagency – That is awesome. Thank you!

    acdesigns

    (@acdesigns)

    @mooveagency – I have a question 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.

    Plugin Author Moove Agency

    (@mooveagency)

    Hi @acdesigns

    We can’t answer your question regarding premium feature on this forum due to WordPress.org rules.

    However we’ve got a dedicated forum where you can ask any questions here:
    https://support.mooveagency.com/forum/gdpr-cookie-compliance/

    We’re happy to help you over there.

    Thanks

    acdesigns

    (@acdesigns)

    Will do. Thanks.

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