forked from product-fruits/npm-product-fruits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (35 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
let initialized = false;
function initInternal() {
if (typeof window == 'undefined') {
throw new Error('product-fruits package can run only in the browser environment. If you use server-side-generation, please update your code to import this package only in the browser.')
}
(function (w, d, u) {
w.$productFruits = w.$productFruits || [];
w.productFruits = w.productFruits || {}; w.productFruits.scrV = '2';
let a = d.getElementsByTagName('head')[0]; let r = d.createElement('script'); r.async = 1; r.src = u; a.appendChild(r);
})(window, document, 'https://app.productfruits.com/static/script.js'); // todo make it env agnostic
}
function apiInit(projectCode, language, userInfo, options) {// todo make it env agnostic
// if (initialized) {
// throw new Error('Do not call init() multiple times');
// }
initInternal();
window.$productFruits.push(['init', projectCode, language, userInfo, options]);
initialized = true;
}
function apiSafeExec(callback) {
if (typeof callback !== 'function') throw new Error('You have to pass a callback to safeExec');
if (window.productFruitsIsReady) {
callback(window.$productFruits);
} else {
const handler = () => {
callback(window.$productFruits);
}
window.addEventListener('productfruits_ready', handler, { once: true });
}
}
export const productFruits = {
init: apiInit,
//updateUserData: apiUpdateUserData,
safeExec: apiSafeExec
}