Support » Plugin: Cookie Notice for GDPR » Cookie notice and MonsterInsights

  • Resolved joped


    I’m very new to this, so it might just be me not understanding it, but hope you can help.

    On our website we implemented Cookie Notice and MosterInsights. As you can see from the link MosterInsights made it possible to optout of tracking, and I want to do this if the user presses “No” on the cookie notice.

    Can you help me with this?

    It is properly quite obvious, but I have been stuck for too long.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • joped


    Anyone able to help?



    I realize this might be the wrong forum to post the answer since most of it is related to the MonsterInsights plugin, but since the thread already exists this seemed the easiest thing to do. Feel free to delete the post, if you find it irrelevant.

    Since I had trouble finding a solution online I will tell you what I did, to solve this. However I kinda did it in another way, where the cookies will be created only if the user accepts cookies. Pressing “No” or not doing anything the analytics cookies will not exist.

    You need to access the source code for MonsterInsights. In wordpress achieve this by navigating to Plugins -> Editor. Then set the plugin to edit as the MonsterInsights plugin and press select. In “Includes -> Frontend -> Tracking” there is a file called class-tracking-analytics.php open it and find the line starting with (function(i,s,o,g,r,a,m) [around line 200 for me]. All you need to do is to insert this code on the line above the function <?php if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() ) { ?> and this on the line below the function <?php } ?>

    MonsterInsights is compatible now with Cookie Notice using the EU Compliance Addon (available with any of the paid plans).

    It’s rather annoying that something as important as this requires a paid plan from monster insights. I’m afraid for me that means goodbye to monster insights as it is easier to just add the analytics code to the Cookie Notice scripts box. So no, it isn’t really compatible at all.

    The following snippet will make Monsterinsights plugin aware of the choice users made through the Cookie Notice plugin. Just add it to your functions.php file.

    add_action( 'init', 'set_monsterinsight_cookie_according_to_cookie_notice' );
    function set_monsterinsight_cookie_according_to_cookie_notice() {
        if ( function_exists( 'cn_cookies_accepted' ) && function_exists('monsterinsights_get_ua')) {
            if ( cn_cookies_accepted() ) {
                setCookie( 'ga-disable-'.monsterinsights_get_ua(), 'false' );
    	     setCookie( 'ga-disable-'.monsterinsights_get_ua(), 'true' );


    I tried your solution and it works perfectly as long as I accept the Cookie Notice on the homepage of my site. However, if I accept the notice on another page GA doesn’t start tracking unless I navigate back to the homepage. Once I have hit the homepage it will track me to any other page. Any idea?


    Your solution worked, but I wish I could get John Boone’s functions.php solution to work since it would prevent the issues with plugin updates that your solution will experience.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Cookie notice and MonsterInsights’ is closed to new replies.