|
| 1 | +(function() { |
| 2 | + // Create a dummy analytics object until real loaded |
| 3 | + window.analytics || (window.analytics = []); |
| 4 | + window.analytics.methods = [ |
| 5 | + 'identify', |
| 6 | + 'track', |
| 7 | + 'trackLink', |
| 8 | + 'trackForm', |
| 9 | + 'trackClick', |
| 10 | + 'trackSubmit', |
| 11 | + 'page', |
| 12 | + 'pageview', |
| 13 | + 'ab', |
| 14 | + 'alias', |
| 15 | + 'ready', |
| 16 | + 'group', |
| 17 | + 'on', |
| 18 | + 'once', |
| 19 | + 'off' |
| 20 | + ]; |
| 21 | + window.analytics.factory = function(method) { |
| 22 | + return function() { |
| 23 | + var args = Array.prototype.slice.call(arguments); |
| 24 | + args.unshift(method); |
| 25 | + window.analytics.push(args); |
| 26 | + return window.analytics; |
| 27 | + }; |
| 28 | + }; |
| 29 | + for (var i = 0; i < window.analytics.methods.length; i++) { |
| 30 | + var method = window.analytics.methods[i]; |
| 31 | + window.analytics[method] = window.analytics.factory(method); |
| 32 | + } |
| 33 | + |
| 34 | + // Load analytics async |
| 35 | + analytics.load = function(callback) { |
| 36 | + if (document.getElementById('dreamdata-analytics')) return; |
| 37 | + |
| 38 | + // We make a copy if our dummy object |
| 39 | + window.a = window.analytics; |
| 40 | + var script = document.createElement('script'); |
| 41 | + script.async = true; |
| 42 | + script.id = 'dreamdata-analytics'; |
| 43 | + script.type = 'text/javascript'; |
| 44 | + script.src = |
| 45 | + ('https:' === document.location.protocol ? 'https://' : 'http://') + |
| 46 | + 'cdn.dreamdata.cloud/scripts/analytics/v1/dreamdata.min.js'; |
| 47 | + if (script.addEventListener) { |
| 48 | + script.addEventListener( |
| 49 | + 'load', |
| 50 | + function(e) { |
| 51 | + if (typeof callback === 'function') { |
| 52 | + callback(e); |
| 53 | + } |
| 54 | + }, |
| 55 | + false |
| 56 | + ); |
| 57 | + } else { |
| 58 | + //IE8 |
| 59 | + script.onreadystatechange = function() { |
| 60 | + if (this.readyState == 'complete' || this.readyState == 'loaded') { |
| 61 | + callback(window.event); |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | + var first = document.getElementsByTagName('script')[0]; |
| 66 | + first.parentNode.insertBefore(script, first); |
| 67 | + }; |
| 68 | + |
| 69 | + analytics.load(function() { |
| 70 | + // On load init our integrations |
| 71 | + analytics.initialize({ 'Dreamdata.io': { apiKey: '<API_KEY>' } }); |
| 72 | + // Now copy whatever we applied to our dummy object to the real analytics |
| 73 | + while (window.a.length > 0) { |
| 74 | + var item = window.a.shift(); |
| 75 | + var method = item.shift(); |
| 76 | + if (analytics[method]) analytics[method].apply(analytics, item); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + analytics.page(); |
| 81 | +})(); |
0 commit comments