// Javascript Class for google analytics.
/*
<html>
    <head>
        <title>Google Analytics</title>
        <script src="/assets/js/analytics.js"></script>

        <script type="text/javascript">
            var site_analytics = new Analytics('UA-25853725-1'); // Propeller
            
            site_analytics.track_page();
        </script>
    </head>
    <body>
        <h1>Google Analytics</h1>
        <a href="http://code.google.com/apis/analytics/docs/" target="_blank" onclick="site_analytics.track_event('Outbound Link', 'Google Home Page', 'http://code.google.com/apis/analytics/docs/');" >Read Developer Docs</a>

        <script type="text/javascript">
            site_analytics.add_script();
        </script>
    </body>
</html>
*/

// Future Development: Cross-Domain Tracking, Visitor Configuration(session time out, sample rate, custom variables, custom segments), Traffic Sources (Organic Campaigns)

// Setup global google analytics queue.
// The commands in the queue are only executed once google's
// analytics script is loaded using the add_script() method.
var _gaq = _gaq || [];

// When creating an analytics object you can optionally list one or more account ids.
// e.g. var site_analytics = new Analytics('UA-1234567-1', 'UA-7654321-1');
function Analytics() {
    var _accounts = arguments;

    // Loads the google analytics script.
    // The method can be called anywhere on the page but 
    // google recommends to output it just before the closing body tag
    // to ensure most of the page is loaded before tracking begins.
    this.add_script = 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);
    }

    // Adds additional accounts to the analytics object.
    // You can list one or more account ids.
    // e.g. site_analytics.add_account('UA-1234567-1', 'UA-7654321-1');
    this.add_account = function(account) {
        for (index in arguments) {
            _accounts.push(arguments[index]);
        }
    }

    // Track a real or virtual page view.
    // You should only use this method for virtual pages when you have to setup a goal. 
    // This is because virtual page views will add to your total page view count.
    // It's recommended that you use the track_event method for virtual page views that don't need a goal setup.
    // If you don't pass a uri the current request is used.
    this.track_page = function(page) {
        for (index in _accounts) {
            _gaq.push(
                ['_setAccount', _accounts[index]],
                ['_trackPageview', page]
            );
        }
    }

    // Track an event/action that occurs on the page.
    // Parameters:
        // Category = A way to group a set of actions/events.
        // Action = The event/action that took place.
        // Label = A short bit of text that uniquely identifies the event/action.
        // Value [Optional] = A value to associate with the event/action. This could be a quantity, no of views, price. These values are displayed as totals and averages in analytics.
    // Examples:
        //site_analytics.track_event('Facebook', 'View Wall', 'www.facebook.com/apple');
        //site_analytics.track_event('Outbound Link', 'View iPhone 5', 'http://www.apple.com/iphone');
        //site_analytics.track_event('Gallery', 'Play Video', 'Introducing the iPhone 5');
        //site_analytics.track_event('Gallery', 'Viewed Full Size Picture', 'iPhone 5', $duration);
        //site_analytics.track_event('Basket', 'Add To Basket', 'iPhone 5', $quantity);
    this.track_event = function(category, action, label, value) {
        for (index in _accounts) {
            _gaq.push(
                ['_setAccount', _accounts[index]]
                ['_trackEvent', category, action, label, value]  
            ); 
        }
    }
    
    // Track a social event/action that occurs on the page.
    // Parameters:
        // network = The network on which the action occurs (e.g. Facebook, Twitter)
        // social_action = The type of action that happens (e.g. Like, Send, Tweet).
        // opt_target [Optional] = The text value that indicates the subject of the action; most typically this is a URL target. If undefined, defaults to document.location.href.
        // opt_page_path [Optional] = The page (by path, not full URL) from which the action occurred. If undefined, defaults to document.location.pathname plus document.location.search. You will only need to supply a value for this string if you use virtual (or custom) page paths for Analytic reporting purposes.
    // Examples:
        //site_analytics.track_event('facebook', 'like', 'http://www.apple.com/iphone');
    this.track_social = function(network, social_action, opt_target, opt_page_path) {
        for (index in _accounts) {
            _gaq.push(
                ['_setAccount', _accounts[index]]
                ['_trackSocial', network, social_action, opt_target, opt_page_path]  
            ); 
        }
    }
    
    // Creates a new transaction.
    // Duplicate transactions will overwrite existing transactions in the session.
    // The order ID and total are required.
    // To use this method make sure e-commerce tracking is enabled in the google analytic accounts.
    // Arguments for this method are matched by position, so be sure to supply all parameters, even if some of them have an empty value ('').
    this.add_trans = function(order_id, affiliation_or_store, total, tax, shipping, city, county_or_state, country) {
        for (index in _accounts) {
            _gaq.push(
                ['_setAccount', _accounts[index]]
                ['_addTrans', order_id, affiliation_or_store, total, tax, shipping, city, county_or_state, country]  
            ); 
        }
    }
    
    // Track purchased items.
    // This method tracks individual items by their SKU. It's recommended that the SKU is unique.
    // An item with the same SKU will overwrite the existing item in the session. Due to this, it's recommended that items are first grouped by SKU.
    // If VAT/Tax is involved, specify the gross price unless told otherwise.
    // The SKU, unit price and quantity are required.
    // It's recommended that you call the add_trans() method first to create a new transaction.
    // Items added are associated with a transaction via the order ID argument.
    // If you add an item without a transaction then google analytics will show products by SKU that are not associated with any transaction.
    // While the name parameter is not required, items added to a transaction without a name parameter do not appear in the product breakdown for a transaction.
    // While you will still see the total revenue for the transaction, you will not be able to see how much revenue a particular item contributed to the transaction total.
    // To use this method make sure e-commerce tracking is enabled in the google analytic accounts.
    // Arguments for this method are matched by position, so be sure to supply all parameters, even if some of them have an empty value ('').
    this.add_item = function(order_id, sku_or_code, name, category, price, quantity) {
        for (index in _accounts) {
            _gaq.push(
                ['_setAccount', _accounts[index]]
                ['_addItem', order_id, sku_or_code, name, category, price, quantity]  
            ); 
        }
    }
    
    // Sends transaction and item data to google analytics.
    // It's you only call this method once you have confirmed a transaction has been successfully completed. Usually this is done on the confirmation or receipt page.
    // It's 'recommended you call the track_page() method to associate a transaction with the confirmation or receipt page.
    // This method should be called after the items and transaction elements have been set up.
    // To use this method make sure e-commerce tracking is enabled in the google analytic accounts.
    this.track_trans = function() {
        for (index in _accounts) {
            _gaq.push(
                ['_setAccount', _accounts[index]]
                ['_trackTrans']
            ); 
        }
    }
}
