Esempio n. 1
0
        protected void SetDataLayerVariables <T>(T obj, WebhookEventTypes activityType)
        {
            _headTagbuilder.AddDataLayer("EventType", activityType.ToString());
            if (obj != null)
            {
                var objType = obj.GetType().Name;
                switch (objType)
                {
                case "ProductDetailModel":
                    var p = (ProductDetailModel)(object)obj;
                    if (p != null)
                    {
                        var prod = new { id = p.Id, sku = p.Sku, name = p.Name, stockCode = p.StockCode };
                        _headTagbuilder.AddDataLayer("EntityId", p.StockCode);
                        _headTagbuilder.AddDataLayer("EntityName", p.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(prod));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Product.ToString());
                    break;

                case "BasketModel":
                    var b = (BasketModel)(object)obj;
                    if (b != null)
                    {
                        var lines = (from l in b.LineItems select new { id = l.Id, stockCode = l.StockCode, name = l.Name, qty = l.Qty, price = l.Price?.Raw?.WithoutTax, tax = l.Price?.Raw?.Tax, manufacturer = l.Manufacture }).ToList();
                        var name  = string.Join(",", (from l in lines select l.name + " (" + l.qty.ToString() + ")").ToArray());
                        var bskt  = new { id = b.Id, grandTotal = b.GrandTotal?.Raw?.WithoutTax, tax = b.GrandTotal?.Raw.Tax, shipCharge = b.ShippingCharge?.Raw?.WithoutTax, shipTax = b.ShippingCharge?.Raw.Tax, lineitems = lines, promoCode = b.PromotionsApplied };
                        _headTagbuilder.AddDataLayer("BasketTotal", b.GrandTotal?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("Tax", b.GrandTotal?.Raw?.Tax);
                        _headTagbuilder.AddDataLayer("BasketItems", JsonConvert.SerializeObject(lines));
                        _headTagbuilder.AddDataLayer("BasketItemCount", lines.Sum(l => l.qty));
                        _headTagbuilder.AddDataLayer("PromoCodes", JsonConvert.SerializeObject(b.PromotionsApplied));
                        _headTagbuilder.AddDataLayer("ShippingCost", b.ShippingCharge?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("EntityId", b.Id);
                        _headTagbuilder.AddDataLayer("EntityName", name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(bskt));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Basket.ToString());
                    break;

                case "OrderModel":
                    var ord = (OrderModel)(object)obj;
                    if (ord != null)
                    {
                        var ordlines = (from o in ord.Items select new { id = o.Id, stockCode = o.StockCode, name = o.Name, qty = o.Qty, price = o.Price?.Raw.WithoutTax, tax = o.Price?.Raw?.Tax, manufacturer = o.Manufacturer, categories = o.CategoryItems }).ToList();
                        var order    = new { id = ord.Id, basketId = ord.BasketId, customerId = ord.CustomerId, OrderNo = ord.OrderNo, grandTotal = ord.GrandTotal?.Formatted, shipCharge = ord.ShippingCharge?.Formatted, subTotal = ord.SubTotal?.Formatted, discount = ord.Discount, lineitems = ordlines, promoCode = ord.Promotions, shippingMethod = ord.Shipping, shippingAddress = ord.ShippingAddress };
                        _headTagbuilder.AddDataLayer("BasketTotal", ord.GrandTotal?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("Tax", ord.GrandTotal?.Raw?.Tax);
                        _headTagbuilder.AddDataLayer("BasketItems", JsonConvert.SerializeObject(ordlines));
                        _headTagbuilder.AddDataLayer("BasketItemCount", ordlines.Sum(l => l.qty));
                        _headTagbuilder.AddDataLayer("PromoCodes", JsonConvert.SerializeObject(ord.Promotions));
                        _headTagbuilder.AddDataLayer("ShippingCost", ord.ShippingCharge?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("EntityId", ord.OrderNo);
                        _headTagbuilder.AddDataLayer("EntityName", ord.OrderNo);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(order));

                        _headTagbuilder.AddDataLayer("VisitorId", ord.CustomerId, true);    // VisitorId is added here because we don't have userid in session in case of guest checkout
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Order.ToString());
                    break;

                case "BlogDetailViewModel":
                    var blg = (BlogDetailViewModel)(object)obj;
                    if (blg != null)
                    {
                        var blog = new { slug = blg.Slug, freeText = blg.FreeText, blg.Detail?.RecordId, blg.Detail?.Tag, blg.Detail?.Title };
                        _headTagbuilder.AddDataLayer("EntityId", blg.Slug);
                        _headTagbuilder.AddDataLayer("EntityName", blg.Detail?.Title);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(blog));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Blog.ToString());
                    break;

                case "BrandDetailModel":
                    var brnd = (BrandDetailModel)(object)obj;
                    if (brnd != null)
                    {
                        var brand = new { id = brnd.Id, name = brnd.Name, manufName = brnd.ParentManufacturerName };
                        _headTagbuilder.AddDataLayer("EntityId", brnd.Id);
                        _headTagbuilder.AddDataLayer("EntityName", brnd.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(brand));
                    }

                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Brand.ToString());
                    break;

                case "CategoryModel":
                    var cat = (CategoryModel)(object)obj;
                    if (cat != null)
                    {
                        var category = new { id = cat.Id, name = cat.Name };
                        _headTagbuilder.AddDataLayer("EntityId", cat.Id);
                        _headTagbuilder.AddDataLayer("EntityName", cat.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(category));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Category.ToString());
                    break;

                case "SiteViewModel":
                    var sv = (SiteViewModel)(object)obj;
                    if (sv != null)
                    {
                        var view = new { id = sv.Id, name = sv.Name, metaTitle = sv.MetaTitle, sv.MetaKeywords, sv.MetaDescription, sv.Slug, sv.Title, sv.ViewType };
                        _headTagbuilder.AddDataLayer("EntityId", sv.Id);
                        _headTagbuilder.AddDataLayer("EntityName", sv.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(view));
                    }
                    if (sv?.Slug == "/")
                    {
                        _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Page.ToString());
                    }
                    else
                    {
                        _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.CmsPage.ToString());
                    }

                    break;

                case "DynamicListCollection":
                    var cl = (DynamicListCollection)(object)obj;
                    if (cl != null)
                    {
                        var collections = new { cl.Slug, cl.Name, cl.Id, cl.MetaKeywords, cl.NoOfRecords, cl.MetaTitle };
                        _headTagbuilder.AddDataLayer("EntityId", cl.Id);
                        _headTagbuilder.AddDataLayer("EntityName", cl.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(collections));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Collection.ToString());
                    break;

                case "DynamicListModel":
                    var dl = (DynamicListModel)(object)obj;
                    if (dl != null)
                    {
                        var collections = new { dl.Name, dl.Id, dl.MetaKeywords, dl.DisplayTitle, dl.MetaTitle };
                        _headTagbuilder.AddDataLayer("EntityId", dl.Id);
                        _headTagbuilder.AddDataLayer("EntityName", dl.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(collections));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Collection.ToString());
                    break;

                case "SearchRequestModel":
                    var sm = (SearchRequestModel)(object)obj;
                    if (sm != null)
                    {
                        _headTagbuilder.AddDataLayer("EntityId", sm.FreeText);
                        _headTagbuilder.AddDataLayer("EntityName", sm.FreeText);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(sm));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Search.ToString());
                    break;

                default:

                    break;
                }
            }
        }
Esempio n. 2
0
        protected void SetDataLayerVariables <T>(T obj, WebhookEventTypes activityType)
        {
            _headTagbuilder.AddDataLayer("EventType", activityType.ToString());
            if (obj != null)
            {
                var objType = obj.GetType().Name;
                switch (objType)
                {
                case "ProductDetailModel":
                    var p = (ProductDetailModel)(object)obj;
                    if (p != null)
                    {
                        var prod = new { id = p.RecordId, sku = p.Sku, name = p.Name, stockCode = p.StockCode, img = p.Image };
                        _headTagbuilder.AddDataLayer("EntityId", p.RecordId);
                        _headTagbuilder.AddDataLayer("EntityName", p.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(prod));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Product.ToString());
                    break;

                case "BasketModel":
                    var b = (BasketModel)(object)obj;
                    if (b != null)
                    {
                        var lines = (from l in b.LineItems select new { id = l.Id, productId = l.ProductId, basketId = b.Id, stockCode = l.StockCode, name = l.Name, qty = l.Qty, price = l.Price?.Raw?.WithoutTax, tax = l.Price?.Raw?.Tax, manufacturer = l.Manufacture, img = l.Image }).ToList();
                        var name  = string.Join(",", (from l in lines select l.name + " (" + l.qty.ToString() + ")").ToArray());
                        var bskt  = new { id         = b.Id,
                                          grandTotal = b.GrandTotal?.Raw?.WithoutTax,
                                          tax        = b.GrandTotal?.Raw.Tax,
                                          taxPercent = b.TaxPercent,
                                          shipCharge = b.ShippingCharge?.Raw?.WithoutTax,
                                          shipTax    = b.ShippingCharge?.Raw.Tax,
                                          lineitems  = lines,
                                          promoCode  = b.PromotionsApplied };

                        _headTagbuilder.AddDataLayer("BasketTotal", b.GrandTotal?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("Tax", b.GrandTotal?.Raw?.Tax);
                        _headTagbuilder.AddDataLayer("BasketItems", JsonConvert.SerializeObject(lines));
                        _headTagbuilder.AddDataLayer("BasketItemCount", lines.Sum(l => l.qty));
                        _headTagbuilder.AddDataLayer("PromoCodes", JsonConvert.SerializeObject(b.PromotionsApplied));
                        _headTagbuilder.AddDataLayer("ShippingCost", b.ShippingCharge?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("EntityId", b.Id);
                        _headTagbuilder.AddDataLayer("EntityName", name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(bskt));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Basket.ToString());
                    break;

                case "OrderModel":
                    var ord = (OrderModel)(object)obj;
                    if (ord != null)
                    {
                        var ordlines = (from o in ord.Items select new { id = o.Id, productId = o.ProductId, stockCode = o.StockCode, name = o.Name, discountAmt = o.DiscountAmt?.Raw?.WithoutTax, isSubscription = o.IsSubscription, itemType = o.ItemType, qty = o.Qty, price = o.Price?.Raw.WithoutTax, tax = o.Price?.Raw?.Tax, manufacturer = o.Manufacturer, categories = o.CategoryItems, img = o.Image }).ToList();
                        //var order = new { id = ord.Id, basketId = ord.BasketId, customerId = ord.CustomerId, OrderNo = ord.OrderNo, grandTotal = ord.GrandTotal?.Formatted, shipCharge = ord.ShippingCharge?.Formatted, subTotal = ord.SubTotal?.Formatted, discount = ord.Discount?.Formatted, lineitems = ordlines, promoCode = ord.Promotions, shippingMethod = ord.Shipping, shippingAddress = ord.ShippingAddress,status=ord.OrderStatus };
                        var payments = (from py in ord.Payments  where !string.IsNullOrEmpty(py.PaymentMethod) select new { methodName = py.PaymentMethod, paymentGateway = py.PaymentGateway, amount = py.OrderAmount }).ToList();

                        var order = new
                        {
                            id              = ord.Id,
                            basketId        = ord.BasketId,
                            customerId      = ord.CustomerId,
                            OrderNo         = ord.OrderNo,
                            grandTotal      = ord.GrandTotal.Raw.WithTax,
                            tax             = ord.GrandTotal.Raw.Tax,
                            taxPercent      = ord.TaxPercent,
                            shipCharge      = ord.ShippingCharge.Raw.WithoutTax,
                            subTotal        = ord.SubTotal.Raw.WithoutTax,
                            discount        = ord.Discount.Raw.WithoutTax,
                            lineitems       = ordlines,
                            promoCode       = ord.Promotions,
                            payments        = payments,
                            paidAmount      = payments.Sum(py => py.amount),
                            shippingMethod  = ord.Shipping,
                            shippingAddress = ord.ShippingAddress,
                            billingAddress  = ord.BillingAddress,
                            status          = ord.OrderStatus
                        };
                        _headTagbuilder.AddDataLayer("BasketTotal", ord.GrandTotal?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("Tax", ord.GrandTotal?.Raw?.Tax);
                        _headTagbuilder.AddDataLayer("BasketItems", JsonConvert.SerializeObject(ordlines));
                        _headTagbuilder.AddDataLayer("BasketItemCount", ordlines.Sum(l => l.qty));
                        _headTagbuilder.AddDataLayer("PromoCodes", JsonConvert.SerializeObject(ord.Promotions));
                        _headTagbuilder.AddDataLayer("ShippingCost", ord.ShippingCharge?.Raw?.WithoutTax);
                        _headTagbuilder.AddDataLayer("EntityId", ord.Id);
                        _headTagbuilder.AddDataLayer("EntityName", ord.OrderNo);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(order));

                        _headTagbuilder.AddDataLayer("VisitorId", ord.CustomerId, true);    // VisitorId is added here because we don't have userid in session in case of guest checkout
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Order.ToString());
                    break;

                case "BlogDetailViewModel":
                    var blg = (BlogDetailViewModel)(object)obj;
                    if (blg != null)
                    {
                        var blog = new { slug = blg.Slug, freeText = blg.FreeText, blg.Detail?.RecordId, blg.Detail?.Tag, blg.Detail?.Title };
                        _headTagbuilder.AddDataLayer("EntityId", blg.Slug);
                        _headTagbuilder.AddDataLayer("EntityName", blg.Detail?.Title);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(blog));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Blog.ToString());
                    break;

                case "BrandDetailModel":
                    var brnd = (BrandDetailModel)(object)obj;
                    if (brnd != null)
                    {
                        var brand = new { id = brnd.Id, name = brnd.Name, manufName = brnd.ParentManufacturerName };
                        _headTagbuilder.AddDataLayer("EntityId", brnd.Id);
                        _headTagbuilder.AddDataLayer("EntityName", brnd.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(brand));
                    }

                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Brand.ToString());
                    break;

                case "CategoryModel":
                    var cat = (CategoryModel)(object)obj;
                    if (cat != null)
                    {
                        var category = new { id = cat.Id, name = cat.Name };
                        _headTagbuilder.AddDataLayer("EntityId", cat.Id);
                        _headTagbuilder.AddDataLayer("EntityName", cat.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(category));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Category.ToString());
                    break;

                case "SiteViewModel":
                    var sv = (SiteViewModel)(object)obj;
                    if (sv != null)
                    {
                        var view = new { id = sv.Id, name = sv.Name, metaTitle = sv.MetaTitle, sv.MetaKeywords, sv.MetaDescription, sv.Slug, sv.Title, sv.ViewType };
                        _headTagbuilder.AddDataLayer("EntityId", sv.Id);
                        _headTagbuilder.AddDataLayer("EntityName", sv.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(view));
                    }
                    if (sv?.Slug == "/")
                    {
                        _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Page.ToString());
                    }
                    else
                    {
                        _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.CmsPage.ToString());
                    }

                    break;

                case "DynamicListCollection":
                    var cl = (DynamicListCollection)(object)obj;
                    if (cl != null)
                    {
                        var collections = new { cl.Slug, cl.Name, cl.Id, cl.MetaKeywords, cl.NoOfRecords, cl.MetaTitle };
                        _headTagbuilder.AddDataLayer("EntityId", cl.Id);
                        _headTagbuilder.AddDataLayer("EntityName", cl.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(collections));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Collection.ToString());
                    break;

                case "DynamicListModel":
                    var dl = (DynamicListModel)(object)obj;
                    if (dl != null)
                    {
                        var collections = new { dl.Name, dl.Id, dl.MetaKeywords, dl.DisplayTitle, dl.MetaTitle };
                        _headTagbuilder.AddDataLayer("EntityId", dl.Id);
                        _headTagbuilder.AddDataLayer("EntityName", dl.Name);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(collections));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Collection.ToString());
                    break;

                case "SearchRequestModel":
                    var sm = (SearchRequestModel)(object)obj;
                    if (sm != null)
                    {
                        _headTagbuilder.AddDataLayer("EntityId", sm.FreeText);
                        _headTagbuilder.AddDataLayer("EntityName", sm.FreeText);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(sm));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Search.ToString());
                    break;

                case "CustomerProfileModel":
                    var user = (CustomerProfileModel)(object)obj;
                    if (user != null)
                    {
                        DateTime dob;
                        DateTime.TryParse(user.CustomerDetail.YearOfBirth + "-" + user.CustomerDetail.MonthOfBirth + "-" + user.CustomerDetail.DayOfBirth, out dob);

                        var userBase = new
                        {
                            id          = user.CustomerDetail.UserId,
                            name        = user.CustomerDetail.FirstName + user.CustomerDetail.LastName,
                            dateOfBirth = (dob.Date == DateTime.MinValue) ? "" : dob.ToString(),
                            gender      = user.CustomerDetail.Gender,
                            email       = user.CustomerDetail.Email,
                            postCode    = user.CustomerDetail.PostCode
                        };
                        // making the dob format standard.
                        user.ChangePasswordViewModel  = null;     //explicitly setting this view to nulll to avoid sending accidentla pwd data.
                        user.CustomerDetail.BirthDate = user.CustomerDetail.YearOfBirth + "-" + user.CustomerDetail.MonthOfBirth + "-" + user.CustomerDetail.DayOfBirth;
                        _headTagbuilder.AddDataLayer("EntityId", user.CustomerDetail.UserId);
                        _headTagbuilder.AddDataLayer("EntityName", user.CustomerDetail.FirstName + user.CustomerDetail.LastName);
                        _headTagbuilder.AddDataLayer("Entity", JsonConvert.SerializeObject(userBase));
                    }
                    _headTagbuilder.AddDataLayer("EntityType", WebhookEntityTypes.Customer.ToString());
                    break;

                default:
                    break;
                }
            }
        }