Hi there!
Google Analytics Plugin should reverse transactions when an order is reffunded.
It is simple to add, see:
https://support.google.com/analytics/answer/1037443?hl=en
Thanks!
using System;
using Nop.Core;
using Nop.Core.Domain.Orders;
using Nop.Core.Plugins;
using Nop.Services.Events;
using Nop.Services.Orders;
using System.Globalization;
using Middelpat.GoogleAnalytics;
using System.Web.Mvc;
using Nop.Services.Logging;
using Nop.Services.Catalog;
using System.Linq;
namespace Nop.Plugin.Widgets.GoogleAnalytics
{
public class OrderCancelledConsumer : IConsumer<OrderCancelledEvent>
{
private readonly GoogleAnalyticsSettings _googleSettings;
private readonly IPluginFinder _pluginFinder;
private readonly IOrderService _orderService;
private readonly IStoreContext _storeContext;
private readonly ILogger _logger;
private readonly ICategoryService _categoryService;
private readonly IProductAttributeParser _productAttributeParser;
public OrderCancelledConsumer(GoogleAnalyticsSettings googleSettings,
IPluginFinder pluginFinder,
IOrderService orderService,
IStoreContext storeContext,
ILogger logger,
ICategoryService categoryService,
IProductAttributeParser productAttributeParser)
{
this._googleSettings = googleSettings;
this._pluginFinder = pluginFinder;
this._orderService = orderService;
this._storeContext = storeContext;
this._logger = logger;
this._categoryService = categoryService;
this._productAttributeParser = productAttributeParser;
}
/// <summary>
/// Handles the event.
/// </summary>
/// <param name="eventMessage">The event message.</param>
public void HandleEvent(OrderCancelledEvent eventMessage)
{
//is enabled?
if (!_googleSettings.OrderCancelledEventEnabled)
return;
var order = eventMessage.Order;
try
{
//load settings for a chosen store scope
var usCulture = new CultureInfo("en-US");
GoogleRequest request = new GoogleRequest(_googleSettings.GoogleId, null);
request.Culture = "pt-BR";
request.HostName = System.Web.HttpContext.Current.Request.Url.ToString();
request.PageTitle = "CancelTransaction";
//OrderId, City, Country, State, StoreName, Shippincosts, Ordertotal, TaxCosts
Transaction trans = new Transaction(order.Id, order.ShippingAddress.City,
"Brasil", order.ShippingAddress.StateProvince.Name, "Store Name", (order.OrderShippingInclTax * -1), (order.OrderTotal * -1), (order.OrderTax * -1));
//For each orderline add a new TransactionItem to the Transaction object
foreach (var item in order.OrderItems)
{
string categ = "";
var defaultProductCategory = _categoryService.GetProductCategoriesByProductId(item.ProductId).FirstOrDefault();
if (defaultProductCategory != null)
categ = defaultProductCategory.Category.Name;
//ProductCode or SKU, ProductName, ItemPrice, Quantity, GroupName
TransactionItem product = new TransactionItem(FixIllegalJavaScriptChars(item.Product.FormatSku(item.AttributesXml, _productAttributeParser)),
FixIllegalJavaScriptChars(item.Product.Name), item.UnitPriceInclTax, (item.Quantity * -1), FixIllegalJavaScriptChars(categ));
trans.AddTransactionItem(product);
}
//Now just make a request for this transaction object
request.SendRequest(trans);
}
catch (Exception ex)
{
_logger.InsertLog(Core.Domain.Logging.LogLevel.Error, "Error canceling transaction from server side", ex.ToString());
}
}
private string FixIllegalJavaScriptChars(string text)
{
if (String.IsNullOrEmpty(text))
return text;
//replace ' with \' (http://stackoverflow.com/questions/4292761/need-to-url-encode-labels-when-tracking-events-with-google-analytics)
text = text.Replace("'", "\\'");
return text;
}
}
}