对于一个新项目,我想使用 Google Analytics 4。
直到今天我都在使用普通的 Google Analytics,并且在我的隐私政策页面上我有一个 Google Opt Out Cookie。
这是通过简单的 JavaScript 代码实现的。
代码如下:
<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXX-Y';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true; …Run Code Online (Sandbox Code Playgroud)