|
1 |
| -export default function shimGlobalAnalytics() { |
| 1 | +export default function shimGlobalAnalytics(res, rej, segmentKey) { |
2 | 2 | // Create a queue, but don't obliterate an existing one!
|
3 | 3 | const analytics = window.analytics || [];
|
4 | 4 | window.analytics = analytics;
|
@@ -51,7 +51,34 @@ export default function shimGlobalAnalytics() {
|
51 | 51 | };
|
52 | 52 |
|
53 | 53 | // For each of our methods, generate a queueing stub.
|
54 |
| - analytics.methods.forEach(key => { |
55 |
| - analytics[key] = analytics.factory(key); |
| 54 | + analytics.methods.forEach(method => { |
| 55 | + analytics[method] = analytics.factory(method); |
56 | 56 | });
|
| 57 | + |
| 58 | + // Load in our external analytics script see this page for details: |
| 59 | + // https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement |
| 60 | + // Create our script |
| 61 | + |
| 62 | + // Define a method to load Analytics.js from our CDN, |
| 63 | + // and that will be sure to only ever load it once. |
| 64 | + analytics.load = function(key, options) { |
| 65 | + const script = document.createElement('script'); |
| 66 | + script.type = 'text/javascript'; |
| 67 | + // Handle load/error states |
| 68 | + script.onload = res; |
| 69 | + script.onerror = rej; |
| 70 | + // Inject into the head of the document |
| 71 | + document.head.prepend(script); |
| 72 | + // Assign a src so it **actually** loads |
| 73 | + script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`; |
| 74 | + |
| 75 | + analytics._loadOptions = options; // eslint-disable-line |
| 76 | + }; |
| 77 | + |
| 78 | + // Add a version to keep track of what's in the wild. |
| 79 | + analytics.SNIPPET_VERSION = '4.1.0'; |
| 80 | + |
| 81 | + // Load Analytics.js with your key, which will automatically |
| 82 | + // load the tools you've enabled for your account. Boosh! |
| 83 | + analytics.load(segmentKey, { initialPageView: false }); |
57 | 84 | }
|
0 commit comments