@builder.io/sdk-react-native 0.0.6-0 → 0.0.6-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/functions/get-content/ab-testing.js +52 -38
- package/src/functions/get-content/index.js +5 -2
- package/src/functions/sanitize-styles.js +1 -2
- package/src/functions/track.js +3 -4
- package/src/helpers/ab-tests.js +5 -7
- package/src/helpers/cookie.js +20 -5
- package/src/helpers/sessionId.js +5 -7
- package/src/helpers/document-cookie.js +0 -0
- package/src/helpers/storage.js +0 -37
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@builder.io/sdk-react-native",
|
|
3
3
|
"description": "Builder.io SDK for React Native",
|
|
4
|
-
"version": "0.0.6-
|
|
4
|
+
"version": "0.0.6-2",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"release:patch": "npm version patch --no-git-tag-version && npm publish --access public",
|
|
8
8
|
"release:dev": "npm version prerelease --no-git-tag-version && npm publish --tag dev --access public"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@react-native-async-storage/async-storage": "^1.17.10",
|
|
12
|
-
"@react-native-cookies/cookies": "^6.2.1",
|
|
13
11
|
"react-native-render-html": "^6.3.4",
|
|
14
12
|
"react-native-storage": "^1.0.1",
|
|
15
13
|
"react-native-video": "^5.1.1"
|
|
16
14
|
},
|
|
17
15
|
"peerDependencies": {
|
|
16
|
+
"@react-native-async-storage/async-storage": "^1.17.10",
|
|
18
17
|
"react-native": "^0.64.3"
|
|
19
18
|
},
|
|
20
19
|
"devDependencies": {
|
|
20
|
+
"@react-native-async-storage/async-storage": "^1.17.10",
|
|
21
21
|
"@types/react-native-video": "^5.0.9",
|
|
22
22
|
"react-native": "^0.64.3"
|
|
23
23
|
}
|
|
@@ -22,62 +22,76 @@ import {
|
|
|
22
22
|
getContentVariationCookie,
|
|
23
23
|
setContentVariationCookie
|
|
24
24
|
} from "../../helpers/ab-tests.js";
|
|
25
|
-
const
|
|
25
|
+
const checkIsBuilderContentWithVariations = (item) => !item.id || !item.variations || Object.keys(item.variations).length === 0;
|
|
26
|
+
const getRandomVariationId = ({
|
|
27
|
+
id,
|
|
28
|
+
variations
|
|
29
|
+
}) => {
|
|
30
|
+
var _a;
|
|
26
31
|
let n = 0;
|
|
27
32
|
const random = Math.random();
|
|
28
|
-
for (const
|
|
29
|
-
const testRatio = variations[
|
|
33
|
+
for (const id2 in variations) {
|
|
34
|
+
const testRatio = (_a = variations[id2]) == null ? void 0 : _a.testRatio;
|
|
30
35
|
n += testRatio;
|
|
31
36
|
if (random < n) {
|
|
32
|
-
return
|
|
37
|
+
return id2;
|
|
33
38
|
}
|
|
34
39
|
}
|
|
35
|
-
return
|
|
40
|
+
return id;
|
|
41
|
+
};
|
|
42
|
+
const getTestFields = ({
|
|
43
|
+
item,
|
|
44
|
+
testGroupId
|
|
45
|
+
}) => {
|
|
46
|
+
const variationValue = item.variations[testGroupId];
|
|
47
|
+
if (testGroupId === item.id || !variationValue) {
|
|
48
|
+
return {
|
|
49
|
+
testVariationId: item.id,
|
|
50
|
+
testVariationName: "Default"
|
|
51
|
+
};
|
|
52
|
+
} else {
|
|
53
|
+
return {
|
|
54
|
+
data: variationValue.data,
|
|
55
|
+
testVariationId: variationValue.id,
|
|
56
|
+
testVariationName: variationValue.name || (variationValue.id === item.id ? "Default" : "")
|
|
57
|
+
};
|
|
58
|
+
}
|
|
36
59
|
};
|
|
37
60
|
const getContentVariation = (_0) => __async(void 0, [_0], function* ({
|
|
38
61
|
item,
|
|
39
|
-
|
|
62
|
+
canTrack
|
|
40
63
|
}) {
|
|
41
|
-
if (!item.id || !item.variations || Object.keys(item.variations).length === 0) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
64
|
const testGroupId = yield getContentVariationCookie({
|
|
45
|
-
canTrack
|
|
46
|
-
contentId: item.id
|
|
47
|
-
orgId
|
|
65
|
+
canTrack,
|
|
66
|
+
contentId: item.id
|
|
48
67
|
});
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return variationValue;
|
|
68
|
+
const testFields = testGroupId ? getTestFields({ item, testGroupId }) : void 0;
|
|
69
|
+
if (testFields) {
|
|
70
|
+
return testFields;
|
|
53
71
|
} else {
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
72
|
+
const randomVariationId = getRandomVariationId({
|
|
73
|
+
variations: item.variations,
|
|
74
|
+
id: item.id
|
|
75
|
+
});
|
|
76
|
+
setContentVariationCookie({
|
|
77
|
+
contentId: item.id,
|
|
78
|
+
value: randomVariationId,
|
|
79
|
+
canTrack
|
|
80
|
+
}).catch((err) => {
|
|
81
|
+
console.error("could not store A/B test variation: ", err);
|
|
82
|
+
});
|
|
83
|
+
return getTestFields({ item, testGroupId: randomVariationId });
|
|
65
84
|
}
|
|
66
85
|
});
|
|
67
86
|
const handleABTesting = (_0) => __async(void 0, [_0], function* ({
|
|
68
87
|
item,
|
|
69
|
-
|
|
88
|
+
canTrack
|
|
70
89
|
}) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
} : {
|
|
77
|
-
testVariationId: item.id,
|
|
78
|
-
testVariationName: "Default"
|
|
79
|
-
};
|
|
80
|
-
Object.assign(item, newValues);
|
|
90
|
+
if (!checkIsBuilderContentWithVariations(item)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const variationValue = yield getContentVariation({ item, canTrack });
|
|
94
|
+
Object.assign(item, variationValue);
|
|
81
95
|
});
|
|
82
96
|
export {
|
|
83
97
|
handleABTesting
|
|
@@ -80,8 +80,11 @@ function getAllContent(options) {
|
|
|
80
80
|
const url = generateContentUrl(options);
|
|
81
81
|
const fetch = yield getFetch();
|
|
82
82
|
const content = yield fetch(url.href).then((res) => res.json());
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
const canTrack = options.canTrack !== false;
|
|
84
|
+
if (canTrack) {
|
|
85
|
+
for (const item of content.results) {
|
|
86
|
+
yield handleABTesting({ item, canTrack });
|
|
87
|
+
}
|
|
85
88
|
}
|
|
86
89
|
return content;
|
|
87
90
|
});
|
|
@@ -48,8 +48,7 @@ const sanitizeBlockStyles = (styles) => {
|
|
|
48
48
|
const newValue = parseFloat(propertyValue);
|
|
49
49
|
const normalizedValue = normalizeNumber(newValue);
|
|
50
50
|
if (normalizedValue) {
|
|
51
|
-
|
|
52
|
-
return __spreadProps(__spreadValues({}, acc), { [key]: valueWithUnits });
|
|
51
|
+
return __spreadProps(__spreadValues({}, acc), { [key]: normalizedValue });
|
|
53
52
|
} else {
|
|
54
53
|
return acc;
|
|
55
54
|
}
|
package/src/functions/track.js
CHANGED
|
@@ -55,13 +55,12 @@ import { getVisitorId } from "../helpers/visitorId.js";
|
|
|
55
55
|
import { isBrowser } from "./is-browser.js";
|
|
56
56
|
import { isEditing } from "./is-editing.js";
|
|
57
57
|
const getTrackingEventData = (_0) => __async(void 0, [_0], function* ({
|
|
58
|
-
canTrack
|
|
59
|
-
orgId
|
|
58
|
+
canTrack
|
|
60
59
|
}) {
|
|
61
60
|
if (!canTrack) {
|
|
62
61
|
return { visitorId: void 0, sessionId: void 0 };
|
|
63
62
|
}
|
|
64
|
-
const sessionId = yield getSessionId({ canTrack
|
|
63
|
+
const sessionId = yield getSessionId({ canTrack });
|
|
65
64
|
const visitorId = getVisitorId({ canTrack });
|
|
66
65
|
return {
|
|
67
66
|
sessionId,
|
|
@@ -82,7 +81,7 @@ const createEvent = (_a) => __async(void 0, null, function* () {
|
|
|
82
81
|
]);
|
|
83
82
|
return {
|
|
84
83
|
type: eventType,
|
|
85
|
-
data: __spreadProps(__spreadValues(__spreadValues({}, properties), yield getTrackingEventData({ canTrack
|
|
84
|
+
data: __spreadProps(__spreadValues(__spreadValues({}, properties), yield getTrackingEventData({ canTrack })), {
|
|
86
85
|
ownerId: orgId,
|
|
87
86
|
contentId
|
|
88
87
|
})
|
package/src/helpers/ab-tests.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import { getCookie, setCookie } from "./cookie.js";
|
|
2
|
-
const BUILDER_STORE_PREFIX = "
|
|
2
|
+
const BUILDER_STORE_PREFIX = "builderio.variations";
|
|
3
3
|
const getContentTestKey = (id) => `${BUILDER_STORE_PREFIX}.${id}`;
|
|
4
4
|
const getContentVariationCookie = ({
|
|
5
5
|
contentId,
|
|
6
|
-
canTrack
|
|
7
|
-
|
|
8
|
-
}) => getCookie({ name: getContentTestKey(contentId), canTrack, orgId });
|
|
6
|
+
canTrack
|
|
7
|
+
}) => getCookie({ name: getContentTestKey(contentId), canTrack });
|
|
9
8
|
const setContentVariationCookie = ({
|
|
10
9
|
contentId,
|
|
11
10
|
canTrack,
|
|
12
|
-
value
|
|
13
|
-
|
|
14
|
-
}) => setCookie({ name: getContentTestKey(contentId), value, canTrack, orgId });
|
|
11
|
+
value
|
|
12
|
+
}) => setCookie({ name: getContentTestKey(contentId), value, canTrack });
|
|
15
13
|
export {
|
|
16
14
|
getContentVariationCookie,
|
|
17
15
|
setContentVariationCookie
|
package/src/helpers/cookie.js
CHANGED
|
@@ -18,7 +18,22 @@ var __async = (__this, __arguments, generator) => {
|
|
|
18
18
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
|
-
import
|
|
21
|
+
import Storage from "react-native-storage";
|
|
22
|
+
import { isBrowser } from "../functions/is-browser.js";
|
|
23
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
24
|
+
const ONE_DAY = 1e3 * 60 * 60 * 24;
|
|
25
|
+
const initStorage = () => {
|
|
26
|
+
const backend = isBrowser() ? window.localStorage : AsyncStorage;
|
|
27
|
+
const storage2 = new Storage({
|
|
28
|
+
size: 1e3,
|
|
29
|
+
storageBackend: backend,
|
|
30
|
+
defaultExpires: ONE_DAY * 30,
|
|
31
|
+
enableCache: true
|
|
32
|
+
});
|
|
33
|
+
return storage2;
|
|
34
|
+
};
|
|
35
|
+
const storage = initStorage();
|
|
36
|
+
const getStorageName = (name) => `builderio.${name}`;
|
|
22
37
|
const getCookie = (_0) => __async(void 0, [_0], function* ({
|
|
23
38
|
name,
|
|
24
39
|
canTrack
|
|
@@ -28,8 +43,8 @@ const getCookie = (_0) => __async(void 0, [_0], function* ({
|
|
|
28
43
|
return void 0;
|
|
29
44
|
}
|
|
30
45
|
const parsedName = name.replace(/_/g, ".");
|
|
31
|
-
const data = yield
|
|
32
|
-
console.log("data", data);
|
|
46
|
+
const data = yield storage.load({ key: getStorageName(parsedName) });
|
|
47
|
+
console.log("loaded data: ", data.value);
|
|
33
48
|
return data.value;
|
|
34
49
|
} catch (err) {
|
|
35
50
|
console.debug("[COOKIE] GET error: ", err);
|
|
@@ -45,8 +60,8 @@ const setCookie = (_0) => __async(void 0, [_0], function* ({
|
|
|
45
60
|
if (!canTrack) {
|
|
46
61
|
return void 0;
|
|
47
62
|
}
|
|
48
|
-
yield
|
|
49
|
-
console.log("data",
|
|
63
|
+
yield storage.save({ key: getStorageName(name), data: { value }, expires });
|
|
64
|
+
console.log("saved data: ", { key: name, data: { value }, expires });
|
|
50
65
|
} catch (err) {
|
|
51
66
|
console.warn("[COOKIE] SET error: ", err);
|
|
52
67
|
}
|
package/src/helpers/sessionId.js
CHANGED
|
@@ -22,28 +22,26 @@ import { getCookie, setCookie } from "./cookie.js";
|
|
|
22
22
|
import { checkIsDefined } from "./nullable.js";
|
|
23
23
|
import { uuid } from "./uuid.js";
|
|
24
24
|
const SESSION_LOCAL_STORAGE_KEY = "builderSessionId";
|
|
25
|
-
const getSessionId = (_0) => __async(void 0, [_0], function* ({ canTrack
|
|
25
|
+
const getSessionId = (_0) => __async(void 0, [_0], function* ({ canTrack }) {
|
|
26
26
|
if (!canTrack) {
|
|
27
27
|
return void 0;
|
|
28
28
|
}
|
|
29
29
|
const sessionId = yield getCookie({
|
|
30
30
|
name: SESSION_LOCAL_STORAGE_KEY,
|
|
31
|
-
canTrack
|
|
32
|
-
orgId
|
|
31
|
+
canTrack
|
|
33
32
|
});
|
|
34
33
|
if (checkIsDefined(sessionId)) {
|
|
35
34
|
return sessionId;
|
|
36
35
|
} else {
|
|
37
36
|
const newSessionId = createSessionId();
|
|
38
|
-
setSessionId({ id: newSessionId, canTrack
|
|
37
|
+
setSessionId({ id: newSessionId, canTrack });
|
|
39
38
|
}
|
|
40
39
|
});
|
|
41
40
|
const createSessionId = () => uuid();
|
|
42
41
|
const setSessionId = ({
|
|
43
42
|
id,
|
|
44
|
-
canTrack
|
|
45
|
-
|
|
46
|
-
}) => setCookie({ name: SESSION_LOCAL_STORAGE_KEY, value: id, canTrack, orgId });
|
|
43
|
+
canTrack
|
|
44
|
+
}) => setCookie({ name: SESSION_LOCAL_STORAGE_KEY, value: id, canTrack });
|
|
47
45
|
export {
|
|
48
46
|
createSessionId,
|
|
49
47
|
getSessionId,
|
|
File without changes
|
package/src/helpers/storage.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
var __async = (__this, __arguments, generator) => {
|
|
2
|
-
return new Promise((resolve, reject) => {
|
|
3
|
-
var fulfilled = (value) => {
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import Storage from "react-native-storage";
|
|
22
|
-
import { isBrowser } from "../functions/is-browser.js";
|
|
23
|
-
const ONE_DAY = 1e3 * 60 * 60 * 24;
|
|
24
|
-
const initStorage = () => __async(void 0, null, function* () {
|
|
25
|
-
const backend = isBrowser() ? window.localStorage : yield import("@react-native-async-storage/async-storage");
|
|
26
|
-
const storage = new Storage({
|
|
27
|
-
size: 1e3,
|
|
28
|
-
storageBackend: backend,
|
|
29
|
-
defaultExpires: ONE_DAY * 30,
|
|
30
|
-
enableCache: true
|
|
31
|
-
});
|
|
32
|
-
return storage;
|
|
33
|
-
});
|
|
34
|
-
var stdin_default = initStorage();
|
|
35
|
-
export {
|
|
36
|
-
stdin_default as default
|
|
37
|
-
};
|