我有多个环境,比如开发,预生产,生产等基于我需要访问库的环境 只有一个环境 因为我需要在app load
的基础上更改主机来源的id值<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
使用此lib,函数gtag()将调用
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
如果我在index.html中提到了3个脚本库,那么函数gtag()将调用哪个lib数据
答案 0 :(得分:3)
我有多个环境,比如开发,预生产,生产,所以我需要根据环境放置带有脚本CDN链接的差异ID,
所以不要隐藏lib,而是使用获取window.location.host;
var host = window.location.host;
var url;
if (host.includes('dev-something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else if (host.includes('preprod-something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else if (host.includes('something.com')) {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
} else {
url = 'https://www.googletagmanager.com/gtag/js?id=xxxxxxxxxxx';
}
var head = document.getElementsByTagName('head')[0];
var theScript = document.createElement('script');
theScript.type = 'text/javascript';
theScript.src = url;
head.appendChild(theScript);
所以它在加载我的索引文件时动态创建带有所需ID的CDN链接