简体   繁体   English

Google dataLayer实施-dataLayer.push-如何将其添加到页面?

[英]Google dataLayer Implementation - dataLayer.push - How to add it to the page?

I have 2 questions related to GTM implementation 我有2个与GTM实施相关的问题

  1. What's the best way to add GTM in the body. 在体内添加GTM的最佳方法是什么。 I'm adding it using SSI file which is placed immediately after the tag. 我正在使用SSI文件添加该文件,该文件位于标签后。

     <!--#include virtual="/local-assets/gtm-noscript.html" --> 

Is it ok to include it like that or is there any better way to add it after the tag? 可以像这样包含它吗,或者有什么更好的方法可以在标签后添加它?

<body>
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-YYYY"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
  1. Secondly, I am implementing dataLayer.push code using Javascript onclick event . 其次,我正在使用Javascript onclick event实现dataLayer.push代码。 Is this a good practice to hard code dataLayer.push on the page like the example below? 像下面的示例一样,对页面上的dataLayer.push进行硬编码是一个好习惯吗?

     <a href="#" name="button1" onclick="dataLayer.push({'event': 'button1-click'});" >Button</a> 

I have to add onclick dataLayer.push events on at least 50 pages and all are static html pages and each page will have unique dataLayer variables. 我必须在至少50个页面上添加onclick dataLayer.push事件,并且所有事件都是静态html页面,并且每个页面将具有唯一的dataLayer变量。

Any suggestions on the best practices and how to best implement it would be much appreciated! 任何有关最佳做法以及如何最佳实施的建议将不胜感激!

I can't answer your first question because I myself am not sure if your method is better or the default is preferred. 我无法回答您的第一个问题,因为我自己不确定您的方法是更好的方法还是默认方法是首选方法。

However I do know that best practices for Javascript listening events is to have the click trigger a function that you place elsewhere instead of inline. 但是,我确实知道,Javascript侦听事件的最佳实践是使单击触发您放置在其他位置而不是内联的功能。 If it's not a complicated process then you should certainly separate it. 如果这不是一个复杂的过程,那么您当然应该将其分开。

There are cases where separating it is not practical. 在某些情况下,将其分离是不切实际的。 For example in my last implementation, I pushed the dataLayer code directly from the addToCart button because the system I was implementing it on pulls data to populate the product list once, then loops through the table rows to generate the listing. 例如,在我的上一个实现中,我直接从addToCart按钮推送了dataLayer代码,因为在其上实现它的系统提取数据以一次填充产品列表,然后循环遍历表行以生成清单。 The problem was that there were hundreds of products. 问题是有数百种产品。 Doing separate db queries would impact performance severely so instead I just relied on the first results set and queued my dataLayer processing code into the original loop. 进行单独的数据库查询会严重影响性能,因此我只是依靠第一个结果集并将dataLayer处理代码排队到原始循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM