﻿// Registers the Google Analytics scripts.
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

// Provides methods for working with the Google Analytics api.
GoogleAnalytics =
{
    runTracker: function(accountId, pageViews, bookings) {
        /// <summary>
        /// Runs the tracker associated with the account id and
        /// registers the page views in the pageViews-array and the bookings
        /// in the bookings-array.
        /// </summary>

        var tracker = this.getTracker(accountId);
        if (!tracker) return;

        if (pageViews && pageViews.length > 0) {
            for (var i = 0; i < pageViews.length; i++) {
                var pageName = pageViews[0];

                if (pageName && pageName.length > 0) {
                    tracker._trackPageview(pageName);
                }
                else {
                    tracker._trackPageview();
                }
            }
        }

        if (bookings && bookings.length > 0) {
            for (var i = 0; i < bookings.length; i++) {
                var booking = bookings[i];
                tracker._addTrans(booking.bookingId, "" /*Affiliation*/, booking.totalPrice, "" /*Tax*/, "" /*Shipping*/, booking.customerCity, "" /*State*/, booking.customerCountry);

                if (booking.products) {
                    for (var i = 0; i < booking.products.length; i++) {
                        var p = booking.products[i];
                        tracker._addItem(booking.bookingId, "" /*SKU*/, p.productCode, p.productCategory, p.price, p.quantity);
                    }
                }

                tracker._trackTrans();
            }
        }
    },

    getTracker: function(accountId) {
        if (typeof (_gat) === "undefined") return null;
        if (!accountId) throw { message: "The accountId must be specified when getting tracker." };

        var tracker = _gat._getTracker(accountId);
        tracker._setDomainName("none");
        tracker._setAllowLinker(true);
        tracker._initData();

        return tracker;
    }
};
