编辑(2019 年):以下答案早于 GDPR,可能需要修订。
谷歌分析有一套新的 API 来帮助遵守 cookie 退出。这是文档,这是他们的帮助文档。
关于欧盟 Cookie 法规(在成员国实施)是否要求被动网络分析跟踪需要选择加入机制以确保合规性,存在一些歧义。如果您担心某种方式,请咨询律师。谷歌授权您决定如何进行。
他们会将实施细节留给您,但是,想法是,一旦您确定是否在 Google Analytics 中跟踪用户,如果答案是不跟踪,您可以在 Google 之前将以下属性设置为 true分析运行:
window['ga-disable-UA-XXXXXX-Y'] = true;
其中 UA-XXXXXX-Y 是您在 Google Analytics 中的帐户 ID
正如其他海报所指出的,谷歌分析依赖于 cookie。因此,如果没有 cookie,您将无法进行任何类型的跟踪。如果您确定某人不会被 cookie 用于跟踪,则需要实现以下内容:
if(doNotCookie()){
window['ga-disable-UA-XXXXXX-Y'] = true;
}
选择参加
当您第一次加载 Google Analytics 时,这确实需要一点柔术,因为需要在 Google Analytics 运行之前设置此属性以防止跟踪发生,这意味着,对于“选择跟踪”方法,您d 可能需要实施一种机制,在第一次访问时,如果没有选择加入 cookie(明确允许确定 cookie 偏好的 cookie),Google Analytics 会自动禁用,然后,如果发生选择加入,则重新运行谷歌分析。在随后的综合浏览量中,一切都会顺利进行。
可能看起来像(伪代码):
if( hasOptedOut() || hasNotExpressedCookiePreferenceYet() ){ //functions you've defined elsewhere
window['ga-disable-UA-XXXXXX-Y'] = true;
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_trackPageview']);
function onOptIn(){ //have this run when/if they opt-in.
window['ga-disable-UA-XXXXXX-Y'] = false;
//...snip...
//set a cookie to express that the user has opted-in to tracking, for future pageviews
_gaq.push(['_trackPageview']); // now run the pageview that you 'missed'
}
选择退出
使用这种方法,您将允许用户选择退出跟踪,这意味着您将使用 cookie 来设置ga-disable-UA-XXXXXX-Y'
属性并在将来使用 cookie 来管理它:
if( hasOptedOut() ){ // function you've defined elsewhere
window['ga-disable-UA-XXXXXX-Y'] = true;
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-Y']);
_gaq.push(['_trackPageview']);