使用if语句关闭html中的一段代码

Turning off a section of code in html with something like an if statement

本文关键字:代码 一段 语句 if html 使用      更新时间:2023-09-26

我有一个活跃的网站,我必须继续在一个活跃的网站上工作,这意味着我正在查看页面很多。

我想知道是否有一种在HTML中使用if语句的方法,我可以关闭谷歌分析的代码,这样就不会弄乱我们的报告?所以我可以简单地改变页面上的值(如TRUE/FALSE),这样我就可以随心所欲地工作,而不会破坏分析。

更好的方法是让Google分析过滤掉你的IP地址。

https://support.google.com/analytics/answer/1034840?hl=en

检查window.location.search查询字符串变量,并在不需要分析时设置该变量。下面显示了当querystring包含hidegoogle=yes

时隐藏的代码
var hide = window.location.search.match(/hidegoogle=(yes|no)/);
if (hide !== null && hide[1] === "yes") {
    console.log('yes');
} else {
    console.log('no');
    // Run analytics here
}

考虑使用Google标签管理器:

http://tagmanager.google.com/

可以管理Google Analytics Tags.