Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * producer-tracking.js
- * Author: Adam Bratt
- * Version: 0.1
- * Date: September 15, 2014
- * Description:
- *
- * Allows Marketfy.com Mavens to track sales originating from their personal
- * sites and promotional landing pages.
- *
- */
- MarketfyToolkit = window.MarketfyToolkit || {};
- MarketfyToolkit.version = '0.3.1';
- /**
- * Instantaneously invoked function to wrap tracker script. We bind the
- * tracker creation to the MarketfyToolkit global at the end of the invocation.
- *
- * @param {window} window
- * @param {document} document
- * @param {MarketfyToolkit) toolkit Global instance of our Marketfy Toolkit
- *
- */
- !function(window, document, toolkit, undefined) {
- // Check to see if already loaded this script
- if (toolkit.sessionTracker)
- return;
- /**
- * Marketfy Session Tracker Class for handling all types of session tracking
- * on external partner websites.
- *
- * @param {object} options Settings overrides for the class
- * @constructor
- *
- */
- var Tracker = function (options) {
- this.init(options);
- };
- Tracker.prototype = (function () {
- // Private static scope
- var cls = Tracker;
- var defaultSettings = {
- domain: 'marketfy.com',
- trackerEndpoint: '/api/v2/store/track-session/'
- };
- var instanceCount = 0;
- /**
- * Loads a JSONP script asynchronously
- * @param {string} url URL of the script to load
- * @return {obj} DOM object for generated <script>
- */
- var loadJSONP = function (url) {
- var js = document.createElement('script');
- js.type = 'text/javascript';
- js.async = true;
- js.src = script;
- document.getElementsByTagName('head')[0].appendChild(js);
- return js;
- };
- // Public scope
- return {
- settings: defaultSettings,
- /**
- * Initialize the tracker class and start initial tracking
- * @param {options} options Settings to override
- *
- */
- init: function (options) {
- // Setup the options
- for(var key in options){
- if(options.hasOwnProperty(key)){
- this.settings[key] = options[key];
- }
- }
- this.id = ++instanceCount;
- this.requestCounter = 0;
- // Ping the session tracker
- this.ping();
- },
- /**
- * Pings the session tracking interface on Marketfy.
- * To allow for Cross Domain usage we implement JSONP which requires us
- * to create a new static method for callback on the Tracker class for
- * proper scoping.
- */
- ping: function () {
- var callbackJSONP = 'JSONP_' + this.id + '_' + this.requestCounter;
- cls[callbackJSONP] = function (ret) {
- // No callback processing needed for now
- };
- this.callback = 'MarketfyToolkit.Tracker.' + callbackJSONP;
- var requestURL = '//' + this.settings.domain + this.settings.trackerEndpoint
- + '?callback=' + this.callback + '&ref_code=' + this.settings.code;
- loadJSONP(requestURL);
- this.requestCounter++;
- }
- }
- })();
- toolkit.sessionTracker = Tracker;
- toolkit.startSession = function (options) {
- if (!toolkit.session) {
- toolkit.session = new Tracker(options);
- }
- return toolkit.session;
- }
- }(window, document, MarketfyToolkit);
- MarketfyToolkit.startSession();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement