dynamically building the Google e-commerce javascript from C#

J

James Irvine

I'm trying to dynamically build the example script below from C#, substituting the sale item details with data values I get from an online transaction. Is
there a preferred method to do this? My approach so far has been to concatenate a bunch of strings, then plug it into a ClientScript.RegisterStartupScript,
like this:


string line1 = @"<script type=""text/javascript"">";
string line2 = @"var pageTracker = _gat._getTracker(""UA-2263877-1"");";
string line3 = " pageTracker._initData(); pageTracker._trackPageview(); pageTracker._addTrans(";
etc...

string gxAnalyticsScriptx = line1 + line2 + line3;

ClientScript.RegisterStartupScript(this.GetType(), "whatever", gxAnalyticsScriptx);




But with all the embedded quotes and all to deal with, it's getting messy real quick. Is there some more standard way to get this done? thanks -James








This is the example script I'm trying to dynamically build, substituting item details from my shopping cart:

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXX-1");
pageTracker._initData();
pageTracker._trackPageview();

pageTracker._addTrans(
"1234", // Order ID
"Mountain View", // Affiliation
"11.99", // Total
"1.29", // Tax
"5", // Shipping
"San Jose", // City
"California", // State
"USA" // Country
);

pageTracker._addItem(
"1234", // Order ID
"DD44", // SKU
"T-Shirt", // Product Name
"Green Medium", // Category
"11.99", // Price
"1" // Quantity
);

pageTracker._trackTrans();
</script>
 
M

Mufaka

You can create an ascx for this. Your ascx can have properties for the
values you want to set in the script. Then use the property in your code
in front like the following:

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<script type="text/javascript">

....

pageTracker._addTrans(
"<%=OrderID%>", // <-- get the property value

....
</script>

The code behind for the control just has a standard property on it, in
this case 'OrderID'. You can add the others.

You can drop the control on a page and then set the properties in the
page code behind.

protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1.OrderID = "12345-A";
}
 
Top