|
| 1 | +(function(window, document) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + // 1. Configuration from script tag |
| 5 | + var script = document.currentScript || document.querySelector('script[data-site]'); |
| 6 | + var siteId = script && script.dataset.site; |
| 7 | + var endpoint = script && script.dataset.endpoint || 'https://analytics.example.com/ingest'; |
| 8 | + var sdkUrl = script && script.dataset.sdkUrl || 'https://cdn.example.com/rush.min.js'; |
| 9 | + var debug = script && script.dataset.debug === 'true'; |
| 10 | + |
| 11 | + // 2. Event queue (stores calls before SDK loads) |
| 12 | + var queue = []; |
| 13 | + |
| 14 | + // 3. Stub Rush object (queues all calls) |
| 15 | + window.Rush = { |
| 16 | + init: function(config) { |
| 17 | + queue.push(['init', config]); |
| 18 | + }, |
| 19 | + track: function(eventName, data) { |
| 20 | + queue.push(['track', eventName, data]); |
| 21 | + }, |
| 22 | + trackPageview: function() { |
| 23 | + queue.push(['trackPageview']); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + // 4. Auto-init if data-site provided |
| 28 | + if (siteId) { |
| 29 | + window.Rush.init({ |
| 30 | + siteId: siteId, |
| 31 | + endpoint: endpoint, |
| 32 | + debug: debug, |
| 33 | + autoTrack: script.dataset.autoTrack !== 'false' |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + // 5. Inject external SDK script |
| 38 | + var sdkScript = document.createElement('script'); |
| 39 | + sdkScript.src = sdkUrl; |
| 40 | + sdkScript.async = true; |
| 41 | + sdkScript.onload = function() { |
| 42 | + // SDK loaded, it will process the queue |
| 43 | + if (debug) console.log('[Rush] SDK loaded, processing queue:', queue.length, 'events'); |
| 44 | + }; |
| 45 | + sdkScript.onerror = function() { |
| 46 | + if (debug) console.error('[Rush] Failed to load SDK'); |
| 47 | + }; |
| 48 | + |
| 49 | + // Inject before first script or in head |
| 50 | + var firstScript = document.getElementsByTagName('script')[0]; |
| 51 | + if (firstScript) { |
| 52 | + firstScript.parentNode.insertBefore(sdkScript, firstScript); |
| 53 | + } else { |
| 54 | + document.head.appendChild(sdkScript); |
| 55 | + } |
| 56 | + |
| 57 | + // 6. Expose queue for SDK to process |
| 58 | + window.Rush._queue = queue; |
| 59 | + |
| 60 | +})(window, document); |
0 commit comments