disable third party cookies using javascript

巧了我就是萌 提交于 2019-12-13 01:50:00

问题


i am working to implement the new cookie policy compliance as per data protection rules for all companies operating in the EU. According to which user has to be able to refuse/accept all but required cookies when he is using any website. In my client's website i can see the following third party cookies are getting stored.

  • _ga - my-site
  • _gid - my-site
  • __ncuid - .doubleclick.net
  • DSID - .doubleclick.net
  • IDE - .doubleclick.net
  • 1P_JAR - .google.be
  • NID - .google.be
  • CONSENT -.google.be
  • NID - .google.com
  • AID - .google.com
  • CONSENT - .google.com
  • 1P_JAR - .google.com
  • DV - www.google.be
  • OTZ - www.google.com
  • locale - my-site
  • anonymousUserId - my-site
  • SESSID - my-site
  • JSESSIONID - my-site
  • TS01c70fa1 - my-site

in the above mentioned list few cookies are google analytics cookies intiated by google tag manager. I have handled them. but all the cookies with domain 'google' and '.doubleclick.net' i have no control over them? and i am not able to understand how are they getting stored.

can anyone help me, or suggest me if there is anyway to disable these cookies on user consent using javascript? Thanks in advanced.


回答1:


Instead of blocking cookies that are being/were set, it's easier to not load i.e. the GTM script until after the user has consented to the cookie policy.




回答2:


To block third-party cookies, find a JavaScript code that is setting third-party cookies and:

  1. change type attribute from text/javascript to text/plain (if type attribute missing, just add it)
  2. add data-cookiescript attribute and set it to accepted

All JavaScript with such attribute changes will only execute if user agreed with Cookie Policy.

Go here for details: https://cookie-script.com/how-to-block-third-party-cookies.html

Hope this helps !!




回答3:


To disable _ga and _gid cookies do add the following code on your headers.php:

<script>
    if(document.cookie.split('; ').findIndex(x => x.split('=')[0] == 'cookiename') >= 0) 
    {
        // some code
    } else {
        window["ga-disable-UA-7358061-1"] = true;
        window["gid-disable-UA-7358061-1"] = true;
    }
</script>



回答4:


A little bit old but I think you deserve a answer that works:

Step 1: Like thomaux already wrote: Don't load/execute the script. You can provide the code with the trick from vizsatiz without executing but add an id to the html element like GA_SCRIPT.

Step 2: Show the cookie banner.

Step 3: After the user accepted, load the code from the html element with document.getElementById("GA_Script").innerText and execute with eval.

Worked for me.

Alternatively you could write the GA script code into your code and execute it as a callback.



来源:https://stackoverflow.com/questions/50870511/disable-third-party-cookies-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!