3
votes

Looking for clarification on Google Analytics Enhanced Ecommerce using Google Tag Manager Purchase action.

I've successfully implemented adding a product to the shopping cart using the event addToCart. Here's my question:

When the user purchases the items in their cart (hitting a submit order button) I will use the purchase event. At this time do I need to "re-gather" all of the products that are in their cart?

Is the following correct: Because the dataLayer is not persistent I do not have access to the items I pushed to the dataLayer using the addToCart event. In order to send all of the products along with the purchase event I need to supply the products again as part of the purchase event.

Appreciate any help. Thank you!

2

2 Answers

4
votes

Adding/removing items to the shopping cart is a different action event then the checkout (purchase) event and there is no persistency between them.

So yes, you need to re-gather all the products when firing the purchase event.

see the examples on https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-checkout

3
votes

According to this documentation, https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#measuring-checkout, you would need to store the products from the cart (eg. into an array) so that you can call the addProduct method for each of them. Here is the example shown:

/**
 * Called when the user begins the checkout process.
 * @param {Array} cart An array representing the user's shopping cart.
 */
function checkout(cart) {
  for(var i = 0; i < cart.length; i++) {
    var product = cart[i];
    ga('ec:addProduct', {
      'id': product.id,
      'name': product.name,
      'category': product.category,
      'brand': product.brand,
      'variant':  product.variant,
      'price': product.price,
      'quantity': product.qty
    });
}