有没有办法在谷歌分析代码周围编写 if 语句,以便它根据 URL 知道我处于 DEV 环境中?这是在index.html页面上。
Web.Config 对此是不可能的。
现在代码:
<script>alert(window.location.href + "test");</script> ---This is to see what URL I am using for my if statement
<script async src="https://www.googletagmanager.com/gtag/js?id=<number>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<number>');
</script>
将脚本标签转换为使用 javascript 动态附加到 DOM,并且您可以按照您想要的任何条件包装代码。不确定你的条件与操作是什么,但这里是一个仅在 URL 包含“test”时执行代码的示例
<script>
(function() {
if (location.href.indexOf('test')>-1) {
var s = document.createElement("script");
s.src = 'https://www.googletagmanager.com/gtag/js?id=<number>';
s.async = true;
document.body.appendChild(s);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<number>');
}
})();
</script>