Google Analytics: How do I pull all these values?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
William_firefold
Commander (CMDR)
Commander (CMDR)
Posts: 186
Joined: Fri Aug 01, 2008 8:38 am

Google Analytics: How do I pull all these values?

Post by William_firefold » Thu Jun 11, 2009 7:08 am

We want to use a more complete analytics script on our goal page( ReceiptPage.ascx ), but I am pretty lost trying to put all of these values together in one place.
The script requires values, such as taxes, which are not present on the order object alone. On this page, taxes are pulled through a separate control( OrderTotalSummary.ascx ), and I dont know how to access these values from a separate codefile.

The second problem I could use some help with is how to iteratively insert javascript with order item information inserted each cycle.

This is the code that needs to be used.

Code: Select all

<script language="javascript" type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-1");
pageTracker._trackPageview();
pageTracker._addTrans(
      "order-id", // required
      "affiliate or store name",
      "total",
      "tax",
      "shipping",
      "city",
      "state",
      "country"
); 
pageTracker._addItem(
      "order-id", // required
      "SKU",
      "product name",
      "product category",
      "unit price",  // required
      "quantity"  //required
);
pageTracker._trackTrans();
</script>
I know how to get the values for order-id and a few similar things, but taxes, shipping, city, etc. cant be pulled directly from the order object.
For the second part, i dont know where to begin. Do I write a for loop in the codebehind and somehow inject the code? Do i use a javascript loop and pull the values sequentially for each item? Can C# and JS work together like this?

Any Advice would be appreciated.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Google Analytics: How do I pull all these values?

Post by mazhar » Thu Jun 11, 2009 8:28 am

You can try following to find out tax amount for order

Code: Select all

LSDecimal taxAmount = order.Items.TotalPrice(new OrderItemType[] { OrderItemType.Tax });
shipping amount for order

Code: Select all

LSDecimal shipping = order.Items.TotalPrice(new OrderItemType[] { OrderItemType.Shipping });
For city information, if you want to use billing address information then you can access it directly via order object as below

Code: Select all

order.BillToCity;
If you want to use shipping address then you would need to iterate over shipments and fetch the information via order shipments

Code: Select all

foreach(OrderShipment orderShipmetn in order.Shipments)
orderShipmetn.ShipToCity;

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Google Analytics: How do I pull all these values?

Post by mazhar » Thu Jun 11, 2009 8:43 am

In order to build javascript you need to write some C# that in turn generates javascript code for you for example you can create a method to build javascript as below

Code: Select all

    protected void BuildJS(Order order) 
    {
        System.Text.StringBuilder javaScript = new System.Text.StringBuilder();

        javaScript.Append("var pageTracker = _gat._getTracker('UA-xxxxxx-1');");
        javaScript.Append("pageTracker._trackPageview();");
        javaScript.Append(string.Format("pageTracker._addTrans('{0}','{1}','{2}','{3}','4','{5},'{6}','{}7');", order.OrderId, Token.Instance.Store.Name, order.TotalCharges, order.Items.TotalPrice(new OrderItemType[] { OrderItemType.Tax }), order.Items.TotalPrice(new OrderItemType[] { OrderItemType.Shipping }),order.BillToCity,order.BillToProvince,order.BillToCountry));
        foreach (OrderItem orderItem in order.Items)
            if(orderItem.OrderItemType == OrderItemType.Product)
                javaScript.Append(string.Format("pageTracker._addItem('{0},'{1}','{2}','{3}','{4}','{5}');",orderItem.OrderId,orderItem.Sku,orderItem.Name,orderItem.Product.Categories[0],orderItem.Price,orderItem.Quantity));
        return javaScript.ToString();
    }
Then you need to pass it the order object when ever its available and output javascript to page as below code

Code: Select all

string javaScript = BuildJS(order);
Page.RegisterClientScriptBlock("trackingscript", javaScript);

Post Reply