public async Task <KlaviyoEvent> BuildEvent(VtexOrder vtexOrder, string eventType, DateTimeOffset lastChange) { KlaviyoEvent klaviyoEvent = null; if (vtexOrder != null) { MerchantSettings merchantSettings = await _orderFeedAPI.GetMerchantSettings(); klaviyoEvent = new KlaviyoEvent { CustomerProperties = new CustomerProperties { Address1 = vtexOrder.ShippingData.Address.Street, Address2 = vtexOrder.ShippingData.Address.Number, City = vtexOrder.ShippingData.Address.City, Country = vtexOrder.ShippingData.Address.Country, Email = vtexOrder.ClientProfileData.Email, FirstName = vtexOrder.ClientProfileData.FirstName, //vtexOrder.ShippingData.Address.FirstName, LastName = vtexOrder.ClientProfileData.LastName, //vtexOrder.ShippingData.Address.LastName, PhoneNumber = vtexOrder.ClientProfileData.Phone, //vtexOrder.ShippingData.Address.Phone, Region = vtexOrder.ShippingData.Address.State, Zip = vtexOrder.ShippingData.Address.PostalCode }, Event = eventType, Properties = new Properties { Brands = vtexOrder.Items.Select(i => i.AdditionalInfo.BrandName).ToList(), Categories = vtexOrder.Items.Select(i => i.AdditionalInfo.CategoriesIds.Replace(@"\", string.Empty)).ToList(), DiscountCode = string.Join(',', vtexOrder.RatesAndBenefitsData.RateAndBenefitsIdentifiers.Select(r => r.Name).ToList()), DiscountValue = vtexOrder.Totals.Where(t => t.Name == "Discounts").Select(d => d.Value).FirstOrDefault(), EventId = vtexOrder.OrderId, ItemNames = vtexOrder.Items.Select(i => i.Name).ToList(), Items = new List <KlaviyoItem>(), Value = ToDollar(vtexOrder.Totals.Sum(t => t.Value)) }, //Time = lastChange.UtcTicks, Token = merchantSettings.AppId }; foreach (Item item in vtexOrder.Items) { KlaviyoItem klaviyoItem = new KlaviyoItem { BillingAddress = new Address { Address1 = vtexOrder.ShippingData.Address.Street, Address2 = vtexOrder.ShippingData.Address.Number, City = vtexOrder.ShippingData.Address.City, Country = vtexOrder.ShippingData.Address.Country, FirstName = vtexOrder.ClientProfileData.FirstName, //vtexOrder.ShippingData.Address.FirstName, LastName = vtexOrder.ClientProfileData.LastName, //vtexOrder.ShippingData.Address.LastName, Region = vtexOrder.ShippingData.Address.State, Zip = vtexOrder.ShippingData.Address.PostalCode, Company = null, CountryCode = vtexOrder.ShippingData.Address.Country, Phone = vtexOrder.ClientProfileData.Phone, RegionCode = vtexOrder.ShippingData.Address.State }, Brand = item.AdditionalInfo.BrandName, Categories = new Dictionary <string, string>(), ImageUrl = item.ImageUrl.AbsoluteUri, ItemPrice = ToDollar(item.Price), ProductId = item.ProductId, ProductName = item.Name, ProductUrl = $"{merchantSettings.ProductRootUrl}{item.DetailUrl}", Quantity = item.Quantity, RowTotal = ToDollar(item.Price * item.Quantity), ShippingAddress = new Address { Address1 = vtexOrder.ShippingData.Address.Street, Address2 = vtexOrder.ShippingData.Address.Number, City = vtexOrder.ShippingData.Address.City, Country = vtexOrder.ShippingData.Address.Country, FirstName = vtexOrder.ClientProfileData.FirstName, //vtexOrder.ShippingData.Address.FirstName, LastName = vtexOrder.ClientProfileData.LastName, //vtexOrder.ShippingData.Address.LastName, Region = vtexOrder.ShippingData.Address.State, Zip = vtexOrder.ShippingData.Address.PostalCode, Company = null, CountryCode = vtexOrder.ShippingData.Address.Country, Phone = vtexOrder.ClientProfileData.Phone, RegionCode = vtexOrder.ShippingData.Address.State }, Sku = item.SellerSku }; klaviyoItem.Categories.Add("001", item.AdditionalInfo.CategoriesIds.ToString().Replace(@"\", string.Empty)); klaviyoItem.Categories.Add("002", item.AdditionalInfo.CategoriesIds.ToString().Replace(@"\", string.Empty)); klaviyoEvent.Properties.Items.Add(klaviyoItem); } _context.Vtex.Logger.Info("BuildEvent", null, JsonConvert.SerializeObject(klaviyoEvent)); } else { Console.WriteLine($"Could not load order {vtexOrder.OrderId}"); _context.Vtex.Logger.Info("BuildEvent", null, $"Could not load order {vtexOrder.OrderId}"); } return(klaviyoEvent); }
public async Task <KlaviyoEvent> BuildEvent(VtexOrder vtexOrder, string eventType, DateTimeOffset lastChange) { KlaviyoEvent klaviyoEvent = null; if (vtexOrder != null) { MerchantSettings merchantSettings = await _orderFeedAPI.GetMerchantSettings(); if (eventType.Equals(Constants.Events.PlacedOrder) || eventType.Equals(Constants.Events.Canceled) || eventType.Equals(Constants.Events.FulfilledOrder)) { List <string> categoryNames = new List <string>(); List <string> rawCategoryIds = vtexOrder.Items.Select(i => i.AdditionalInfo.CategoriesIds).ToList(); List <string> categoryIds = new List <string>(); foreach (string rawCategoryId in rawCategoryIds) { Console.WriteLine($" --- rawCategoryId = {rawCategoryId}"); List <string> parsedIds = rawCategoryId.Split('/').ToList(); categoryIds.AddRange(parsedIds); } foreach (string categoryId in categoryIds) { Console.WriteLine($" --- categoryId = {categoryId}"); if (!string.IsNullOrEmpty(categoryId)) { CategoryResponse categoryResponse = await _orderFeedAPI.GetCategoryById(categoryId); if (categoryResponse != null) { categoryNames.Add(categoryResponse.Name); } } } klaviyoEvent = new KlaviyoEvent { CustomerProperties = new CustomerProperties { //Address1 = vtexOrder.ShippingData.Address.Street, //Address2 = vtexOrder.ShippingData.Address.Number, //City = vtexOrder.ShippingData.Address.City, //Country = vtexOrder.ShippingData.Address.Country, Email = vtexOrder.ClientProfileData.Email, FirstName = vtexOrder.ClientProfileData.FirstName, //vtexOrder.ShippingData.Address.FirstName, LastName = vtexOrder.ClientProfileData.LastName, //vtexOrder.ShippingData.Address.LastName, PhoneNumber = vtexOrder.ClientProfileData.Phone, //vtexOrder.ShippingData.Address.Phone, //Region = vtexOrder.ShippingData.Address.State, //Zip = vtexOrder.ShippingData.Address.PostalCode }, Event = eventType, Properties = new Properties { Brands = vtexOrder.Items.Select(i => i.AdditionalInfo.BrandName).ToList(), Categories = categoryNames, DiscountCode = string.Join(',', vtexOrder.RatesAndBenefitsData.RateAndBenefitsIdentifiers.Select(r => r.Name).ToList()), DiscountValue = vtexOrder.Totals.Where(t => t.Name == "Discounts").Select(d => d.Value).FirstOrDefault(), EventId = vtexOrder.OrderId, ItemNames = vtexOrder.Items.Select(i => i.Name).ToList(), Items = new List <KlaviyoItem>(), Value = ToDollar(vtexOrder.Totals.Sum(t => t.Value)), BillingAddress = new Address { //Address1 = vtexOrder.ShippingData.Address.Street, //Address2 = vtexOrder.ShippingData.Address.Number, //City = vtexOrder.ShippingData.Address.City, //Country = vtexOrder.ShippingData.Address.Country, FirstName = vtexOrder.ClientProfileData.FirstName, //vtexOrder.ShippingData.Address.FirstName, LastName = vtexOrder.ClientProfileData.LastName, //vtexOrder.ShippingData.Address.LastName, //Region = vtexOrder.ShippingData.Address.State, //Zip = vtexOrder.ShippingData.Address.PostalCode, Company = null, //CountryCode = vtexOrder.ShippingData.Address.Country, Phone = vtexOrder.ClientProfileData.Phone, //RegionCode = vtexOrder.ShippingData.Address.State }, ShippingAddress = new Address { Address1 = vtexOrder.ShippingData.Address.Street, Address2 = vtexOrder.ShippingData.Address.Number, City = vtexOrder.ShippingData.Address.City, Country = vtexOrder.ShippingData.Address.Country, //FirstName = vtexOrder.ClientProfileData.FirstName, //vtexOrder.ShippingData.Address.FirstName, //LastName = vtexOrder.ClientProfileData.LastName, //vtexOrder.ShippingData.Address.LastName, Region = vtexOrder.ShippingData.Address.State, Zip = vtexOrder.ShippingData.Address.PostalCode, Company = null, CountryCode = vtexOrder.ShippingData.Address.Country, Phone = vtexOrder.ClientProfileData.Phone, RegionCode = vtexOrder.ShippingData.Address.State } }, //Time = lastChange.UtcTicks, Token = merchantSettings.AppId }; foreach (Item item in vtexOrder.Items) { KlaviyoItem klaviyoItem = new KlaviyoItem { Brand = item.AdditionalInfo.BrandName, Categories = new List <string>(), ImageUrl = item.ImageUrl.AbsoluteUri, ItemPrice = ToDollar(item.Price), ProductId = item.ProductId, ProductName = item.Name, ProductUrl = $"{merchantSettings.ProductRootUrl}{item.DetailUrl}", Quantity = item.Quantity, RowTotal = ToDollar(item.Price * item.Quantity), Sku = item.SellerSku }; List <string> itemCategoryIds = item.AdditionalInfo.CategoriesIds.Split('/').ToList(); foreach (string itemCategoryId in itemCategoryIds) { if (!string.IsNullOrEmpty(itemCategoryId)) { CategoryResponse categoryResponse = await _orderFeedAPI.GetCategoryById(itemCategoryId); if (categoryResponse != null && !string.IsNullOrEmpty(categoryResponse.Name)) { klaviyoItem.Categories.Add(categoryResponse.Name); } } } klaviyoEvent.Properties.Items.Add(klaviyoItem); } if (eventType.Equals(Constants.Events.Canceled)) { klaviyoEvent.Properties.Reason = vtexOrder.CancelReason; } } else if (eventType.Equals(Constants.Events.OrderedProduct)) { klaviyoEvent = new KlaviyoEvent { Token = merchantSettings.AppId, Event = eventType, CustomerProperties = new CustomerProperties { Email = vtexOrder.ClientProfileData.Email, FirstName = vtexOrder.ClientProfileData.FirstName, LastName = vtexOrder.ClientProfileData.LastName }, Properties = new Properties { EventId = vtexOrder.OrderId, Value = ToDollar(vtexOrder.Items[0].Price), //ToDollar(vtexOrder.Items[0].Price * vtexOrder.Items[0].Quantity), OrderId = vtexOrder.OrderId, ProductId = vtexOrder.Items[0].ProductId, Sku = vtexOrder.Items[0].SellerSku, ProductName = vtexOrder.Items[0].Name, Quantity = vtexOrder.Items[0].Quantity, ProductUrl = $"{merchantSettings.ProductRootUrl}{vtexOrder.Items[0].DetailUrl}", ImageUrl = vtexOrder.Items[0].ImageUrl.AbsoluteUri, Categories = new List <string>(), ProductBrand = vtexOrder.Items[0].AdditionalInfo.BrandName }, //Time = }; List <string> itemCategoryIds = vtexOrder.Items[0].AdditionalInfo.CategoriesIds.Split('/').ToList(); foreach (string itemCategoryId in itemCategoryIds) { if (!string.IsNullOrEmpty(itemCategoryId)) { CategoryResponse categoryResponse = await _orderFeedAPI.GetCategoryById(itemCategoryId); if (categoryResponse != null && !string.IsNullOrEmpty(categoryResponse.Name)) { klaviyoEvent.Properties.Categories.Add(categoryResponse.Name); } } } } _context.Vtex.Logger.Debug("BuildEvent", eventType, JsonConvert.SerializeObject(klaviyoEvent)); } else { Console.WriteLine($"Could not load order {vtexOrder.OrderId}"); _context.Vtex.Logger.Warn("BuildEvent", null, $"Could not load order {vtexOrder.OrderId}"); } return(klaviyoEvent); }