在服务器上删除Google Analytics(分析)cookie
当我们指定用户不希望启用它们时,我们一直在尝试删除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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论