Click here to Skip to main content
15,893,337 members
Articles / Programming Languages / C#

Server-side Google Analytics Transactions

Rate me:
Please Sign up or sign in to vote.
4.86/5 (10 votes)
21 May 2013CPOL4 min read 42.5K   1.1K   25  
Handling Google Analytics Transactions on server-side using C# intead of JavaScript on client-side.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Middelpat.GoogleAnalytics
{
    public class TransactionItem
    {
        private readonly string _utmt = "item";

        private string _utmtid;     //OrderId
        private string _utmipc;     //Product code
        private string _utmipn;     //Product name
        private string _utmipr;     //Product price (unit price)
        private string _utmiqt;     //Quantity
        private string _utmiva;     //Product category

        /// <summary>
        /// Create a new TransactionItem
        /// </summary>
        /// <param name="productPrice">The unit price of the item</param>
        /// <param name="category">The product category or varition</param>
        public TransactionItem(string productCode, string productName, decimal productPrice, int quantity, string category)
        {
            _utmipc = Uri.EscapeDataString(productCode);
            _utmipn = Uri.EscapeDataString(productName);
            _utmipr = productPrice.ToString("F");
            _utmiqt = quantity.ToString();
            _utmiva = Uri.EscapeDataString(category);
        }

        public string CreateParameterString()
        {
            return string.Format("utmt={0}&utmtid={1}&utmipc={2}&utmipn={3}&utmipr={4}&utmiqt={5}&utmiva={6}",
                                 _utmt,
                                 _utmtid,
                                 _utmipc,
                                 _utmipn,
                                 _utmipr,
                                 _utmiqt,
                                 _utmiva);
        }

        #region get set

        /// <summary>
        /// The orderId will be set automatically when the item is being added to the Transaction
        /// </summary>
        public int OrderId
        {
            set
            {
                _utmtid = value.ToString();
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Netherlands Netherlands
Student software engineer and partime .NET backend developer

Comments and Discussions