简体   繁体   English

将来自Google跟踪代码管理器DataLayer的值保存在JavaScript变量中

[英]Save a value from Google Tag manager DataLayer in a JavaScript variable

Iam using Google Tag manager==GTM as well as Visual Website Optimizer==VWO. Iam使用Google跟踪代码管理器== GTM以及Visual Website Optimizer == VWO。 All the script tags for both application are implemented accordingly. 两个应用程序的所有脚本标签均已相应实现。

In order to be able to track revenue we have to save the transactionTotal of an order into a variable that VWO uses to count the conversion rates on a specific test. 为了能够跟踪收入,我们必须将订单的transactionTotal保存到一个变量中,VWO使用该变量来计算特定测试的转换率。 The VWO clicks and visits are counted correctly but not the conversion. 正确计算了VWO的点击和访问次数,但未正确计算转化次数。

I have created a macro in GTM ({transactionTotal}) based on fetching the data. 我已经基于获取数据在GTM({transactionTotal})中创建了一个宏。

  1. First I get all the meta tags where we have the ecommerce values. 首先,我获得了所有具有电子商务价值的元标记。

    var metaObj = {}, m = document.getElementsByTagName('meta'), i = m.length; var metaObj = {},m = document.getElementsByTagName('meta'),i = m.length; //scope expanded from only .head while (i--) { // looping down will result in the same behaviour as stopping @ 1st metaObj[m[i].name] = m[i].content; //范围仅从.head扩展,而(i--){//向下循环将产生与停止@ 1st metaObj [m [i] .name] = m [i] .content相同的行为。 } }
  2. Then push the meta value in the dataLayer 然后将meta值推送到dataLayer中

    dataLayer.push({'transactionId': metaObj['WT.tx_i']}); dataLayer.push({'transactionId':metaObj ['WT.tx_i']});;

  3. In GTM I created a macro 在GTM中,我创建了一个宏 GTM宏

  4. in the confirmation page: 在确认页面中:

    //should be the value of the order total var _vis_opt_revenue = ({transactionTotal}); //应该是订单总数的值var _vis_opt_revenue =({transactionTotal});

This returns an error and no value is save in this variable. 这将返回错误,并且该变量中没有任何值。

Any ideas or help is much appreciated. 任何想法或帮助将不胜感激。

I figured out I had a syntax error user parenthesis instead of curly brackets 我发现我有一个语法错误的用户括号而不是大括号

so it should be 所以应该

var _vis_opt_revenue = {{transactionTotal}}; 

instead of 代替

var _vis_opt_revenue = ({transactionTotal});

Could you indent your code please ? 您可以缩进代码吗? It will be much easier. 这样会容易得多。

Secondly, you use a macro that is looking for transactionTotal and you push on dataLayer an object named transactionId Therefore, it could not work 其次,您使用一个正在查找transactionTotal的宏,然后将一个名为transactionId的对象推入dataLayer,因此,它无法正常工作

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

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