Organize and Share your Electronics the way you want. Sign-Up for a free account now. It takes only 30 seconds!

Google Analytics server side ecommerce tracking

Google Analytics server side ecommerce tracking

A recurring issue we have on several of the ecommerce websites we manage is the Google Analytics Ecommerce Tracking system which often fails to save the customer’s order into the analytics account when the customers have completed their purchases.

This can be caused by the customer having ad blocking software installed or not returning to the online shop and viewing the order confirmation page which contains the Google tracking code.

Google have a server side system called Google Analytics Measurement Protocol API which allows you to post direct requests to the analytics servers to log events such as page views or other tracked events.

I found a basic C# wrapper for the Google Analytics Measurement Protocol API on github https://gist.github.com/0liver/11229128 and modified the code to allow me to track the items ordered in addition to the customers order as shown below.

I hope this code is useful for anyone trying to track ecommerce purchases using the Google Analytics Measurement Protocol API systems.

 try
        {

            var request = (HttpWebRequest)WebRequest.Create("http://www.google-analytics.com/collect");
            request.Method = "POST";
            request.KeepAlive = false;
            // the request body we want to send

            int x = 1;
            var postData = new Dictionary<string, string>
                           {
                               { "v", "1" },
                               { "tid", "UA-000000000" }, // Replace with your Google tracking account number
                               { "cid", "555" },

                               { "dh","yourdomainname.com" },
                               { "dp", "http://cdn.electronicsinfoline.com/callback.aspx" },
                               { "dt", "Payment Complete" },

                              { "ti", "PP0001"}, // Transaction ID. Required.
                               { "ta", "Yoursitename" }, // Affiliation.
                               { "tr", 99.99" }, // Revenue.
                               { "tt", "0" }, // Tax.
                               { "ts", "5.99" }, // Shipping.
                               { "pa", "purchase" }, // purchase.

                           };
			// loop over ordered items to add to stats
            foreach (DataRow row in ds.Tables["ShopOrderDetails"].Rows)
            {
                postData.Add("pr" + x.ToString() + "id", Convert.ToString(row["ProductRef"]));
                postData.Add("pr" + x.ToString() + "nm", Convert.ToString(row["ProductName"]));
                postData.Add("pr" + x.ToString() + "ca", "CategoryName");
                postData.Add("pr" + x.ToString() + "br", "BrandName");
                postData.Add("pr" + x.ToString() + "va", "");
                postData.Add("pr" + x.ToString() + "ps", x.ToString());

                x++;
            }
            //  

            var postDataString = postData
                .Aggregate("", (data, next) => string.Format("{0}&{1}={2}", data, next.Key,
                                                             HttpUtility.UrlEncode(next.Value)))
                .TrimEnd('&');

            // set the Content-Length header to the correct value
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);

            // write the request body to the request
            using (var writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postDataString);
            }

            try
            {
                var webResponse = (HttpWebResponse)request.GetResponse();
                // Response.Write(webResponse.StatusCode);
                if (webResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new HttpException((int)webResponse.StatusCode,
                                            "Google Analytics tracking did not return OK 200");

                }
                webResponse.Close();
            }
            catch (Exception ex)
            {
                DoLog(ex.ToString());
            }
        }
        catch (Exception ex)
        {
            DoLog(ex.ToString());
           
        }

 

More Articles to Read

Guide to build your 3.3v power supply
Guide to build your 3.3v power supply
SDR radio breathes life into a 75 year old Marconi CR100
SDR radio breathes life into a 75 year old Marconi CR100
A Time for Ranting!
A Time for Ranting!
Emulate a Commodore 64 keyboard with a modern PC and an Arduino
Emulate a Commodore 64 keyboard with a modern PC and an Arduino
USB2005 and USB97C202 Sharing ATA/ATAPI Drive w/ Another Controller
USB2005 and USB97C202 Sharing ATA/ATAPI Drive w/ Another Controller
Robotic Cat Laser
Robotic Cat Laser
LED traffic light
LED traffic light
Estimating Power for ADSP-BF561 Blackfin® Processors
Estimating Power for ADSP-BF561 Blackfin® Processors
Teach Your Arduino to Switch Itself Off!
Teach Your Arduino to Switch Itself Off!
Control a tracked robot with your mind (or joystick)
Control a tracked robot with your mind (or joystick)

Top




Shares