function formatEvents() {
  var events = [];
  var catalogTypes = {"a":"Article", "b":"Blog", "p":"Product"}
  for (i = 0; i < array.users.length; i++) {
    for (j = 0; j < array.users[i].sessions.length; j++) {
      var timeStamp = array.users[i].sessions[j].time;
      for (k = array.users[i].sessions[j].events.length -1; k >= 0; k--) {
        var itemId = array.users[i].sessions[j].events[k].itemId
        var item = array["items"].data.filter(data => data._id == itemId)[0];
        var catalogType = catalogTypes[array.users[i].sessions[j].events[k].itemType];
        var page = (array.users[i].sessions[j].events[k].page === undefined) ? '' : array.users[i].sessions[j].events[k].page;
        var event = {
          "action": array.users[i].sessions[j].events[k].userAction,
          "flags": {
            "noCampaigns": true,
            "pageView": true
          },
          "source": {
            "channel": "Demo Builder",
            "local": "enGB",
            "time": timeStamp,
            "top":array.users[i].sessions[j].events[k].viewTime
          },
          "user": array.users[i].user
        }
        // Add in custom options
        if(event.action == 'Purchase' || event.action == 'Update Cart' || event.action == 'View Item'){
          event.itemAction = array.users[i].sessions[j].events[k].userAction;
          event.catalog = {};
          event.catalog[catalogType] = {"_id": itemId}
        }
        // Add in Page options
        if(page != ''){
          event.page = page;
        }
        // Add in Order node if required
        if(array.users[i].sessions[j].events[k].userAction=='Purchase'){
          event.order = {}
          event.order[catalogType] = {
            "orderId": createID(),
            "totalValue": item.price,
            "currency": "GBP",
            "lineItems": [{"quantity": 1, "_id": itemId, "price":item.price}]
          }
        }
        // Add in Cart node if required
        if(array.users[i].sessions[j].events[k].userAction=='Update Cart'){
          event.cart = {"singleLine": {}};
          event.cart.singleLine[catalogType] = {
            "quantity": 1, 
            "_id": itemId,
            "price":item.price
          }
        }
        events.push(event);
        timeStamp = timeStamp + array.users[i].sessions[j].events[k].viewTime;
        if(k == 0){
          var event = {
            "action": array.users[i].sessions[j].events[k].userAction,
            "flags": {
              "noCampaigns": true,
              "pageView": true
            },
            "source": {
              "channel": "Demo Builder",
              "local": "enGB",
              "time": timeStamp,
              "top":0
            },
            "user": array.users[i].user
          }
          event.itemAction = 'View Item';
          event.catalog = {};
          event.catalog[catalogType] = {"_id": itemId}
          events.push(event);
        }
      }
    }
  }
  array.events = events;
}
// Function to format events in to the correct evergage format
Function to format events in to the correct evergage format
Function to format events in to the correct evergage format
function formatEvents() {
  var events = [];
  var catalogTypes = {"a":"Article", "b":"Blog", "p":"Product"}
  for (i = 0; i < array.users.length; i++) {
    for (j = 0; j < array.users[i].sessions.length; j++) {
      var timeStamp = array.users[i].sessions[j].time;
      for (k = array.users[i].sessions[j].events.length -1; k >= 0; k--) {
        var itemId = array.users[i].sessions[j].events[k].itemId
        var item = array["items"].data.filter(data => data._id == itemId)[0];
        var catalogType = catalogTypes[array.users[i].sessions[j].events[k].itemType];
        var page = (array.users[i].sessions[j].events[k].page === undefined) ? '' : array.users[i].sessions[j].events[k].page;
        var event = {
          "action": array.users[i].sessions[j].events[k].userAction,
          "flags": {
            "noCampaigns": true,
            "pageView": true
          },
          "source": {
            "channel": "Demo Builder",
            "local": "enGB",
            "time": timeStamp,
            "top":array.users[i].sessions[j].events[k].viewTime
          },
          "user": array.users[i].user
        }
        if(event.action == 'Purchase' || event.action == 'Update Cart' || event.action == 'View Item'){
          event.itemAction = array.users[i].sessions[j].events[k].userAction;
          event.catalog = {};
          event.catalog[catalogType] = {"_id": itemId}
        }
        if(page != ''){
          event.page = page;
        }
        if(array.users[i].sessions[j].events[k].userAction=='Purchase'){
          event.order = {}
          event.order[catalogType] = {
            "orderId": createID(),
            "totalValue": item.price,
            "currency": "GBP",
            "lineItems": [{"quantity": 1, "_id": itemId, "price":item.price}]
          }
        }
        if(array.users[i].sessions[j].events[k].userAction=='Update Cart'){
          event.cart = {"singleLine": {}};
          event.cart.singleLine[catalogType] = {
            "quantity": 1, 
            "_id": itemId,
            "price":item.price
          }
        }
        events.push(event);
        timeStamp = timeStamp + array.users[i].sessions[j].events[k].viewTime;
        if(k == 0){
          var event = {
            "action": array.users[i].sessions[j].events[k].userAction,
            "flags": {
              "noCampaigns": true,
              "pageView": true
            },
            "source": {
              "channel": "Demo Builder",
              "local": "enGB",
              "time": timeStamp,
              "top":0
            },
            "user": array.users[i].user
          }
          event.itemAction = 'View Item';
          event.catalog = {};
          event.catalog[catalogType] = {"_id": itemId}
          events.push(event);
        }
      }
    }
  }
  array.events = events;
}
function formatEvents ( ) { var events = [ ] ; var catalogTypes = { "a" : "Article" , "b" : "Blog" , "p" : "Product" } for ( i = 0 ; i < array . users . length ; i ++ ) { for ( j = 0 ; j < array . users [ i ] . sessions . length ; j ++ ) { var timeStamp = array . users [ i ] . sessions [ j ] . time ; for ( k = array . users [ i ] . sessions [ j ] . events . length - 1 ; k >= 0 ; k -- ) { var itemId = array . users [ i ] . sessions [ j ] . events [ k ] . itemId var item = array [ "items" ] . data . filter ( data => data . _id == itemId ) [ 0 ] ; var catalogType = catalogTypes [ array . users [ i ] . sessions [ j ] . events [ k ] . itemType ] ; var page = ( array . users [ i ] . sessions [ j ] . events [ k ] . page === undefined ) ? '' : array . users [ i ] . sessions [ j ] . events [ k ] . page ; var event = { "action" : array . users [ i ] . sessions [ j ] . events [ k ] . userAction , "flags" : { "noCampaigns" : true , "pageView" : true } , "source" : { "channel" : "Demo Builder" , "local" : "enGB" , "time" : timeStamp , "top" : array . users [ i ] . sessions [ j ] . events [ k ] . viewTime } , "user" : array . users [ i ] . user } if ( event . action == 'Purchase' || event . action == 'Update Cart' || event . action == 'View Item' ) { event . itemAction = array . users [ i ] . sessions [ j ] . events [ k ] . userAction ; event . catalog = { } ; event . catalog [ catalogType ] = { "_id" : itemId } } if ( page != '' ) { event . page = page ; } if ( array . users [ i ] . sessions [ j ] . events [ k ] . userAction == 'Purchase' ) { event . order = { } event . order [ catalogType ] = { "orderId" : createID ( ) , "totalValue" : item . price , "currency" : "GBP" , "lineItems" : [ { "quantity" : 1 , "_id" : itemId , "price" : item . price } ] } } if ( array . users [ i ] . sessions [ j ] . events [ k ] . userAction == 'Update Cart' ) { event . cart = { "singleLine" : { } } ; event . cart . singleLine [ catalogType ] = { "quantity" : 1 , "_id" : itemId , "price" : item . price } } events . push ( event ) ; timeStamp = timeStamp + array . users [ i ] . sessions [ j ] . events [ k ] . viewTime ; if ( k == 0 ) { var event = { "action" : array . users [ i ] . sessions [ j ] . events [ k ] . userAction , "flags" : { "noCampaigns" : true , "pageView" : true } , "source" : { "channel" : "Demo Builder" , "local" : "enGB" , "time" : timeStamp , "top" : 0 } , "user" : array . users [ i ] . user } event . itemAction = 'View Item' ; event . catalog = { } ; event . catalog [ catalogType ] = { "_id" : itemId } events . push ( event ) ; } } } } array . events = events ; }
Function to format events in to the correct evergage format
Function to format events in to the correct evergage format
// Add in custom options // Add in Page options // Add in Order node if required // Add in Cart node if required