Integrating statistics with cookie notice

I need to unset this cookie and move it behand a if($cookieconcent) condition. Can you help out?

How does hivepress statistics add the tracking code and cookie from Google analytic?

Think I managed to controll it now. Need a bit more testing. If anyone is interessted.

function handleConsent(isConsentGiven) {
    if (isConsentGiven && isConsentGiven !== "null") {
    // sanitize and validate user's consent
    isConsentGiven = sanitizeInput(isConsentGiven);
        if (isConsentGiven === "yes") {
            // store consent in local storage
            localStorage.setItem("consent", "yes");
            // set expiration time for consent
            localStorage.setItem("consent_expires", Date.now() + expirationTime);
            // enable GA tracking
            gtag('config', TRACK ID', {
                'anonymize_ip': true
            });
            gtag('set', 'send_page_view', true);
        } else if(isConsentGiven === "no"){
            // store consent in local storage
            localStorage.setItem("consent", "no");
            // set expiration time for consent
            localStorage.setItem("consent_expires", Date.now() + expirationTime);
            // disable GA tracking
            gtag('set', 'send_page_view', false);
        }
        else {
            // do not store invalid consent
            return;
        }
    }
}

After some testing, I have issues because the hivepress statistic code runs before my concent check, sending the data anyways.

Maybe if I can unset the script and add it in my own javascript, or do you have another solution?

Update*
I think I can fix it by changing the class-statistic file, and just wrap it in a concent check using local storage.

I think its fine, but needs some more testing. However , this obviously needs to be added every statistic update. Is there a more sustainable way I can do this? Childtheme, filter etc?

Hi,

If you check the consent via PHP, a possible way to prevent loading Google Analytics is using the WordPress scripts filter hook, then you can dequeue the script if there’s no content.

Yes, This works great, thank you.

And for the record, i hade to change the code adding the concent to the local storage and change that to cookie, to check it with php

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.