views:

1085

answers:

4

I've been asked to add Google e-commerce tracking into my site. This tracking involves inserting some javascript on your receipt page and then calling it's functions. From my asp.net receipt page, I need to call one function (_addTrans) for the transaction info and then another (_addItem) for each item on the order. An example of what they want is here

This is for a 1.1 site. Can anybody give me a jumpstart on calling these two functions from my c# code-behind? I can't imagine that I'm alone out there in needing to call Google e-commerce tracking, so I'm hopeful.

+5  A: 

Probably the easiest way is to build up the required Javascript as a string with something like

StringBuilder sb = new StringBuilder()
sb.AppendLine( "<script>" );
sb.AppendLine( "var pageTracker = _gat._getTracker('UA-XXXXX-1');" );
sb.AppendLine( "pageTracker._trackPageview();" );
sb.AppendFormat( "pageTracker._addTrans('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}' );\n", orderId, affiliation, total, tax, shipping, city, state, country );
sb.AppendFormat( "pageTracker._addItem('{0}','{1}','{2}','{3}','{4}','{5}');\n", itemNumber, sku, productName, category, price, quantity );
sb.AppendLine("pageTracker._trackTrans();");
sb.AppendLine( "</script>" );

Then register it to appear in the page with

Page.RegisterStartupScript("someKey", sb.ToString());
stevemegson
Note also that Page.RegisterStartupScript is obsolete according to MSDN, which recommends using ClientScriptManager.RegisterStartupScript instead.
Liam
Yes, you are correct. However you have to use Page.RegisterStartupScript in ASP.NET 1.1
Scott
+3  A: 

Here i just wrote an Google Analytics E-Commerce class to dynamically add analytics transactions.

http://www.sarin.mobi/2008/11/generate-google-analytics-e-commerce-code-from-c/

Hope this hope.

That is fantastic and exactly what I was just starting to write. Much appreciated!
Scott
+1  A: 

In response to stevemegson (first answer) - shouldn't the first parameter into the pageTracker._addItem method be the OrderID, not the itemNumber?

yes, you are correct. Should be the orderID in the first parm.
Scott
+1  A: 

A project i have released allows for easy integration with Google Analytics to fire page views and events through native .net code.

This way you can simply call a method that will log either and event or a page view for you.

I am planning on supporting transaction logging as well over the next few weeks.

It's called GaDotNet and can be found here: http://www.diaryofaninja.com/projects/details/ga-dot-net

Doug