Skip to content

Commit 3d55127

Browse files
author
Nikolaos Veneti
committed
init scripts
1 parent 17a68a9 commit 3d55127

File tree

3 files changed

+84
-146
lines changed

3 files changed

+84
-146
lines changed

lib/init-script/init.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
})();

lib/init-script/init.min.js

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

lib/package-lock.json

Lines changed: 0 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)