创建GA电子商务“addItem”代码使用JS? [英] Create the GA Ecommerce "addItem" code using JS?

查看:231
本文介绍了创建GA电子商务“addItem”代码使用JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery或JS填充GA电子商务代码。我无法访问代码,所以我无法从实际页面填充它们 - 这是我背后的痛苦,我知道!

到目前为止,我已能够获得订单号码和订单总额。为了让每个产品进入购物篮,我一直试图获取每一行 order-item ,然后获取数据以填充 ecommerce:addItem每件商品。然后我需要将它附加到页面中,以便在订单中为每件商品添加一个 ga('ecommerce:addItem',{});



我想知道是否有其他人有更好的想法来做到这一点。这是我的代码到目前为止,任何改进将不胜感激。我认为一个更好的选择可能是创建 ga('ecommerce:addItem',{}); 然后将它们返回到页面,但我不知道该怎么做这个。

 < script> 
var order_id = document.getElementsByClassName('order_id')[0] .innerText
var order_total = document.getElementsByClassName('order_total')[0] .innerText

ga( 'ecommerce:addTransaction',{
'id':order_id,$ b $'affiliation':'Test Store',
'revenue':order_total,
'currency':'GBP '
});

'('。order_item')。each(function(){
ga('ecommerce:addItem',{
'id':order_id,
' name':$(this).find('。product-name')[0] .innerText,
'sku':$(this).find('。product-sku')[0] .innerText ,
'price':$(this).find('。item_price')[0] .innerText,
'quantity':$(this).find('。iitem_qty')[0] .innerText
});

ga('ecommerce:send');
});


ga('ecommerce:clear');
< / script>


解决方案

将send函数移入每个循环都有效。 / p>

不起作用的原因是由于交易项目未发送至GA


I am trying to populate the GA e-commerce code using jQuery, or JS. I don't have access to the code, so I am unable to populate them from the actual page - which is a pain in the backside, i know!

I have so far been able to get the order number and order total. To get each product in the basket I have been trying to get each row order-item and then grab the data to populate an instance of ecommerce:addItem per item. I then need to append it to the page so i have a ga('ecommerce:addItem', { }); per item in the order.

I was wondering if anyone else has a better idea to do this. Here is my code so far, any improvements would be much appreciated. I think a better option maybe to create the ga('ecommerce:addItem', { }); and then return them to the page, but i am not sure how to do this.

<script>
    var order_id = document.getElementsByClassName('order_id')[0].innerText
    var order_total = document.getElementsByClassName('order_total')[0].innerText

    ga('ecommerce:addTransaction', {
      'id': order_id,
      'affiliation': 'Test Store',
      'revenue': order_total,
      'currency': 'GBP'
    });

    $('.order_item').each(function(){
      ga('ecommerce:addItem', {
        'id': order_id,
        'name': $(this).find('.product-name')[0].innerText,
        'sku': $(this).find('.product-sku')[0].innerText,
        'price': $(this).find('.item_price')[0].innerText,
        'quantity': $(this).find('.iitem_qty')[0].innerText
      });

      ga('ecommerce:send');
    });


    ga('ecommerce:clear');
  </script>

解决方案

Moving the send function into the each loop worked.

The reason it didn't work was due to the transaction items not being sent to GA

这篇关于创建GA电子商务“addItem”代码使用JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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