Skip to content

Automatically parse ecommerce parameter from webhook #84

@juansebas0712

Description

@juansebas0712

Hi guys, could you please add support to parse ecommerce data from incoming webhook (shopify and woocommerce)?

let valueFromItems = 0;

I changed and testes this tweak and it works properly:
Added a new variable called "items" that picks either the items or ecommerce.items from event data:
let items = eventData.items || eventData.ecommerce.items;
then changed the reference within the addEcommerceData function.

function addEcommerceData(eventData, mappedData) {
let currencyFromItems = '';
let valueFromItems = 0;
let items = eventData.items || eventData.ecommerce.items;

if ( items && items[0]) {
mappedData.custom_data.contents = [];
mappedData.custom_data.content_type =
eventData['x-fb-cd-content_type'] || eventData.content_type || 'product';
currencyFromItems = items[0].currency;

if (!items[1]) {
  if (items[0].item_name)
    mappedData.custom_data.content_name = items[0].item_name;
  if (items[0].item_category)
    mappedData.custom_data.content_category =
      items[0].item_category;

  if (items[0].price) {
    mappedData.custom_data.value = items[0].quantity
      ? items[0].quantity * items[0].price
      : items[0].price;
  }
}

const itemIdKey = data.itemIdKey ? data.itemIdKey : 'item_id';
items.forEach((d, i) => {
  let content = {};
  if (d[itemIdKey]) content.id = d[itemIdKey];
  if (d.item_name) content.title = d.item_name;
  if (d.item_brand) content.brand = d.item_brand;
  if (d.quantity) content.quantity = d.quantity;
  if (d.item_category) content.category = d.item_category;

  if (d.price) {
    content.item_price = makeNumber(d.price);
    valueFromItems += d.quantity
      ? d.quantity * content.item_price
      : content.item_price;
  }

  mappedData.custom_data.contents.push(content);
});

}

if (eventData['x-ga-mp1-ev'])
mappedData.custom_data.value = eventData['x-ga-mp1-ev'];
else if (eventData['x-ga-mp1-tr'])
mappedData.custom_data.value = eventData['x-ga-mp1-tr'];
else if (eventData.value) mappedData.custom_data.value = eventData.value;

if (eventData.currency) mappedData.custom_data.currency = eventData.currency;
else if (currencyFromItems)
mappedData.custom_data.currency = currencyFromItems;

if (eventData.search_term)
mappedData.custom_data.search_string = eventData.search_term;

if (eventData.transaction_id)
mappedData.custom_data.order_id = eventData.transaction_id;

if (mappedData.event_name === 'Purchase') {
if (!mappedData.custom_data.currency)
mappedData.custom_data.currency = 'USD';
if (!mappedData.custom_data.value)
mappedData.custom_data.value = valueFromItems ? valueFromItems : 0;
}

return mappedData;
}

Thanks for this awesome resource.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions