feat: 页面新增谷歌分析

This commit is contained in:
spylecym 2024-08-09 16:41:27 +08:00 committed by gwok
parent 2926472f7d
commit b56ba93634

View File

@ -200,5 +200,23 @@ const openInNewTab = (url)=> {
const win = window.open(url, '_blank'); const win = window.open(url, '_blank');
win.focus(); win.focus();
} }
// 引入谷歌分析代码
let scriptElement = document.createElement('script');
scriptElement.async = true;
scriptElement.src = 'https://www.googletagmanager.com/gtag/js?id=AW-16660800396"';
// 在 script 元素加载完成后执行的回调函数
scriptElement.onload = function() {
console.log('The gtag.js script has been loaded successfully.');
let customScript = document.createElement('script');
customScript.textContent = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-16660800396');`
document.head.appendChild(customScript);
};
// 将 script 元素插入到文档的 head 元素中
document.head.appendChild(scriptElement);