在服务器上删除Google Analytics(分析)cookie

发布于 2025-01-25 10:18:45 字数 1443 浏览 3 评论 0原文

当我们指定用户不希望启用它们时,我们一直在尝试删除GTM创建的cookie。

我们加载GTM是标头的第一件事,然后是JS cookies库,最后是我们以下代码。

// Loaded page
window.dataLayer = window.dataLayer || [];
function gtag(){
    dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'XX-XXXXXXXX-X');

if (cookiesEnabled) { // a variable that shows if they are enabled
  // Cookies accepted
  window['ga-disable-XX-XXXXXXXX-X'] = false
} else {
  window['ga-disable-XX-XXXXXXXX-X'] = true
}

// Function to set the cookies
function setCookies (accepted) {
if (!accepted) {
  Cookies.set('_ga', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.set('_gid', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.set('_gat_gtag_XX_XXXXXXXX_X', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  
  // This was also tested
  Cookies.remove('_ga', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.remove('_gid', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.remove('_gat_gtag_XX_XXXXXXXX_X', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
}
}

我们已经通过设置和删除了这两种方式尝试了这一点。我们本地计算机上运行的代码完美工作,毫无问题地删除了cookie,但是当我们将完全相同的代码传递给测试服务器时,这些cookie将不会被删除。

有些东西也许我们缺少了?我们找不到什么。

We have been trying to remove the Cookies that GTM creates when we specify that the user don't want them enabled.

We load GTM the first thing of the header, then the JS Cookies library and finally our following code.

// Loaded page
window.dataLayer = window.dataLayer || [];
function gtag(){
    dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'XX-XXXXXXXX-X');

if (cookiesEnabled) { // a variable that shows if they are enabled
  // Cookies accepted
  window['ga-disable-XX-XXXXXXXX-X'] = false
} else {
  window['ga-disable-XX-XXXXXXXX-X'] = true
}

// Function to set the cookies
function setCookies (accepted) {
if (!accepted) {
  Cookies.set('_ga', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.set('_gid', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.set('_gat_gtag_XX_XXXXXXXX_X', undefined, {
    expires: -1,
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  
  // This was also tested
  Cookies.remove('_ga', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.remove('_gid', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
  Cookies.remove('_gat_gtag_XX_XXXXXXXX_X', {
    path: '', // Also tested with /
    domain: `.${document.domain}`
  })
}
}

We have tried this on both ways, with set and remove. The code running on our local machine works perfectly, the cookies are being removed without any problem, but then, when we pass the exact same code to our test server, those cookies will not be removed.

There is something that maybe we are missing? We were unable to find what.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文