Search code examples
javascriptasp.nete-commerceanalytics

Passing server-side object arrays to client on a postback


I'm using Google Analytics to track eCommerce. The checkout page is is dynamic with 3 different views (the final being the confirmation of a successful transaction). At the end of each view, a postback is performed to do server-side calculations, then the next view is displayed.

The information about the sale is saved in an array of Product objects and contains (for the most part) all of the information I would like to pass Google Analytics. The problem is that I need to get everything from this object array to the client so I can loop through it in javascript and pass it to Google.

What would be the simplest way to get this data from the server to the client for javascript processing? The data within the object array is not final until the 2nd postback and is destroyed after the 3rd postback.


Solution

  • I'm not sure I fully understand the situation you're asking about, but the simplest way to get data from server to client is to request the data from the client and have the server return a JSON formatted block of data (often an array or object). Since JSON is just javascript, the client can do a JSON-safe eval on the returned data and instantly have a javascript data structure that represents all the data. Depending upon what library you are using on client/server, there are usually JSON helper functions built-in to the library to make both ends even easier (though writing the code from scratch is easy too).