如何使用 jQuery 插入 Google Analytics 代码

发布于 2025-01-15 18:53:56 字数 134 浏览 0 评论 0原文

我正在使用一个网站,以前的开发人员已对 30 多个 HTML 页面中的 部分进行了硬编码。也没有页脚。

有没有办法通过 jQuery 将 GTAG 插入到每个页面中?

如有任何反馈,我们将不胜感激!

I'm working with a site where the previous dev has hard-coded the <head> section in 30+ HTML pages. No footer either.

Is there a way I can insert the GTAG into every page via jQuery?

Any feedback is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2025-01-22 18:53:56

理论上,如果有一个 JavaScript 文件链接到所有其他页面,您可以通过附加添加它。请记住,提供的示例尚未通过 GA 测试,而且考虑到任何人都可以查看您的源代码,这也不是一种安全的方法。
我希望它有帮助,祝你好运! :)

$(document).ready(function() {
    console.log("working");
    
    let gtag = `
        <script async src="https://www.googletagmanager.com/gtag/js?id=///"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){
                dataLayer.push(arguments);
            }
            gtag('js', new Date());
            gtag('config', '///');
        </script>
    `;
    console.log(gtag);
    
    $("head").append(gtag);
});

In theory, you could add it through an append if there is a single JavaScript file that links to all other pages. Do keep in mind that the example provided has not been tested through GA and it's also not a safe way to do it considering that your source code will be viewable by any person.
I hope it helps, best of luck! :)

$(document).ready(function() {
    console.log("working");
    
    let gtag = `
        <script async src="https://www.googletagmanager.com/gtag/js?id=///"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){
                dataLayer.push(arguments);
            }
            gtag('js', new Date());
            gtag('config', '///');
        </script>
    `;
    console.log(gtag);
    
    $("head").append(gtag);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文