如何删除 Google Analytics cookie

问题描述 投票:0回答:1

你好要创建 cookie 横幅, 我知道有免费的解决方案、现成的解决方案等等...

但我想为 YouTube 视频实现一个横幅(我已经找到了)。 但我陷入困境的是谷歌分析。 当我单击按钮显示 cookie 时,我似乎无法显示它们。 然而,当我想把它们摘下来的时候,却又摘不下来。 我从今天早上就开始寻找。 如果有好心人可以帮助我,请。

<?php session_start() ?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Création de Cookie</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.1/js.cookie.min.js"></script>
</head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-Eaaaaaa"></script>
<body>
<script>
     function acceptAllCookies() {
       // Charger le code de suivi Google Analytics
       var gaScript = document.createElement('script');
       gaScript.src = 'https://www.googletagmanager.com/gtag/js?id=G-E';
       document.head.appendChild(gaScript);

       gaScript.onload = function() {
         window.dataLayer = window.dataLayer || [];

         function gtag() {
           dataLayer.push(arguments);
         }
         gtag('js', new Date());

         gtag('config', 'G-Eaaaaaaaa');
       }
     }

     function refuseAllCookies() {
       // Supprimer le cookie de Google Analytics
       Cookies.remove("_ga_Eaaaaaaaa, { path: '/' }); // Ajoutez cette ligne
         Object.keys(Cookies.get()).forEach(function(cookieName) {
           var neededAttributes = {
             // Here you pass the same attributes that were used when the cookie was created
             // and are required when removing the cookie
           };
           Cookies.remove(cookieName, neededAttributes);
         });
       }

</script>
<div id="cookieBanner" style="background-color: #fff; padding: 10px; border: 1px solid #ccc; position: fixed; bottom: 0; left: 0; width: 100%;">
    <p>Nous utilisons des cookies pour améliorer votre expérience. Veuillez choisir vos préférences :</p>
    <button onclick="acceptAllCookies()">Accepter tous les cookies</button>
    <button onclick="refuseAllCookies()">Refuser tous les cookies</button>
</div>
</body>
</html>
javascript google-analytics
1个回答
0
投票

您正在尝试使用相对路径删除谷歌分析cookie,但该cookie是使用绝对路径设置的....这意味着无法找到并删除该cookie

将以下行添加到您的receiveAllCookies()函数中:

Cookies.remove("_ga_Eaaaaaaaa", { path: '/' });
© www.soinside.com 2019 - 2024. All rights reserved.