suppie
suppie

Reputation: 3

Google Analytics E-Commerce Tracking in C#

Working with a 3rd party developer on implementing Google Analytics server-side e-commerce tracking in conjunction with Paypal. Referencing source code on Code Project (http://www.codeproject.com/Articles/493455/Server-side-Google-Analytics-Transactions), we have implemented the following Google Analytics code on staging, but it is not firing any transactions:

private void GoogleAnalytics(Order order)
        {
            var wrapper = new HttpRequestWrapper(Request);
 
            try
            {
 
                GoogleRequest request = new GoogleRequest("UA-3820067-1", wrapper);
 
                request.Culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
                request.HostName = System.Web.HttpContext.Current.Request.Url.ToString();
                request.PageTitle = "ShoppingCart";
 
Transaction trans = new Transaction(Convert.ToInt32(order.OrderId), string.IsNullOrEmpty(order.Billing_City) ? (string.IsNullOrEmpty(order.Shipping_City) ? "" : order.Shipping_City) : order.Billing_City, string.IsNullOrEmpty(order.Shipping_Country) ? "" : order.Shipping_Country,
                Convert.ToString(order.RegionId), "store", string.IsNullOrEmpty(order.Shipping_Additional) ? 0 : Convert.ToDecimal(order.Shipping_Additional), Convert.ToDecimal(order.Total), Convert.ToDecimal(order.TaxAmount));
 
                foreach (OrderItem orderItem in order.Items)
                {
                    //ProductCode or SKU, ProductName, ItemPrice, Quantity, GroupName
                    TransactionItem item = new TransactionItem(Convert.ToString(orderItem.SurveyId), orderItem.SurveyName, Convert.ToDecimal(orderItem.Price), orderItem.Quantity, "Survey");
                    trans.AddTransactionItem(item);
                                    }
 
                   //Now just make a request for this transaction object
                    request.SendRequest(trans);
            }
            catch (Exception ex)
            {
                    log.Error("Exception occured while Google Analytics process ", ex);
            }
 
        }

We have also implemented Paypal PDT for receiving the transaction information, and this is currently functioning properly. The following is what we are calling when PDTResponse is successful.

if (strResponse.StartsWith("SUCCESS"))
                {
                    PDTHolder pdt = PDTHolder.Parse(strResponse);
                    strCustom = pdt.Custom;
                    strCustom = HttpUtility.UrlDecode(strCustom);
 
 
                    if (strCustom.Contains("&"))
                    {
                        string[] arrCustom = strCustom.Split('&');
                        cc = arrCustom[0].Split('=')[1];
                        pp = arrCustom[1].Split('=')[1];
                        orderID = Convert.ToInt32(arrCustom[2].Split('=')[1]);
                        cartID = arrCustom[3].Split('=')[1];
                    }
 
                    order.OrderId = Convert.ToInt32(orderID);
                    order.LoadByOrderId(Config.SurveyDsn);
                    this.GoogleAnalytics(order);
                }
                else
                {
                    log.Debug("PDT failure");
                }

Any assistance with this would be greatly appreciated as I am stuck on what to do next to receive the information from Paypal and send to the Google Analytics servers.

Upvotes: 0

Views: 714

Answers (0)

Related Questions