View Full Version : Adding Javascript to a Form - an 'Add To Cart' Button

09-02-2010, 06:28 PM
Good morning! Hopefully somebody can help me with this javascript question. (No knowledge of Google Anayltics needed for this.) I'm adding Google Analytics 'Event Tracking' javascript to a typical 'Add to Cart' button. The customer enters desired quantity in the text box and clicks the 'Add to Cart' button to order. I would like to use the Event Tracking javascript to record the button click - plus the Value entered in the text box - as one event. Here's the code for the form (the text box and 'Add to Cart' button code lines are bold):

<form method="post" action="http://www.yoursite.com/mm5/merchant.mvc?">
<input type="hidden" name="Screen" value="BASK" />
<input type="hidden" name="Product_Code" value="yourproductcode" />
<input type="hidden" name="Action" value="ADPR" />
<label for="quantity">Quantity:</label>
<input type="text" name="quantity" value="0" />
<input type="submit" value="Add To Cart" />

I came up with this javascript for the Event Tracking (to be inserted somewhere above):

onClick="_gaq.push(['_trackEvent', 'OrderForms', 'AddToCart', 'yourproductcode', document.getElementByName('quantity').value]);"

I'm wondering where I would enter this javacript code inside the form code to pick up both the button click - plus the quantity? You'll notice that I'm using 'getElementByName' to extract 'quantity' from the text box and record its 'value'. If I enter this javascript on the 'Add to Cart' line - right after "Add to Cart", would 'quantity' still get picked up upon button-click - since it's all on the same form? Or would it be a problem because 'quantity' is contained in a different <input> container?

Thanks! Any help is greatly appreciated.