提示页面存在 GTM
检测 gtm.js 脚本或 dataLayer 数组,当页面使用 Tag Manager 时显示绿色徽章。
可复制的代码
try {
var hasScript = !!document.querySelector('script[src*="googletagmanager.com/gtm.js"]');
var hasDL = Array.isArray(window.dataLayer);
if (hasScript || hasDL) {
var b = document.createElement('div');
b.textContent = 'GTM detected' + (hasScript ? ' (gtm.js)' : ' (dataLayer)');
b.style.cssText = 'position:fixed;top:8px;right:8px;z-index:2147483647;'
+ 'background:#34A853;color:#fff;font:700 12px sans-serif;'
+ 'padding:4px 10px;border-radius:6px;pointer-events:none;';
document.body.appendChild(b);
} else {
console.log('No Google Tag Manager detected on this page.');
}
} catch (e) {
console.warn('GTM detection failed:', e);
}
如何使用此示例
- 用上方按钮复制代码。
- 安装 JustZix(2 分钟),在目标页面打开扩展。
- 添加一条匹配该页面的新规则。
- 将代码粘贴到规则的 JavaScript 面板并保存 — 每次访问页面时都会运行。
为此示例评分
暂无评分 — 成为第一个。