﻿// Autor: Emilio Alvarez
// Copyright: Pukka's Webs Design
// Versió 1.2.1
// Data: 25/11/2011

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26059141-1']);
_gaq.push(['_setDomainName', '.printandgo.com']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


$(document).ready(function () {

    var urlPath = window.location.pathname;

    $('.gaVirtualURL').live('click', function () {
        if ($(this).attr("gaVirtualURL") != "") {
            var virtualUrl = '/virtual-url/' + $(this).attr('gaVirtualURL') + "/source=" + urlPath;
        } else if ($(this).attr("href") != "") {
            var virtualUrl = $(this).attr('href') + "/source=" + urlPath;
        }
        if (virtualUrl != "") {
            _gaq.push(['_trackPageview', virtualUrl]);
        }
    });

    $('.gaVirtualURL').live('submit', function () {
        if ($(this).attr("gaVirtualURL")) {
            var virtualUrl = '/virtual-url/' + $(this).attr('gaVirtualURL') + "/source=" + urlPath;
            _gaq.push(['_trackPageview', virtualUrl]);
        }
    });

});

function trackEcommerce(generalInfo, products) {
    if (generalInfo != null) {
        _gaq.push(['_addTrans',
		  generalInfo.orderId,           // order ID - required
		  generalInfo.storeName, 		 // affiliation or store name
		  generalInfo.total,          // total - required
		  generalInfo.tax,               // tax
		  generalInfo.shipping,           // shipping
		  generalInfo.city, 	         // city
		  generalInfo.province,          // state or province
		  generalInfo.country            // country
		]);

        for (var i = 0; i < products.length; i++) {
            product = products[i];
            _gaq.push(['_addItem',
			  product.orderId,           // order ID - required
			  product.skuCode,           // SKU/code
			  product.productName,       // product name
			  product.category,          // category or variation
			  product.unitPrice,          // unit price - required
			  produc.quantit              // quantity - required
			]);
        }
        _gaq.push(['_trackTrans']);
    }
}
