Advertisement
Guest User

Untitled

a guest
Sep 15th, 2014
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  producer-tracking.js
  3.  *  Author: Adam Bratt
  4.  *  Version: 0.1
  5.  *  Date: September 15, 2014
  6.  *  Description:
  7.  *
  8.  *  Allows Marketfy.com Mavens to track sales originating from their personal
  9.  *  sites and promotional landing pages.
  10.  *
  11.  */
  12.  
  13. MarketfyToolkit = window.MarketfyToolkit || {};
  14. MarketfyToolkit.version = '0.3.1';
  15.  
  16.  
  17. /**
  18.  * Instantaneously invoked function to wrap tracker script. We bind the
  19.  * tracker creation to the MarketfyToolkit global at the end of the invocation.
  20.  *
  21.  * @param {window} window
  22.  * @param {document} document
  23.  * @param {MarketfyToolkit) toolkit Global instance of our Marketfy Toolkit
  24.  *
  25.  */
  26. !function(window, document, toolkit, undefined) {
  27.  
  28.   // Check to see if already loaded this script
  29.   if (toolkit.sessionTracker)
  30.     return;
  31.  
  32.   /**
  33.    * Marketfy Session Tracker Class for handling all types of session tracking
  34.    * on external partner websites.
  35.    *
  36.    * @param {object} options Settings overrides for the class
  37.    * @constructor
  38.    *
  39.    */
  40.   var Tracker = function (options) {
  41.     this.init(options);
  42.   };
  43.  
  44.   Tracker.prototype = (function () {
  45.  
  46.     // Private static scope
  47.  
  48.     var cls = Tracker;
  49.  
  50.     var defaultSettings = {
  51.       domain: 'marketfy.com',
  52.       trackerEndpoint: '/api/v2/store/track-session/'
  53.     };
  54.  
  55.     var instanceCount = 0;
  56.  
  57.     /**
  58.      * Loads a JSONP script asynchronously
  59.      * @param {string} url URL of the script to load
  60.      * @return {obj} DOM object for generated <script>
  61.      */
  62.     var loadJSONP = function (url) {
  63.       var js = document.createElement('script');
  64.       js.type = 'text/javascript';
  65.       js.async = true;
  66.       js.src = script;
  67.       document.getElementsByTagName('head')[0].appendChild(js);
  68.       return js;
  69.     };
  70.  
  71.     // Public scope
  72.  
  73.     return {
  74.  
  75.       settings: defaultSettings,
  76.  
  77.       /**
  78.        * Initialize the tracker class and start initial tracking
  79.        * @param {options} options Settings to override
  80.        *
  81.        */
  82.       init: function (options) {
  83.  
  84.         // Setup the options
  85.         for(var key in options){
  86.           if(options.hasOwnProperty(key)){
  87.             this.settings[key] = options[key];
  88.           }
  89.         }
  90.  
  91.         this.id = ++instanceCount;
  92.         this.requestCounter = 0;
  93.  
  94.         // Ping the session tracker
  95.         this.ping();
  96.  
  97.       },
  98.  
  99.       /**
  100.        * Pings the session tracking interface on Marketfy.
  101.        * To allow for Cross Domain usage we implement JSONP which requires us
  102.        * to create a new static method for callback on the Tracker class for
  103.        * proper scoping.
  104.        */
  105.       ping: function () {
  106.  
  107.         var callbackJSONP = 'JSONP_' + this.id + '_' + this.requestCounter;
  108.         cls[callbackJSONP] = function (ret) {
  109.           // No callback processing needed for now
  110.         };
  111.  
  112.         this.callback = 'MarketfyToolkit.Tracker.' + callbackJSONP;
  113.         var requestURL = '//' + this.settings.domain + this.settings.trackerEndpoint
  114.           + '?callback=' + this.callback + '&ref_code=' + this.settings.code;
  115.  
  116.         loadJSONP(requestURL);
  117.  
  118.         this.requestCounter++;
  119.       }
  120.  
  121.     }
  122.   })();
  123.  
  124.   toolkit.sessionTracker = Tracker;
  125.   toolkit.startSession = function (options) {
  126.     if (!toolkit.session) {
  127.       toolkit.session = new Tracker(options);
  128.     }
  129.     return toolkit.session;
  130.   }
  131.  
  132.  
  133. }(window, document, MarketfyToolkit);
  134.  
  135. MarketfyToolkit.startSession();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement