1

I need to add a cookie consent system on our website due to GDPR laws. Currently everything is set to disable and enable the cookies, but I can't seem to delete third party cookies which are created by Google Analytics or AddThis (some social media tracking), because I can't find them with document.cookie.

So does anybody know what the best option is to either:

  1. Delete the third party cookies
  2. Prevent the scripts from running at start and launch the script after the user consents
  3. Run the scripts from the start, but unload the scripts if the user declines the usage of cookies

But just to be clear, I know how to remove regular cookies, I really need to find a way to remove the third party cookies or prevent them from being loaded (yet make sure they load when the user wants it)

enter image description here

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-556d15415fgddz4536" async="async"></script>

<script>
function enableCookies() {
    console.log('enable cookies');
}

function disableCookies() {
    console.log('disable cookies');
}

window.cookieconsent.initialise({
    onInitialise: function (status) {
        var type = this.options.type;
        var didConsent = this.hasConsented();
        if (type == 'opt-in' && didConsent) {
            enableCookies();
        }
        if (type == 'opt-out' && !didConsent) {
            disableCookies()
        }
    },
    onStatusChange: function (status, chosenBefore) {
        var type = this.options.type;
        var didConsent = this.hasConsented();
        if (type == 'opt-in' && didConsent) {
            enableCookies();
        }
        if (type == 'opt-out' && !didConsent) {
            disableCookies()
        }
    },
    onRevokeChoice: function () {
        var type = this.options.type;
        if (type == 'opt-in') {
            disableCookies()
        }
        if (type == 'opt-out') {
            enableCookies();
        }
    }
    position: "bottom-left",
    type: "opt-out"
});
</script>
Jordec
  • 1,516
  • 2
  • 22
  • 43
  • i think is impossible to delete third party cookies, just append GA script inside `onStatusChange` – Nico Feb 04 '20 at 10:01
  • 1
    Regarding GDPR compliance, the only valid and legal approach is number two. – str Feb 04 '20 at 10:02
  • @str but then how do you load a script after the page has loaded? And what if the user changes his mind and rejects the cookies again? – Jordec Feb 04 '20 at 10:04
  • 2
    You can read [this question about loading JavaScript dynamically](https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file). And note that GDPR is *not only* about cookies. See https://gdpr.eu for more information. – str Feb 04 '20 at 10:08
  • @str I know about the other GDPR info, but my job currently is to add a cookie consenter :) – Jordec Feb 04 '20 at 10:08
  • Hi @Jordec. Did you find a solution to this yet? Thank you – Raluca Cristina Dumitru Dec 14 '20 at 11:58
  • I'm not quite sure since I'm not on the project anymore, but right now I'd say the only way to remove them is to completely disable the plugin that enables these cookies. So an onload that checks for the consent. The tricky part is to remove the cookies once the site and plugin has already loaded. – Jordec Dec 14 '20 at 12:09

0 Answers0