代码之家  ›  专栏  ›  技术社区  ›  nnikolay

Google标签管理器-自定义跟踪脚本的最佳实践

  •  -1
  • nnikolay  · 技术社区  · 6 年前

    我有一个脚本,应该显示在每个页面上,并在本地存储中写入一个散列。

    <script>
        (function () {
            // write something in the local storage
        })();
    </script>
    

    对于这个标签,我选择“页面视图”作为触发器,因为它应该显示在每个页面上。

    第二个脚本是跟踪某个表单之后的感谢页面上的转换。

    <script>
    
        (function () {
    
            var data = {};
            data.conversionId = 'XXXXXX';
            conversion(data);
    
        })();
    
        function conversion(data) {
            var iframe = document.createElement('iframe');
            iframe.frameBorder = 0;
            iframe.width = 1;
            iframe.height = 1;
            iframe.name = "stlconv";
            iframe.scrolling = "no";
            iframe.allowtransparency = "true";
            iframe.vspace = "0";
            iframe.hspace = "0";
            iframe.marginwidth = "0";
            iframe.marginheight = "0";
            iframe.src = url;
            document.body.appendChild(iframe);
        }
    
    </script>
    

    你在这里选择什么作为触发器?如何以最佳方式管理第二个脚本?

    1. 页面的所有者应该如何设置“conversionId”,以便我可以在脚本中获取值?
    2. 如何以最佳方式生成触发器,因此此脚本仅在send中的表单之后触发?
    1 回复  |  直到 6 年前
        1
  •  1
  •   Matus    6 年前
    1. 这个问题太宽泛了,因为没有通用的“最佳方法”在上下文中将参数从页面传递到GTM的自定义HTML标记。你应该想出一个适合你的解决方案。这里有几个选择:

      • 使用自定义的HTML javascript直接读取conversionid,例如使用全局JS变量、cookies或本地存储
      • 使用dataLayer将conversionid传递给GTM,并在自定义HTML标记中使用dataLayer变量(可以使用双花括号引用dataLayer变量,例如{{example})
    2. 您可以使用GTM的内置“表单提交”触发器来检查验证。有很多关于如何设置它的教程,所以如果你对它有问题,可以扩展你的问题或打开一个新的问题。我可以推荐以下教程: form-trigger-tutorial