|
1 |
| -(function() { |
| 1 | +(function () { |
2 | 2 | // Create a dummy analytics object until real loaded
|
3 | 3 | window.analytics || (window.analytics = []);
|
4 | 4 | window.analytics.methods = [
|
|
18 | 18 | 'once',
|
19 | 19 | 'off'
|
20 | 20 | ];
|
21 |
| - window.analytics.factory = function(method) { |
22 |
| - return function() { |
| 21 | + window.analytics.factory = function (method) { |
| 22 | + return function () { |
23 | 23 | var args = Array.prototype.slice.call(arguments);
|
24 | 24 | args.unshift(method);
|
25 | 25 | window.analytics.push(args);
|
|
32 | 32 | }
|
33 | 33 |
|
34 | 34 | // Load analytics async
|
35 |
| - analytics.load = function(callback) { |
| 35 | + analytics.load = function (apiKey) { |
36 | 36 | if (document.getElementById('dreamdata-analytics')) return;
|
37 | 37 |
|
38 | 38 | // We make a copy if our dummy object
|
|
42 | 42 | script.id = 'dreamdata-analytics';
|
43 | 43 | script.type = 'text/javascript';
|
44 | 44 | script.src = 'https://cdn.dreamdata.cloud/scripts/analytics/v1/dreamdata.min.js';
|
| 45 | + |
| 46 | + var onAnalyticsLoad = function () { |
| 47 | + if (analytics && analytics.initialize) { |
| 48 | + analytics.initialize({ 'Dreamdata.io': { apiKey: apiKey } }); |
| 49 | + // Now copy whatever we applied to our dummy object to the real analytics |
| 50 | + while (window.a.length > 0) { |
| 51 | + var item = window.a.shift(); |
| 52 | + var method = item.shift(); |
| 53 | + if (analytics[method]) analytics[method].apply(analytics, item); |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | + |
45 | 58 | if (script.addEventListener) {
|
46 | 59 | script.addEventListener(
|
47 | 60 | 'load',
|
48 |
| - function(e) { |
49 |
| - if (typeof callback === 'function') { |
50 |
| - callback(e); |
51 |
| - } |
| 61 | + function () { |
| 62 | + onAnalyticsLoad(); |
52 | 63 | },
|
53 | 64 | false
|
54 | 65 | );
|
55 | 66 | } else {
|
56 | 67 | //IE8
|
57 |
| - script.onreadystatechange = function() { |
| 68 | + script.onreadystatechange = function () { |
58 | 69 | if (this.readyState == 'complete' || this.readyState == 'loaded') {
|
59 |
| - callback(window.event); |
| 70 | + onAnalyticsLoad(); |
60 | 71 | }
|
61 | 72 | };
|
62 | 73 | }
|
63 | 74 | var first = document.getElementsByTagName('script')[0];
|
64 | 75 | first.parentNode.insertBefore(script, first);
|
65 | 76 | };
|
66 | 77 |
|
67 |
| - analytics.load(function() { |
68 |
| - // On load init our integrations |
69 |
| - analytics.initialize({ 'Dreamdata.io': { apiKey: '<API_KEY>' } }); |
70 |
| - // Now copy whatever we applied to our dummy object to the real analytics |
71 |
| - while (window.a.length > 0) { |
72 |
| - var item = window.a.shift(); |
73 |
| - var method = item.shift(); |
74 |
| - if (analytics[method]) analytics[method].apply(analytics, item); |
75 |
| - } |
76 |
| - }); |
| 78 | + analytics.load('<API_KEY>'); |
77 | 79 |
|
78 | 80 | analytics.page();
|
79 | 81 | })();
|
0 commit comments