Google Analytics server side ecommerce tracking V2

Friday 17 June 2016

Google Tracking

Following a previous post for Google Analytics server side ecommerce tracking , I have now written a new and improved version of the C# functions to send ecommerce order data to Google Analytics server-side using Google’s Measurement Protocol.

Documentation for the Measurement Protocol are available on https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

The new version of the code has fixed an issue where the items purchased were not being saved in Google Analytics and only the order overview was being saved and has been created as a class to allow it to be easily accessed in your code.

The new functions split the data into the ecommerce transaction and then the items purchased which are sent individually.

The functions are in a class called GoogleMeasurementProtocol.cs and this can be referenced with:

GoogleMeasurementProtocol gmp = new GoogleMeasurementProtocol();

Next we create the ecommerce transaction tracking submission:

gmp.SendGoogleTrackingOrder("UA-000000-00", 
        "12345", 
        "0005", 
        "SiteName", 
        "19.99", 
        "5.99", 
        "mysite.co.uk",
        "callback.aspx", 
        "Paypal Payment Complete"
);

Next we loop over the items ordered and send each one to Google Analytics.

foreach (DataRow row in DataTable)
    {
        gmp.SendGoogleTrackingItem(
        "UA-000000-00", 
        "12345", 
        "0005", 
       "ProductName", 
        "12.99", 
        "1", 
        "ProductRef", 
        "POption"
        );
   }

You can download the code from GitHub at https://github.com/briandorey/EcommerceGoogleAnalyticsMeasurementProtocol