Skip to content

Update init scripts for dreamdata analytics #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions lib/init-script/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function() {
(function () {
// Create a dummy analytics object until real loaded
window.analytics || (window.analytics = []);
window.analytics.methods = [
Expand All @@ -18,8 +18,8 @@
'once',
'off'
];
window.analytics.factory = function(method) {
return function() {
window.analytics.factory = function (method) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(method);
window.analytics.push(args);
Expand All @@ -32,7 +32,7 @@
}

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

// We make a copy if our dummy object
Expand All @@ -42,38 +42,40 @@
script.id = 'dreamdata-analytics';
script.type = 'text/javascript';
script.src = 'https://cdn.dreamdata.cloud/scripts/analytics/v1/dreamdata.min.js';

var onAnalyticsLoad = function () {
if (analytics && analytics.initialize) {
analytics.initialize({ 'Dreamdata.io': { apiKey: apiKey } });
// Now copy whatever we applied to our dummy object to the real analytics
while (window.a.length > 0) {
var item = window.a.shift();
var method = item.shift();
if (analytics[method]) analytics[method].apply(analytics, item);
}
}
};

if (script.addEventListener) {
script.addEventListener(
'load',
function(e) {
if (typeof callback === 'function') {
callback(e);
}
function () {
onAnalyticsLoad();
},
false
);
} else {
//IE8
script.onreadystatechange = function() {
script.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
callback(window.event);
onAnalyticsLoad();
}
};
}
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(script, first);
};

analytics.load(function() {
// On load init our integrations
analytics.initialize({ 'Dreamdata.io': { apiKey: '<API_KEY>' } });
// Now copy whatever we applied to our dummy object to the real analytics
while (window.a.length > 0) {
var item = window.a.shift();
var method = item.shift();
if (analytics[method]) analytics[method].apply(analytics, item);
}
});
analytics.load('<API_KEY>');

analytics.page();
})();
4 changes: 1 addition & 3 deletions lib/init-script/init.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.