最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Analytics tracking code giving CORS error - Stack Overflow

programmeradmin4浏览0评论

I'm using (what looks to me like) new Google Analytics tracking code that I got from my account yesterday. It makes use of some "Global site tag". I've placed it at the top of my , as GA instructs, but I'm getting a cross origin error when loading my page, so GA isn't tracking my site correctly.

This is the exact message:

Access to Script at '={my-id}' from origin '{my website}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value '' that is not equal to the supplied origin.

The script that is causing this error is included like this

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="//www.googletagmanager/gtag/js?id={my-id}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '{my-id}');
</script>

That code above is exactly as it comes from Google Analytics, so I'm not sure why it isn't working..

Is anyone able to help?

I'm using (what looks to me like) new Google Analytics tracking code that I got from my account yesterday. It makes use of some "Global site tag". I've placed it at the top of my , as GA instructs, but I'm getting a cross origin error when loading my page, so GA isn't tracking my site correctly.

This is the exact message:

Access to Script at 'https://www.googletagmanager.com/gtag/js?id={my-id}' from origin '{my website}' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://www.googletagmanager.com' that is not equal to the supplied origin.

The script that is causing this error is included like this

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="//www.googletagmanager.com/gtag/js?id={my-id}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '{my-id}');
</script>

That code above is exactly as it comes from Google Analytics, so I'm not sure why it isn't working..

Is anyone able to help?

Share Improve this question edited Jun 22, 2019 at 9:46 sideshowbarker 88.1k29 gold badges214 silver badges211 bronze badges asked Oct 19, 2017 at 22:44 DennoDenno 2,1683 gold badges20 silver badges29 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 9

2020-04-24 update Google has apparently fixed this, so the error described in the question should no longer occur, per https://support.google.com/tagmanager/thread/15985818?&msgid=37309069 and as noted in the comments to this answer,


2018-01-18 answer

As far as this error message:

Access to Script at 'googletagmanager.com/gtag/js?id={my-id}' from origin '{my website}' has been blocked by CORS policy

…the only case when you should be seeing that error message is if your script element has a crossorigin attribute; e.g., crossorigin="anonymous".

If you’re not adding that crossorigin="anonymous" attribute in any of your own code, you may be using some CDN system that’s injecting it.

I've run into a similar error myself. If you are new to Google Tag Manager maybe it's because you haven't published the tag manager yet. You can check if this is your case by visiting:

https://tagmanager.google.com

And checking whether the green pane says "Container Not Published"

If this is is your case, then the GTM will not load since it's kind of in "draft" mode and not ready to use.

As soon as you have published your first version, it will start working normally.

If you make any changes to your GTM, remember to publish a new version to see the results in your website.

So far (11/6/2020) the issue does not seem to have been resolved by the GTM dev team to the AMP scripts also... https://support.google.com/tagmanager/thread/15985818?hl=en&msgid=37309069

发布评论

评论列表(0)

  1. 暂无评论