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

添加Google Adsense后网站出现CORS错误,寻求解决方案

解决Google Analytics/AdSense相关的CORS错误

Hey there, let's work through this CORS error you're facing. First, let's clarify what's going on here: that https://www.google-analytics.com/collect endpoint is not designed to be read by front-end code—Google built it to accept data via unread requests (like image beacons or sendBeacon calls), so they intentionally don't add the Access-Control-Allow-Origin header. The error pops up only if something is trying to read the response from that request, which shouldn't happen with official Google scripts.

Here are the steps to fix or debug this:

1. Double-check your Analytics/AdSense code implementation

Google's official gtag.js (used for both Analytics and AdSense) handles tracking requests automatically using methods that don't trigger CORS checks. If you're seeing this error, it's likely either:

  • You accidentally added custom code that uses fetch or XMLHttpRequest to call the /collect endpoint directly (don't do that—let Google's scripts handle it)
  • You have conflicting versions of tracking code (e.g., mixing old analytics.js with new gtag.js)

Make sure your code matches exactly what Google provided. For example, the standard gtag setup looks like this:

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

// Replace with your GA/AdSense IDs
gtag('config', 'GA_TRACKING_ID');
gtag('config', 'ADSENSE_PUBLISHER_ID');

2. Rule out browser extensions or proxy interference

A lot of the time, this error is caused by third-party tools modifying requests:

  • Try opening your site in incognito/private mode (which disables most extensions) and check if the error goes away. Ad blockers, privacy tools like uBlock Origin or Privacy Badger often tweak request headers or redirect GA calls, triggering CORS issues.
  • If you're using a CDN or proxy service, check if any security or optimization rules are altering Google's tracking requests. Look for settings that might rewrite headers or block certain request types.

3. Verify AdSense-Analytics integration

If you linked AdSense to Analytics through the AdSense dashboard, make sure:

  • You didn't manually duplicate Analytics code on your site (the integration should handle loading the necessary scripts)
  • The linked GA account is correct, and you're using the latest code snippet from either platform

4. Check if the error actually breaks functionality

Here's a key point: if this error is coming from Google's official scripts, it might be a false alarm. The /collect request only needs to send data to Google—it doesn't need to return anything your site uses. Head over to your Google Analytics real-time report and test a visit to your site. If you see your visit show up, the tracking is working fine, and you can safely ignore this browser console error.


内容的提问来源于stack exchange,提问作者Chanu De Silva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:12:23