Google跟踪代码管理器-解析动态数据层变量 [英] Google Tag Manager - Parse Dynamic Data Layer Variable

查看:49
本文介绍了Google跟踪代码管理器-解析动态数据层变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个推送"数据层字符串.我打算用它来跟踪点击事件并在Google Analytics(分析)中设置适当的渠道,如下所示:products.view.19 |查看最新产品| 19

I want to parse a 'pushed' data layer string. I intend to use it to track click events and setup the appropiate funnels in Google Analytics, it looks as follows: products.view.19|view recent product|19

  • 第一部分(products.view.19)是唯一的页面标识符.
  • 第二部分(查看最新产品)是操作.
  • 最后一部分是(19)是动作标识符,这样可以对动作进行分组和比较轻松.

因此,我执行以下操作,首先创建了一个触发器(当链接中包含标签"data-trackclick"时将触发),该触发器将数据值推入变量(对于datalayer为变量).但是,如上所述,现在我想将该变量分成3个新变量.我为此选择了"javascript宏",但以某种方式返回未定义".该宏如下所示:

So I did the following, I first created a trigger (it fires when a link has the tag 'data-trackclick' in it) which pushes the data value to a variable (variable for datalayer). However, now I want to split that variable in to 3 new variables, as described above. I selected 'javascript macro' for this but somehow it returns 'undefined'. The macro looks as follows:

function() {
  var data = {{TrackClickData}};
  var pieces = data.split('|');

  if (pieces[0].length()) {
      return pieces[0];
  } else {
      return data;
  }
}

显然这没有用,因为它只能在初始加载时运行,而不是(如我所想)在请求宏时运行,因此应该以某种方式在点击"上触发它,然后相应地设置变量.

Obviously this didnt work since it would only run on the initial load and not (like I thought) when the macro was requested, so it should somehow be fired on the 'click' and then set the variables accordingly.

这有可能吗?还是真的必须在脚本标签中添加dataLayer.push()?

Is this possible to do? Or do I really have to add the dataLayer.push() in script tags?

推荐答案

几件事:

  • .length()是错误的,数组长度的属性为 .length ,而没有()
  • 如果存在,则件[0]不是数组,则 .length 将返回字符串长度,请参见
  • .length() is wrong, the property for array length is .length without the ()
  • if it exists, pieces[0] is not an array, then .length would return the string length, see How do you check for an empty string in JavaScript? for more standard way of checking for empty strings
  • Is this possible to do? There's virtually nothing you can't do with GTM, since you can write JavaScript code, you can do whathever you code allows you to do, and splitting a string to use parts of it as variables is certainly within the realm of possibilities.

我的建议是首先让您的代码在GTM之外运行(例如,在浏览器控制台中对其进行测试),然后在所有代码正常工作后,将其移植到GTM.

My advise is to make your code work outside GTM first (eg test it in the browser console), then once it's all working, port it to GTM.

这篇关于Google跟踪代码管理器-解析动态数据层变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆