Ecommerce Tracking
is available for both web and app properties. Setting up Ecommerce
Tracking is a two step process: enable Ecommerce Tracking in an account
profile and add Ecommerce Tracking code to your web or app source
code. Step 2 requires additional technical set up and access to your
site or app source code.
ASP.Net Implementation in ASPX.cs file
private void GAEcommerseTrackingCode()
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("<script type=\"text/javascript\">");
builder.AppendLine("\tvar _gaq = _gaq || [];");
builder.AppendLine(string.Format("\t_gaq.push(['_setAccount', '{0}']);", "UA-17691872-1"));
builder.AppendLine(string.Format("\t_gaq.push(['_trackPageview']);"));
string companyName = "Test";
if (companyName.Length <= 0)
{
companyName = txtBillingFirstName.Text.Trim() + " " + txtBillingFirstName.Text.Trim();
}
builder.AppendLine(string.Format("\t_gaq.push(['_addTrans','" + Convert.ToString(objCheckout.OrderNo) + "', '" + companyName + "','" + objCheckout.GrandTotalAmount + "','" + objCheckout.SaleTax + "','" + objCheckout.ShippingCharge + "','" + objCheckout.BillingCity + "','" + objCheckout.BillingState + "','" + objCheckout.BillingCountry + "']);"));
if (objCartDetail.MemberId != 0)
{
DataSet dsCartDetail = objCheckout.GetOrderDetail(Convert.ToString(objCheckout.OrderNo));
if (dsCartDetail.Tables[1].Rows.Count > 0)
{
foreach (DataRow dr in dsCartDetail.Tables[1].Rows)
{
builder.AppendLine(string.Format("\t_gaq.push(['_addItem','" + Convert.ToString(objCheckout.OrderNo) + "','" + dr["PRODUCTID"].ToString() + "', '" + dr["PRODUCTNAME"].ToString() + "','C','" + dr["UNITPRICE"].ToString().Replace("0000","00") + "','" + dr["PURCHASEQUANTITY"].ToString() + "']);"));
}
}
}
builder.AppendLine(string.Format("\t_gaq.push(['_trackTrans']);"));
builder.AppendLine("(function() {");
builder.AppendLine("\tvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
builder.AppendLine("\tga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
builder.AppendLine("\tvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
builder.AppendLine("})();");
builder.AppendLine("</script>");
((Literal)Master.FindControl("litGAEcommerseCode")).Text = builder.ToString();
}
Use this litGAEcommerseCode litral control either in header or in Master page<asp:Literal id="litGAEcommerseCode" runat="server"></asp:Literal>
For more details you can follow this URL, here you can find the complete details of HTML code also
developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce
Step 1: Enable Ecommerce Tracking
- Click the Admin tab at the top right of any screen in Analytics. If you are not already on the Account Administration screen, click the All Accounts link at top left, just below the menu bar.
- Click the name of the account and then the name of the property that has the profile you want to enable Ecommerce Tracking for. If you have a lot of accounts or properties, use the drop down search box in the top left of the menu bar and type to search.
- Use the Profile drop down menu to select the profile you want.
- Click the Profile Settings tab.
- Under the E-Commerce Settings section, select Yes, an E-Commerce Site/App.
- Click Apply.
ASP.Net Implementation in ASPX.cs file
private void GAEcommerseTrackingCode()
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("<script type=\"text/javascript\">");
builder.AppendLine("\tvar _gaq = _gaq || [];");
builder.AppendLine(string.Format("\t_gaq.push(['_setAccount', '{0}']);", "UA-17691872-1"));
builder.AppendLine(string.Format("\t_gaq.push(['_trackPageview']);"));
string companyName = "Test";
if (companyName.Length <= 0)
{
companyName = txtBillingFirstName.Text.Trim() + " " + txtBillingFirstName.Text.Trim();
}
builder.AppendLine(string.Format("\t_gaq.push(['_addTrans','" + Convert.ToString(objCheckout.OrderNo) + "', '" + companyName + "','" + objCheckout.GrandTotalAmount + "','" + objCheckout.SaleTax + "','" + objCheckout.ShippingCharge + "','" + objCheckout.BillingCity + "','" + objCheckout.BillingState + "','" + objCheckout.BillingCountry + "']);"));
if (objCartDetail.MemberId != 0)
{
DataSet dsCartDetail = objCheckout.GetOrderDetail(Convert.ToString(objCheckout.OrderNo));
if (dsCartDetail.Tables[1].Rows.Count > 0)
{
foreach (DataRow dr in dsCartDetail.Tables[1].Rows)
{
builder.AppendLine(string.Format("\t_gaq.push(['_addItem','" + Convert.ToString(objCheckout.OrderNo) + "','" + dr["PRODUCTID"].ToString() + "', '" + dr["PRODUCTNAME"].ToString() + "','C','" + dr["UNITPRICE"].ToString().Replace("0000","00") + "','" + dr["PURCHASEQUANTITY"].ToString() + "']);"));
}
}
}
builder.AppendLine(string.Format("\t_gaq.push(['_trackTrans']);"));
builder.AppendLine("(function() {");
builder.AppendLine("\tvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
builder.AppendLine("\tga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
builder.AppendLine("\tvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
builder.AppendLine("})();");
builder.AppendLine("</script>");
((Literal)Master.FindControl("litGAEcommerseCode")).Text = builder.ToString();
}
Use this litGAEcommerseCode litral control either in header or in Master page<asp:Literal id="litGAEcommerseCode" runat="server"></asp:Literal>
For more details you can follow this URL, here you can find the complete details of HTML code also
developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce