JS:如何为Google Analytics(分析)dataLayer生成产品数组?

时间:2019-05-11 01:55:26

标签: javascript django google-analytics google-tag-manager

需要Java知识。

当使用Django作为后端并将dataLayer变量发送到{{ order_items }}模板时,我需要一种为 Google Analytics(分析) thank_you_page.html生成产品数组的方法:< / p>

products: [{
            id: 'product_id',
            name: 'Product A',
            price: '24.00',
            quantity: 1,
            coupon: 'FOR20'
          },{
            id: 'another_product_id',
            name: 'MY ADD-ON',
            price: '1.00',
            quantity: 1,
            coupon: 'ADD-ONS OFF'
          }]
  

它将成为要使用的dataLayer的一部分   之后,通过Google Tag捕获Google Analytics(分析)的值   经理。

我将Django用于后端,并将此变量发送至前端:

{{order}}

{{order_items}}

根据Google Analytics Expert上的这篇博客文章,在此数组必须位于另一个“父”数组中之前说过:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'eec.purchase',
  ecommerce: {
    currencyCode: 'EUR',
    purchase: {
      actionField: {
        id: 'ORDER12345',
        affiliation: 'Simo\'s shop',
        revenue: '11.00',
        tax: '1.00',
        shipping: '2.00',
        coupon: 'SUMMER2019'
      },
      products: [{
            id: 'product_id',
            name: 'Product A',
            price: '24.00',
            quantity: 1,
            coupon: 'FOR20'
          },{
        id: 'another_product_id',
        name: 'MY ADD-ON',
        price: '1.00',
        quantity: 1,
        coupon: 'ADD-ONS OFF'
      }]
    }
  }
});

我应该放置一个空的products = [],然后执行for循环将元素推入其中吗?

products = []
{% for item in order_items %}
   products.push({
     id: item.id,
     name: item.name,
     price: item.price,
     quantity: item.quantity,
     coupon: item.coupon
   });
{% endfor %}

更新1:

使用此代码:

{% block data_layer %}
    <script>
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
            event: 'eec.purchase',
            ecommerce: {
                currencyCode: 'PEN',
                purchase: {
                    actionField: {
                        id: {{ order_number }},
                        affiliation: 'Stickers Gallito E-Commerce',
                        revenue: {{ total }},
                        shipping: {{ costo_despacho }},
                        coupon: 'SUMMER2019'
                    }
                },
                products: []
            }
        });
    </script>


    {% for item in order_items %}
        <script>
            window.dataLayer.ecommerce.products.push({
                id: item.order.id,
                name: item.name,
                price: item.price,
                quantity: item.quantity
            });
        </script>
    {% endfor %}



{% endblock %}

出现错误

  

未捕获的TypeError:无法读取未定义的属性“产品”       在(index):72

这是第72行:

 window.dataLayer.ecommerce.products.push({

3 个答案:

答案 0 :(得分:0)

是的,我想这是一个好习惯。我认为order_items是项目数组。

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  event: 'eec.purchase',
  ecommerce: {
    currencyCode: 'EUR',
    purchase: {
      actionField: {
        id: 'ORDER12345',
        affiliation: 'Simo\'s shop',
        revenue: '11.00',
        tax: '1.00',
        shipping: '2.00',
        coupon: 'SUMMER2019'
      }
    },
    products: []
  }
});

然后使用forEach将物品推入产品中。

 order_items.forEach(item => {
   window.dataLayer.ecommerce.purchase.products.push({
     id: item.id,
     name: item.name,
     price: item.price,
     quantity: item.quantity,
     coupon: item.coupon
   });
 });

答案 1 :(得分:0)

您不能将数据直接推入dataLayer的选定键中。您需要将数据推送到dataLayer本身:

template <class T, T*(*Func)()>
class User
{
public:
    void foo()
    {
        auto p = Func();
    }
};
int main()
{
    User<A, &funcA> user;
}

对于您的特定情况,将采购呼叫的所有产品数据放入一个推送中也是一种更好的做法,因为您需要一起处理这些数据,这样GTM便不会知道哪个是最后一个产品,产品阵列准备就绪时。

因此,我的建议是按照增强型电子商务数据的预期来循环产品,并将它们作为对象数组放置到产品中:

MERGE table1 AS s
USING (
SELECT @OId AS oID, @Partno AS partno, @PreviousTotal AS PreviousTotal, 
@YearTotal AS YearTotal) AS source
ON s.oId = source.oId AND s.partno = source.partno
WHEN MATCHED THEN
UPDATE SET  
        UpdatedTotal = (@YearTotal + s.YearTotal) - @PreviousTotal,
        PreviousTotal = source.PreviousTotal,
        ReportDate = GetDate()
WHEN NOT MATCHED THEN
INSERT (...)
VALUES (...)

另外,请注意,这种初始化通常在调用GTM基本代码之前使用:

dataLayer.push({...});

通常在以后的数据推送中指定事件,以使GTM对该特定事件采取行动:

{% block data_layer %}
    <script>
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
            event: 'eec.purchase',
            ecommerce: {
                currencyCode: 'PEN',
                purchase: {
                    actionField: {
                        id: {{ order_number }},
                        affiliation: 'Stickers Gallito E-Commerce',
                        revenue: {{ total }},
                        shipping: {{ costo_despacho }},
                        coupon: 'SUMMER2019'
                    }
                },
                products: [

    {% for item in order_items %}
                  {
                    id: item.order.id,
                    name: item.name,
                    price: item.price,
                    quantity: item.quantity
                  },
    {% endfor %}
                ]
            }
        });
    </script>
{% endblock %}

在初始调用中使用事件可能仍然有效(未经测试),但也可以使用Page View事件进行处理。

答案 2 :(得分:0)

所以问题在于从Python列表(或其他语言的数组)创建产品数组(使用Javascript)。

问题的复杂性是因为我(问题的​​所有者)不了解如何使用模板中的python列表 来生成 javascript数组

答案是,您需要使用序列化程序将python list发送到模板:

def thanks_deposit_payment(request):
    order_number = Order.objects.latest('id').id

    total = Order.objects.latest('id').total

    costo_despacho = Order.objects.latest('id').shipping_cost

    order_items = OrderItem.objects.filter(order=Order.objects.latest('id'))


    order_items = serialize('json', order_items, fields=['id', 'sku', 'product', 'price', 'size', 'quantity'])

    response = render(request, 'thanks_deposit_payment.html', dict(order_number=order_number, total=total,
                                                                   order_items=order_items, costo_despacho=costo_despacho))
    return response

thanks_deposit_payment.html模板中,您必须使用JSON:

像这样:products: JSON.parse('{{ order_items | safe }}')

{% block data_layer %}

    <script>
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
            event: 'eec.purchase',
            ecommerce: {
                currencyCode: 'PEN',
                purchase: {
                    actionField: {
                        id: {{ order_number }},
                        affiliation: 'Stickers Gallito E-Commerce',
                        revenue: {{ total }},
                        shipping: {{ costo_despacho }},
                        coupon: ''
                    },
                    products: JSON.parse('{{ order_items | safe }}')
                },

            }
        });
    </script>

{% endblock %}