Skip to content

Commit 993b7f6

Browse files
committed
Update init scripts for dreamdata analytics
1 parent a2b4abd commit 993b7f6

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

lib/init-script/init.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(function() {
1+
(function () {
22
// Create a dummy analytics object until real loaded
33
window.analytics || (window.analytics = []);
44
window.analytics.methods = [
@@ -18,8 +18,8 @@
1818
'once',
1919
'off'
2020
];
21-
window.analytics.factory = function(method) {
22-
return function() {
21+
window.analytics.factory = function (method) {
22+
return function () {
2323
var args = Array.prototype.slice.call(arguments);
2424
args.unshift(method);
2525
window.analytics.push(args);
@@ -32,7 +32,7 @@
3232
}
3333

3434
// Load analytics async
35-
analytics.load = function(callback) {
35+
analytics.load = function (apiKey) {
3636
if (document.getElementById('dreamdata-analytics')) return;
3737

3838
// We make a copy if our dummy object
@@ -42,38 +42,40 @@
4242
script.id = 'dreamdata-analytics';
4343
script.type = 'text/javascript';
4444
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+
4558
if (script.addEventListener) {
4659
script.addEventListener(
4760
'load',
48-
function(e) {
49-
if (typeof callback === 'function') {
50-
callback(e);
51-
}
61+
function () {
62+
onAnalyticsLoad();
5263
},
5364
false
5465
);
5566
} else {
5667
//IE8
57-
script.onreadystatechange = function() {
68+
script.onreadystatechange = function () {
5869
if (this.readyState == 'complete' || this.readyState == 'loaded') {
59-
callback(window.event);
70+
onAnalyticsLoad();
6071
}
6172
};
6273
}
6374
var first = document.getElementsByTagName('script')[0];
6475
first.parentNode.insertBefore(script, first);
6576
};
6677

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>');
7779

7880
analytics.page();
7981
})();

lib/init-script/init.min.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)