Google Analytics Plugin - Reverse an Ecommerce transaction

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 yıl önce
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!
9 yıl önce
Hi!

I did it using this sample:

http://www.codeproject.com/Articles/493455/Server-side-Google-Analytics-Transactions

I added an OrderCancelledEventConsumer into plugin and used the code above.

Thanks!
9 yıl önce
ivanslater wrote:
Hi!

I did it using this sample:

http://www.codeproject.com/Articles/493455/Server-side-Google-Analytics-Transactions

I added an OrderCancelledEventConsumer into plugin and used the code above.

Thanks!


So you actually created a plugin which replaces the entire GA -ecommerce plugin? Or did you just add a plugin for handling the cancellations/returns?

A server-side solution is so much more desirable than the javascript. We also have problems with folks not returning to the Thank You page. I'm sure others(like me) would be grateful if you shared your solution!
9 yıl önce
Actually I just add an OrderCancelledEvent in nops plugin (Nop.Plugin.Widgets.GoogleAnalytics).

So, I used the soluction from my last post to make a server request to analitycs scripts, cancelling the order, following the google's recomendation.

I simple used some GoogleAnalytics settings and replaced the quantities with negative values.

See:


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;
        }
    }
}
9 yıl önce
OK, I see.

Thanks!
9 yıl önce
Nice, can this become default Nop?
9 yıl önce
Just one fix:

request.HostName = System.Web.HttpContext.Current.Request.Url.ToString();

The request url doesnt exists in current context, so I used hardcoded hostname.

Thanks!
9 yıl önce
Thanks a lot. I've just created a work item
7 yıl önce
OK.
6 yıl önce
And finally done 9with some refactoring). Please see this and this commits
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.