• Resolved startechmarketing

    (@startechmarketing)


    Hello
    I am following your guide here https://complianz.io/set-your-own-cookie-based-on-a-custom-consent-category/

    I created a .php called cloudflare-consent.php and uploaded it to the MU Plugins folder
    but when I add the second part “Adding a custom cookie”
    that code breaks the site

    function cmplz_set_rlx() {
    	ob_start();
    	?>
        <script>
            function cmplz_set_rlx_cookie(value){
                let secure = ";secure";
                let date = new Date();
                let name = 'rlx-consent';
                date.setTime(date.getTime() + (complianz.cookie_expiry * 24 * 60 * 60 * 1000));
                let expires = ";expires=" + date.toGMTString();
                console.log("set cookie with string "+name + "=" + value + ";SameSite=Lax" + secure + expires);
                document.cookie = name + "=" + value + ";SameSite=Lax" + secure + expires;
            }
    
            /**
             * Set a complianz rlx cookie and the custom cookie on save preferences
             */
            document.addEventListener('click', e => {
                if (e.target.closest('.cmplz-save-preferences')) {
                    let rlx_enabled = document.querySelector('input.cmplz-functional').checked;
                    if (rlx_enabled) {
                        console.log("save prefs, rlx enabled");
                        cmplz_set_cookie('rlx', 'allow');
                        cmplz_set_rlx_cookie(true);
                    } else {
                        console.log("save prefs, rlx disabled");
                        cmplz_set_rlx_cookie(false);
                        cmplz_set_cookie('rlx', 'deny');
                    }
                }
            });
    
            document.addEventListener("cmplz_fire_categories", function (e) {
                var consentedCategories = e.detail.categories;
                if ( cmplz_in_array( 'marketing', consentedCategories ) ) {
                    cmplz_set_cookie('rlx', 'allow');
                }
                if ( cmplz_in_array( 'rlx', consentedCategories ) ) {
                    cmplz_set_cookie('rlx', 'allow');
                }
            });
    
            document.addEventListener("cmplz_before_cookiebanner", function(e) {
                if ( cmplz_get_cookie('rlx') === 'allow'){
                    console.log("set checkbox rlx to true");
                    document.querySelector('input.cmplz-functional').checked = true;
                    _satellite.setVar("Analyticsconsent","true");
                    _satellite.track("PageView");
                } else {
                    _satellite.setVar("Analyticsconsent","false");
                    //set a default false value, as workaround for rlx issue which checks for false cookie, instead of also checking for not-existing one.
                    cmplz_set_rlx_cookie(false);
                }
            });
    
            /**
             * Handle revoke
             */
            document.addEventListener("cmplz_revoke", function(e) {
                console.log("revoke rlx cookie");
                cmplz_set_cookie('rlx', 'deny');
                cmplz_set_rlx_cookie(false);
            });
        </script>
    	<?php
    	$script = ob_get_clean();
    	$script = str_replace(array('<script>', '</script>'), '', $script);
    	wp_add_inline_script( 'cmplz-cookiebanner', $script );
    }
    add_action( 'wp_enqueue_scripts', 'cmplz_set_rlx',PHP_INT_MAX );
    

    I added the code in a code snippets plugin first and it would not activate because of an error, so then I thought I must add it to the bottom of the mu plugin I created, but that breaks the site.

    Please assist thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor jarnovos

    (@jarnovos)

    Hello @startechmarketing,

    What is the exact result that you are trying to achieve by adding this filter?

    As the filter in the attached article had been created for a specific third-party implementation (which relies on a cookie to be created, after which the third-party loads their tracking functionality), this might not be needed in your case.

    If you describe the result that you would like to achieve, that would allow us to determine the steps to take.

    Kind regards,
    Jarno

    Thread Starter startechmarketing

    (@startechmarketing)

    We are in Germany (EU) we must have Cloudflare and Google Tag Manager in the consent,
    In the wizard you only have one option Google Anayltics or Google Tag Manager, since we use both I have chosen Google Analytics. (therefore the need to also accept Google Tag Manager)

    While waiting for your reponse I tried another Mu Plugin for Tag Manager without the cookie and it also broke the site, so I removed it.

    I was able while waiting for your quick response (thank you), in the wizard to create cookies for both services.

    Please advise how to proceed, thank you.

    Thread Starter startechmarketing

    (@startechmarketing)

    Also a new problem, I open the website https://starpaint.de/ in a new browser and the consent does not come up, even when I clear cookies

    Plugin Contributor jarnovos

    (@jarnovos)

    Hi @startechmarketing,

    You don’t have to use that MU Plugin to block a script/cookie on your website.

    Concerning Analytics/Tag Manager
    I would recommend firing the Analytics tag from your Tag Manager container, and then loading Tag Manager based on consent in Complianz (so that both Analytics and Tag Manager load only after providing consent), by following these steps: https://complianz.io/how-to-set-google-tag-manager-after-consent-and-is-it-needed/

    CloudFlare
    Blocking CloudFlare’s cookies prior to consent should not be necessary as its cookies are classified as “Functional”, as well as that doing so is very likely to result in unexpected behavior.

    Banner not appearing
    I have tested your website and the banner appears correctly on my end. You most-likely have the option “Respect Do Not Track settings in browsers” enabled in the plugin, found under Wizard > General > Security & Consent.

    Meaning that if the plugin detects a Do Not Track signal from the browser (which browsers such as Firefox have enabled by default), the plugin will automatically assume all your cookie preferences to be “denied”, and doesn’t display a banner as a result.

    Disabling that option in Complianz, or disabling the “Send a Do Not Track” option in your browser, will resolve this behavior and cause the banner to appear.

    Kind regards,
    Jarno

    Thread Starter startechmarketing

    (@startechmarketing)

    Thanks I will have a look on Tuesday we are finished working for the weekend.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Cookie Code breaks site – Set your own Cookie based on a (custom) Consent Categ’ is closed to new replies.