基于电子商务数据层对象的Google跟踪代码管理器自定义javascript变量 [英] Google Tag Manager custom javascript variable based on ecommerce datalayer object

本文介绍了基于电子商务数据层对象的Google跟踪代码管理器自定义javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据GTM数据层电子商务对象中包含的数据来计算购物车的价值。我将整个电子商务对象带入一个数据层变量,所以我可以在自定义js变量中使用它,但我无法使递归计算工作。

I'm trying to calculate the value of the shopping cart based on data contianed in the GTM datalayer ecommerce object. I'm bringing the entire ecommerce object into a datalayer variable so I can work with it in the custom js variable, but I'm unable to make the recursive calculation work.

以下是数据层代码示例:

Here's an example of code from the datalayer:

{
  "event": "checkout",
  "ecommerce": {
    "checkout": {
      "products": [
        {
          "name": "coat",
          "id": "14M",
          "quantity": 1,
          "price": "100.00"
        },
        {
          "name": "pants",
          "id": "12L",
          "quantity": 3,
          "price": "50.00"
        }
      ]
    }
  }
}`

我的目标是识别产品阵列中的产品数量然后通过whol产品阵列执行产品价格*产品数量的计算。在这种情况下,它将是这样的:

My goal is to identifies the number of products in the product array and then perform the calculation of products price * product quantity as many times as is required to go through the whol product array. In this case, it would be something like this:

(1 * 100)+(3 * 50)= 250。

(1*100) + (3*50) = 250.

我尝试修改中的代码Simo Ahava的相关博客没有成功。他的博文和我的意图之间的区别是:

I've tried modifying the code from Simo Ahava's related blogpost without success. The differences between his blogpost and my intention are:


  1. 我不需要将购物车价值设置为电子商务自定义指标。我想在自定义js中返回购物车的价值。

  2. 我不希望这涉及添加或删除购物车中的商品。相反,我只是想在触发GTM事件结账时计算价值并计算所包含产品的价值。

在此先感谢。

推荐答案

由于您已经创建了{{ecommerce}}变量,因此获取总金额非常容易根据产品的数量和产品的价格。

Since you have already created {{ecommerce}} variable , then it is very easy to fetch total amount on the basis of product's quantity and product's price.

您必须在自定义的js变量上创建订单小计
然后在该自定义中js box
粘贴代码

You have to create on custom js variable Order Subtotal then in that custom js box paste below code

function(){
  var productList={{ecommerce}}.checkout.products;
  var totalAmount=0;
  for(var i=0;i<productList.length;i++)
  {
    totalAmount+=(productList[i].quantity)*(parseFloat(productList[i].price));
  }
  return totalAmount;
}

我希望这可以帮助您解决问题

I hope this will help you resolve you issue

这篇关于基于电子商务数据层对象的Google跟踪代码管理器自定义javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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