using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Nop.Core.Domain.Customers; using Nop.Core.Domain.Messages; using Nop.Core.Domain.Orders; using Nop.Core.Events; using Nop.Services.Events; using Nop.Services.Messages; using Nop.Web.Framework; using Nop.Web.Framework.Events; using Nop.Web.Framework.Models; using Nop.Web.Models.Catalog; namespace Nop.Plugin.Widgets.FacebookPixel.Services; /// /// Represents plugin event consumer /// public class EventConsumer : IConsumer, IConsumer>, IConsumer>, IConsumer>, IConsumer, IConsumer, IConsumer { #region Fields protected readonly FacebookPixelService _facebookPixelService; protected readonly IHttpContextAccessor _httpContextAccessor; #endregion #region Ctor public EventConsumer(FacebookPixelService facebookPixelService, IHttpContextAccessor httpContextAccessor) { _facebookPixelService = facebookPixelService; _httpContextAccessor = httpContextAccessor; } #endregion #region Methods /// /// Handle shopping cart item inserted event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { if (eventMessage?.Entity != null) await _facebookPixelService.SendAddToCartEventAsync(eventMessage.Entity); } /// /// Handle order placed event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(OrderPlacedEvent eventMessage) { if (eventMessage?.Order != null) await _facebookPixelService.SendPurchaseEventAsync(eventMessage.Order); } /// /// Handle product details model prepared event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(ModelPreparedEvent eventMessage) { if (eventMessage?.Model is ProductDetailsModel productDetailsModel) await _facebookPixelService.SendViewContentEventAsync(productDetailsModel); } /// /// Handle page rendering event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(PageRenderingEvent eventMessage) { var routeName = eventMessage.GetRouteName() ?? string.Empty; if (routeName == FacebookPixelDefaults.CheckoutRouteName || routeName == FacebookPixelDefaults.CheckoutOnePageRouteName) await _facebookPixelService.SendInitiateCheckoutEventAsync(); if (_httpContextAccessor.HttpContext.GetRouteValue("area") is not string area || area != AreaNames.ADMIN) await _facebookPixelService.SendPageViewEventAsync(); } /// /// Handle product search event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(ProductSearchEvent eventMessage) { if (eventMessage?.SearchTerm != null) await _facebookPixelService.SendSearchEventAsync(eventMessage.SearchTerm); } /// /// Handle message token added event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(MessageTokensAddedEvent eventMessage) { if (eventMessage?.Message?.Name == MessageTemplateSystemNames.CONTACT_US_MESSAGE) await _facebookPixelService.SendContactEventAsync(); } /// /// Handle customer registered event /// /// Event message /// A task that represents the asynchronous operation public async Task HandleEventAsync(CustomerRegisteredEvent eventMessage) { if (eventMessage?.Customer != null) await _facebookPixelService.SendCompleteRegistrationEventAsync(); } #endregion }