Example #1
0
        private void ProcessOrderEvent(Order order, bool add)
        {
            try
            {
                //settings per store
                var store = _storeService.GetStoreById(order.StoreId) ?? _storeContext.CurrentStore;
                var googleAnalyticsSettings = _settingService.LoadSetting <GoogleAnalyticsSettings>(store.Id);

                var request = new GoogleRequest
                {
                    AccountCode = googleAnalyticsSettings.GoogleId,
                    Culture     = "en-US",
                    HostName    = new Uri(_webHelper.GetThisPageUrl(false)).Host,
                    PageTitle   = add ? "AddTransaction" : "CancelTransaction"
                };

                var orderId       = order.CustomOrderNumber;
                var orderShipping = googleAnalyticsSettings.IncludingTax ? order.OrderShippingInclTax : order.OrderShippingExclTax;
                var orderTax      = order.OrderTax;
                var orderTotal    = order.OrderTotal;
                if (!add)
                {
                    orderShipping = -orderShipping;
                    orderTax      = -orderTax;
                    orderTotal    = -orderTotal;
                }
                var trans = new Transaction(FixIllegalJavaScriptChars(orderId),
                                            order.BillingAddress == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.City),
                                            order.BillingAddress == null || order.BillingAddress.Country == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.Country.Name),
                                            order.BillingAddress == null || order.BillingAddress.StateProvince == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.StateProvince.Name),
                                            store.Name,
                                            orderShipping,
                                            orderTax,
                                            orderTotal);

                foreach (var item in order.OrderItems)
                {
                    //get category
                    var category = _categoryService.GetProductCategoriesByProductId(item.ProductId).FirstOrDefault()?.Category?.Name;

                    var unitPrice = googleAnalyticsSettings.IncludingTax ? item.UnitPriceInclTax : item.UnitPriceExclTax;
                    var qty       = item.Quantity;
                    if (!add)
                    {
                        qty = -qty;
                    }

                    var sku = _productService.FormatSku(item.Product, item.AttributesXml);
                    if (String.IsNullOrEmpty(sku))
                    {
                        sku = item.Product.Id.ToString();
                    }
                    var product = new TransactionItem(FixIllegalJavaScriptChars(orderId),
                                                      FixIllegalJavaScriptChars(sku),
                                                      FixIllegalJavaScriptChars(item.Product.Name),
                                                      unitPrice,
                                                      qty,
                                                      FixIllegalJavaScriptChars(category));

                    trans.Items.Add(product);
                }

                request.SendRequest(trans, _httpClientFactory.CreateClient(NopHttpDefaults.DefaultHttpClient));
            }
            catch (Exception ex)
            {
                _logger.InsertLog(LogLevel.Error, "Google Analytics. Error canceling transaction from server side", ex.ToString());
            }
        }
Example #2
0
        private async Task ProcessOrderEventAsync(Order order, bool add)
        {
            try
            {
                //settings per store
                var store = await _storeService.GetStoreByIdAsync(order.StoreId) ?? await _storeContext.GetCurrentStoreAsync();

                var googleAnalyticsSettings = await _settingService.LoadSettingAsync <GoogleAnalyticsSettings>(store.Id);

                var request = new GoogleRequest
                {
                    AccountCode = googleAnalyticsSettings.GoogleId,
                    Culture     = "en-US",
                    HostName    = new Uri(_webHelper.GetThisPageUrl(false)).Host,
                    PageTitle   = add ? "AddTransaction" : "CancelTransaction"
                };

                var orderId       = order.CustomOrderNumber;
                var orderShipping = googleAnalyticsSettings.IncludingTax ? order.OrderShippingInclTax : order.OrderShippingExclTax;
                var orderTax      = order.OrderTax;
                var orderTotal    = order.OrderTotal;
                if (!add)
                {
                    orderShipping = -orderShipping;
                    orderTax      = -orderTax;
                    orderTotal    = -orderTotal;
                }

                var billingAddress = await _addressService.GetAddressByIdAsync(order.BillingAddressId);

                var trans = new Transaction(FixIllegalJavaScriptChars(orderId),
                                            FixIllegalJavaScriptChars(billingAddress.City),
                                            await _countryService.GetCountryByAddressAsync(billingAddress) is Country country ? FixIllegalJavaScriptChars(country.Name) : string.Empty,
                                            await _stateProvinceService.GetStateProvinceByAddressAsync(billingAddress) is StateProvince stateProvince ? FixIllegalJavaScriptChars(stateProvince.Name) : string.Empty,
                                            store.Name,
                                            orderShipping,
                                            orderTax,
                                            orderTotal);

                foreach (var item in await _orderService.GetOrderItemsAsync(order.Id))
                {
                    var product = await _productService.GetProductByIdAsync(item.ProductId);

                    //get category
                    var category  = (await _categoryService.GetCategoryByIdAsync((await _categoryService.GetProductCategoriesByProductIdAsync(product.Id)).FirstOrDefault()?.CategoryId ?? 0))?.Name;
                    var unitPrice = googleAnalyticsSettings.IncludingTax ? item.UnitPriceInclTax : item.UnitPriceExclTax;
                    var qty       = item.Quantity;
                    if (!add)
                    {
                        qty = -qty;
                    }

                    var sku = await _productService.FormatSkuAsync(product, item.AttributesXml);

                    if (string.IsNullOrEmpty(sku))
                    {
                        sku = product.Id.ToString();
                    }

                    var productItem = new TransactionItem(FixIllegalJavaScriptChars(orderId),
                                                          FixIllegalJavaScriptChars(sku),
                                                          FixIllegalJavaScriptChars(product.Name),
                                                          unitPrice,
                                                          qty,
                                                          FixIllegalJavaScriptChars(category));

                    trans.Items.Add(productItem);
                }

                await request.SendRequest(trans, _httpClientFactory.CreateClient(NopHttpDefaults.DefaultHttpClient));
            }
            catch (Exception ex)
            {
                await _logger.InsertLogAsync(LogLevel.Error, "Google Analytics. Error canceling transaction from server side", ex.ToString());
            }
        }
Example #3
0
        private void ProcessOrderEvent(Order order, bool add)
        {
            //ensure the plugin is installed and active
            var plugin = _widgetService.LoadWidgetBySystemName("Widgets.GoogleAnalytics") as GoogleAnalyticsPlugin;

            if (plugin == null ||
                !plugin.IsWidgetActive(_widgetSettings) || !plugin.PluginDescriptor.Installed)
            {
                return;
            }

            try
            {
                var store = _storeService.GetStoreById(order.StoreId) ?? _storeContext.CurrentStore;
                //settings per store
                var googleAnalyticsSettings = _settingService.LoadSetting <GoogleAnalyticsSettings>(store.Id);

                if (!googleAnalyticsSettings.EnableEcommerce)
                {
                    return;
                }

                var request = new GoogleRequest
                {
                    AccountCode = googleAnalyticsSettings.GoogleId,
                    Culture     = "en-US",
                    HostName    = new Uri(_webHelper.GetThisPageUrl(false)).Host,
                    PageTitle   = add ? "AddTransaction" : "CancelTransaction"
                };

                var orderId       = order.Id.ToString(); //pass custom order number? order.CustomOrderNumber
                var orderShipping = googleAnalyticsSettings.IncludingTax ? order.OrderShippingInclTax : order.OrderShippingExclTax;
                var orderTax      = order.OrderTax;
                var orderTotal    = order.OrderTotal;
                if (!add)
                {
                    orderShipping = -orderShipping;
                    orderTax      = -orderTax;
                    orderTotal    = -orderTotal;
                }
                var trans = new Transaction(orderId,
                                            order.BillingAddress == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.City),
                                            order.BillingAddress == null || order.BillingAddress.Country == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.Country.Name),
                                            order.BillingAddress == null || order.BillingAddress.StateProvince == null ? "" : FixIllegalJavaScriptChars(order.BillingAddress.StateProvince.Name),
                                            store.Name,
                                            orderShipping,
                                            orderTax,
                                            orderTotal);

                foreach (var item in order.OrderItems)
                {
                    //get category
                    var category = "";
                    var defaultProductCategory = _categoryService.GetProductCategoriesByProductId(item.ProductId).FirstOrDefault();
                    if (defaultProductCategory != null)
                    {
                        category = defaultProductCategory.Category.Name;
                    }

                    var unitPrice = googleAnalyticsSettings.IncludingTax ? item.UnitPriceInclTax : item.UnitPriceExclTax;
                    var qty       = item.Quantity;
                    if (!add)
                    {
                        qty = -qty;
                    }

                    var product = new TransactionItem(FixIllegalJavaScriptChars(orderId),
                                                      FixIllegalJavaScriptChars(item.Product.FormatSku(item.AttributesXml, _productAttributeParser)),
                                                      FixIllegalJavaScriptChars(item.Product.Name),
                                                      unitPrice,
                                                      qty,
                                                      FixIllegalJavaScriptChars(category));

                    trans.Items.Add(product);
                }

                request.SendRequest(trans);
            }
            catch (Exception ex)
            {
                _logger.InsertLog(LogLevel.Error, "Google Analytics. Error canceling transaction from server side", ex.ToString());
            }
        }