|
| 1 | +(function(apiKey) { |
| 2 | + // stub mParticle and major mParticle classes EventType, eCommerce, and Identity to exist before full |
| 3 | + // mParticle object is initialized |
| 4 | + window.mParticle = window.mParticle || {}; |
| 5 | + window.mParticle.EventType = { |
| 6 | + Unknown: 0, |
| 7 | + Navigation: 1, |
| 8 | + Location: 2, |
| 9 | + Search: 3, |
| 10 | + Transaction: 4, |
| 11 | + UserContent: 5, |
| 12 | + UserPreference: 6, |
| 13 | + Social: 7, |
| 14 | + Other: 8, |
| 15 | + Media: 9, |
| 16 | + }; |
| 17 | + window.mParticle.eCommerce = { Cart: {} }; |
| 18 | + window.mParticle.Identity = {}; |
| 19 | + window.mParticle.Rokt = {}; |
| 20 | + window.mParticle.config = window.mParticle.config || {}; |
| 21 | + window.mParticle.config.rq = []; |
| 22 | + window.mParticle.config.snippetVersion = 2.6; |
| 23 | + window.mParticle.ready = function(f) { |
| 24 | + window.mParticle.config.rq.push(f); |
| 25 | + }; |
| 26 | + |
| 27 | + // methods to be stubbed from the main mParticle object, mParticle.eCommerce, mParticle.Identity, and mParticle.Rokt |
| 28 | + // methods that return objects are not stubbed |
| 29 | + var mainMethods = [ |
| 30 | + 'endSession', |
| 31 | + 'logError', |
| 32 | + 'logBaseEvent', |
| 33 | + 'logEvent', |
| 34 | + 'logForm', |
| 35 | + 'logLink', |
| 36 | + 'logPageView', |
| 37 | + 'setSessionAttribute', |
| 38 | + 'setAppName', |
| 39 | + 'setAppVersion', |
| 40 | + 'setOptOut', |
| 41 | + 'setPosition', |
| 42 | + 'startNewSession', |
| 43 | + 'startTrackingLocation', |
| 44 | + 'stopTrackingLocation', |
| 45 | + ]; |
| 46 | + var ecommerceMethods = ['setCurrencyCode', 'logCheckout']; |
| 47 | + var identityMethods = ['identify', 'login', 'logout', 'modify']; |
| 48 | + var roktMethods = [ |
| 49 | + 'selectPlacements', |
| 50 | + 'hashAttributes', |
| 51 | + 'setExtensionData', |
| 52 | + ]; |
| 53 | + |
| 54 | + // iterates through methods above to create stubs |
| 55 | + mainMethods.forEach(function(method) { |
| 56 | + window.mParticle[method] = preloadMethod(method); |
| 57 | + }); |
| 58 | + ecommerceMethods.forEach(function(method) { |
| 59 | + window.mParticle.eCommerce[method] = preloadMethod(method, 'eCommerce'); |
| 60 | + }); |
| 61 | + identityMethods.forEach(function(method) { |
| 62 | + window.mParticle.Identity[method] = preloadMethod(method, 'Identity'); |
| 63 | + }); |
| 64 | + roktMethods.forEach(function(method) { |
| 65 | + window.mParticle.Rokt[method] = preloadMethod(method, 'Rokt'); |
| 66 | + }); |
| 67 | + |
| 68 | + // stubbing function |
| 69 | + // pushes an array of 2 arguments into readyQueue: 1. the method, and 2. the arguments passed to the method |
| 70 | + // if the method is on the eCommerce, identity, or rokt object, then the 1st argument is base concatenated with "." and the method name |
| 71 | + // ie: Identity.login, eCommerce.setCurrencyCode, Rokt.selectPlacements |
| 72 | + // in main.js, the function "processPreloadedItem" will parse and run stubbed methods stored in the readyQueue (config.rq) |
| 73 | + function preloadMethod(method, base) { |
| 74 | + return function() { |
| 75 | + if (base) { |
| 76 | + method = base + '.' + method; |
| 77 | + } |
| 78 | + var args = Array.prototype.slice.call(arguments); |
| 79 | + args.unshift(method); |
| 80 | + window.mParticle.config.rq.push(args); |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + // set data planning query parameters |
| 85 | + var config = window.mParticle.config, |
| 86 | + env = config.isDevelopmentMode ? 1 : 0, |
| 87 | + dbUrl = '?env=' + env, |
| 88 | + dataPlan = config.dataPlan; |
| 89 | + |
| 90 | + if (dataPlan) { |
| 91 | + var dpId = dataPlan.planId, |
| 92 | + dpV = dataPlan.planVersion; |
| 93 | + if (dpId) { |
| 94 | + if (dpV && (dpV < 1 || dpV > 1000)) { |
| 95 | + dpV = null; |
| 96 | + } |
| 97 | + dbUrl += '&plan_id=' + dpId + (dpV ? '&plan_version=' + dpV : ''); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // set version query parameters |
| 102 | + var versions = config.versions; |
| 103 | + var versionQueryArray = []; |
| 104 | + if (versions) { |
| 105 | + Object.keys(versions).forEach(function(name) { |
| 106 | + versionQueryArray.push(name + '=' + versions[name]); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + // add Rokt script dynamically to the page, insert before the first script tag |
| 111 | + var script = document.createElement('script'); |
| 112 | + script.type = 'text/javascript'; |
| 113 | + script.async = true; |
| 114 | + window.ROKT_DOMAIN = ROKT_DOMAIN || 'https://apps.rokt-api.com'; |
| 115 | + window.mParticle.config.domain = ROKT_DOMAIN.split('//')[1]; |
| 116 | + script.src = |
| 117 | + ROKT_DOMAIN + |
| 118 | + '/js/v2/' + |
| 119 | + apiKey + |
| 120 | + '/app.js' + |
| 121 | + dbUrl + |
| 122 | + '&' + |
| 123 | + versionQueryArray.join('&'); |
| 124 | + var firstScript = document.getElementsByTagName('script')[0]; |
| 125 | + firstScript.parentNode.insertBefore(script, firstScript); |
| 126 | +})('REPLACE WITH API KEY'); |
0 commit comments