You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助:如何让Google Analytics退出代码在Wix网站生效

解决Wix网站Google Analytics退出功能实现问题

Hey there, let's work through why your opt-out link isn't working on Wix and fix it step by step.

The core issue here is that Wix blocks inline JavaScript in HTML content (like the javascript:gaOptout() href you tried) for security purposes, plus the gaOptout function might not be accessible in the right scope. Here's how to get it up and running:

Step 1: Make the gaOptout function globally available

First, you need to define the opt-out function in a spot where your entire site can access it.

  1. Head to your Wix dashboard > Settings > Custom Code
  2. Click Add Custom Code
  3. Paste this code (replace UA-XXXXXXXXX-Y with your actual Google Analytics tracking ID):
function gaOptout() {
  // Set a cookie to disable GA tracking site-wide
  document.cookie = 'ga-disable-UA-XXXXXXXXX-Y=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
  window['ga-disable-UA-XXXXXXXXX-Y'] = true;
  // Optional: Notify the user the opt-out was successful
  alert('You have successfully opted out of Google Analytics tracking.');
}
  1. Choose to load this code in the Head section, and apply it to All pages (or just the pages where you’ll place the opt-out link)
  2. Save and publish this change

Step 2: Attach the opt-out function to your text element

Instead of relying on an inline JavaScript link, we’ll add a click event listener directly to your text element via Wix’s page code editor:

  1. Open the Wix Editor for your target page
  2. Select the text element (#text1) you want to use as the opt-out link
  3. Click the Code (</>) icon to open the page code editor
  4. Replace the existing code with this:
$w.onReady(function () {
  // Style the text to look like a clickable link
  $w("#text1").html = "<span style='color: #0066cc; text-decoration: underline; cursor: pointer;'>Click here to opt-out of Google Analytics</span>";
  
  // Trigger the opt-out function when the text is clicked
  $w("#text1").onClick(() => {
    gaOptout();
  });
});

Step 3: Test the functionality

Publish your site, then click the opt-out text. You’ll see the confirmation alert (if you kept it), and you can verify the cookie was set using your browser’s developer tools (navigate to Application > Cookies > Your site domain — look for the ga-disable-UA-XXXXXXXXX-Y cookie with a value of true).

Quick note for GA4 users

If you’re using Google Analytics 4 instead of the classic ga.js, adjust the gaOptout function to match GA4’s consent model:

function gaOptout() {
  gtag('consent', 'update', {
    'analytics_storage': 'denied'
  });
  alert('You have successfully opted out of Google Analytics 4 tracking.');
}

Just ensure you have the GA4 gtag code loaded on your site first.

内容的提问来源于stack exchange,提问作者Artur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:33:44