Ryan D
Ryan D

Reputation: 1

"This language feature is only supported for ECMASCRIPT6 mode or better: arrow function."

I have a code that I'm having an issue uploading. I'm trying to submit this code in GTM although it says "=>" is not accepted via the title of this thread. Could I please receive assistance on making these normal functions? The error appears in this section "var items = products.map".

Any and all assistance would be GREATLY appreciated.

<script>
 window.addEventListener("message", receiveMessage, false);

  function receiveMessage(event) {
    var payload = event.data && event.data.payload;

    if (!payload || event.data.messageType !== 'analyticsEvent') return;

    if (
      payload.name === "checkout"
    ) {
      var customerEmail = 
        payload.properties && payload.properties.customerEmail;
      var subtotal = 
        payload.properties && payload.properties.estimatedTotal;
      var cartId =
        payload.properties && payload.properties.cartId;
      var products =
        payload.properties && payload.properties.products;

      // do something with payload          
      console.log(subtotal, cartId, products);
      
      // if you're using Enhanced Ecommerce in Google Analytics, 
      // you can use the following code:
      var items = products.map(({ product_id, name, brand, category, kind, unit_price, count, special_id, special_title }) => ({id: product_id, name: name, brand: brand, category: kind, variant: category, quantity: count, price: unit_price}))

      gtag('event', 'purchase', {
        "transaction_id": cartId,
        "affiliation": "store",
        "value": subtotal,
        "currency": "USD",
        "items": items
      });
    }
    
    if (
      payload.name === 'menuLoad' 
    ) {
       var customerEmail = 
         payload.properties && payload.properties.customerEmail;
         
       // do something with the customerEmail if user is authenticated
       console.log(customerEmail);
    }

    if (
      payload.name === 'productView' 
    ) {
       var productId = 
         payload.properties && payload.properties.productId;
       var productKind = 
         payload.properties && payload.properties.productKind;
       var product = 
         payload.properties && payload.properties.product;
         
       // do something with productId, productKind, product
       console.log(productId, productKind, product);
    }

    if (
      payload.name === 'cartItemAdd' 
    ) {
       var productId = 
         payload.properties && payload.properties.productId;
       var product = 
         payload.properties && payload.properties.product;

       console.log(productId); // do something with the productId 
    }

    if (
      payload.name === 'cartItemRemoval' 
    ) {
       var productId = 
         payload.properties && payload.properties.productId;
         
       console.log(productId); // do something with the productId
    }
  }
</script>

Upvotes: 0

Views: 196

Answers (1)

Ben Stephens
Ben Stephens

Reputation: 3371

var items = products.map(({ product_id, name, brand, category, kind, unit_price, count, special_id, special_title }) => ({id: product_id, name: name, brand: brand, category: kind, variant: category, quantity: count, price: unit_price}))

would probably become something like:

var items = products.map(function(product) {
    return {
        id: product.product_id,
        name: product.name,
        brand: product.brand,
        category: product.kind,
        variant: product.category,
        quantity: product.count,
        price: product.unit_price
    };
});

as the ES6 code uses object destructuring and an implicit(?) return value.

Upvotes: 1

Related Questions