当我迁移到 AMP 时,Google 跟踪代码管理器停止工作。 每次打开 AMPed 页面时都会出现问题,我可以在浏览器控制台中看到一些错误,例如
第一个错误:
https://www.googletagmanager.com/amp.json?id=MY_GTM_TAG>m.url=MY_HTTP_URL
(403)
第二个错误:
请求的资源上不存在“Access-Control-Allow-Origin” header 。 Origin '' 因此不允许访问。响应的 HTTP 状态代码为 403。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。
在我的类(class)中扩展 WebMvcConfigurerAdapter
我覆盖了方法 addCorsMappings
像这样:
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("*")
.allowCredentials(true);
};
但是还是不行(这个方法是启动时执行的,我查过了)。为什么有任何想法/提示?
编辑 1(2016 年 12 月 22 日):
问:您如何加载标签管理器?您使用的是 AMP 版本的脚本吗? (@吉姆杰弗里斯)
答:是的,在<head>
我包含了以下代码:
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
并在<body>
有:
<amp-analytics config="https://www.googletagmanager.com/amp.json?id=${googleTagId}>m.url=SOURCE_URL" data-credentials="include"></amp-analytics>
最佳答案
我遇到了同样的问题,结果是您不能为此使用旧的 GTM“Web”容器,因此您必须创建一个特定的 AMP 容器。
根据 Google 的说明找到 here :
Create an AMP container
Tag Manager features an AMP container type. Create a new AMP container for your project:
- On the Accounts screen, click More Actions (More) for the account you'd like to use. Select Create Container.
- Name the container. Use a descriptive name, e.g. "example.com - news - AMP".
- Under "Where to Use Container", select AMP.
- Click "Create".
关于spring-mvc - 即使定义了 CORS 映射,Google Tag Manager 403 的每个请求也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267929/