Disable scripts based on user cookie selection

[ad_1]

Hi Daniel,

Thanks for reaching out. There is a filter than can be used to ensure Site Kit doesn’t place a Google Analytics snippet in various circumstances. The filter is below:
add_filter( 'googlesitekit_analytics_tag_blocked', '__return_true' );

For the Google Analytics 4 snippet, if placed via Site Kit, you can use googlesitekit_analytics-4_tag_blocked.

You can apply this to your own custom functions if you wish.

For consent-type blocking specifically, you can use an alternate filter which will still output the tags, but in an initially disabled state.

add_filter( 'googlesitekit_analytics_tag_block_on_consent', '__return_true' );

The above applies to Universal Analytics. You can replace the analytics reference above with analytics-4 (for GA4), adsense for the AdSense module or tagmanager for Google Tag Manager.

Note that if you’re an AMP user the filters to be applied differ slightly. Let me know if so.

There is also a mini API that can be used by cookie consent type plugins.

Let me know if you have any questions with the above.

Great @jamesosborne

I will make some tests and return if any problem detected. Marking as solved for now!

Thank you.

No problem, happy to help! Best of luck.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer