Search code examples
javascriptangulartypescriptfor-loopfacebook-pixel

Facebook pixel purchase body event


I am trying to make an purchase facebookEvent which it takes an array of objects "Products" that contains the id and the quantity of each product on this array, what I am asking about here that I tried allot to looping into the array of products inside the payload of the event but the compiler has complained, could any one help by how to make the contents array responsive with the array of the whole product ?

  //fbp
  (<any>window).fbq("track", "Purchase", {
    value: this.totalPrice,
    currency: "EGP",
    contents: [
      {
        // this should be applicable for every single item on the arrayOfProducts
        id: arrayOfProducts[0].id,
        quantity: arrayOfProducts[0].quantity
      }
    ],
    content_type: "product"
  });

Solution

  • What i mean by my questions is, how to push the whole arrayOfProducts instead of passing only the first item on the array ?

    Use map

    //fbp
      (<any>window).fbq("track", "Purchase", {
        value: this.totalPrice,
        currency: "EGP",
        contents: arrayOfProducts.map(function(item) {
            return {
               id: item.id,
               quantity: item.quantity
            }
        }),
        content_type: "product"
      });