简体   繁体   English

页面行动前的GTM触发事件如何?

[英]GTM fire event before page act, how?

I have a simple page, which in URL https://www.myshop.com/user/quickact , and the html looks like : 我有一个简单的页面,该页面的URL为https://www.myshop.com/user/quickact ,而html如下所示:

<input id="pid" />Keyin Product ID
<input id="count" />Keyin product count
<a id="addList" href="javascript:void(0);" class="buttonAction">Add to List</a>
<!--some external js file handle click event for this link-->

When user clicked hyper text [Add to List], my page will : 当用户单击超文本[添加到列表]时,我的页面将:

  1. Get pid/count value, put into a javascript array. 获取pid / count值,放入javascript数组中。
  2. Reset pid with '', reset count with 1'. 用''重置pid,用1'重置计数。

Things I'm now doing in GTM are : 我现在在GTM中所做的事情是:

  1. When PageURL contains "user/quickact" 当PageURL包含“用户/快捷方式”时
  2. And user clicked link with ID "addList" 用户单击了ID为“ addList”的链接
  3. Get pid/count value, and send them into GA via GTM tag. 获取pid / count值,然后通过GTM标签将其发送到GA。

Here's how I read them in GTM's custom variable 这是我在GTM的自定义变量中阅读它们的方式

function()
{
  var pid=document.getElementById('pid').value;
  var count=document.getElementById('count').value;
  return pid+','+count;
}

My problem is, GA event did fired, but the value...my custom variable always return ',1'. 我的问题是,GA事件确实触发了,但是值...我的自定义变量始终返回“,1”。

It means my GTM tag fired after page's action , so it can only read reset value, not the actual value user key-in. 这意味着我的GTM标签在页面操作后触发 ,因此它只能读取重置值,而不能读取用户键入的实际值。

Can any one gives suggestion to solve this ? 有人可以提出解决建议吗?

I'm not sure if there's a solution to control the order of execution of multiple events, but one workaround would be to provide the required data for GTM with the same functionality, that you are using to get the values and to reset them. 我不确定是否有解决方案来控制多个事件的执行顺序,但是一种解决方法是为GTM提供具有相同功能的必需数据,您将使用这些数据来获取值并重置它们。 This assumes, that you have control over this code. 假设您可以控制此代码。

You could do something like this: 你可以这样做:

function yourFunctionForButtonClick() {

  //Your code to get pid/count value, put into a javascript array.

  //new code to provide data to GTM
  dataLayer.push({
    event: 'newPidProvided',
    pidcountvalue: pid + ',' + count
  }); 

  //Your code to reset pid with '', reset count with 1'.

}

In this case, you need to set up your GTM trigger to listen to newPidProvided event instead of clicks, while still checking for "user/quickact" to be present in the URL. 在这种情况下,您需要设置GTM触发器以侦听newPidProvided事件(而不是单击),同时仍要检查URL中是否存在“用户/快捷方式”。 (Which could also be done in the function, if no other tag or trigger uses pid+count in GTM on other pages.) You'll also have to use a dataLayer variable instead of custom JavaScript type. (如果没有其他标记或触发器在其他页面的GTM中没有使用pid + count,则也可以在函数中完成此操作。)您还必须使用dataLayer变量而不是自定义JavaScript类型。

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

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