@builder.io/sdk-react-native 0.3.1 → 0.4.0
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/dist/blocks/button/button.js +1 -0
- package/dist/blocks/columns/columns.js +1 -0
- package/dist/blocks/custom-code/custom-code.js +1 -0
- package/dist/blocks/embed/embed.js +1 -0
- package/dist/blocks/form/form.js +1 -0
- package/dist/blocks/fragment/fragment.js +1 -0
- package/dist/blocks/img/img.js +1 -0
- package/dist/blocks/input/input.js +1 -0
- package/dist/blocks/raw-text/raw-text.js +1 -0
- package/dist/blocks/section/section.js +1 -0
- package/dist/blocks/select/select.js +1 -0
- package/dist/blocks/submit-button/submit-button.js +1 -0
- package/dist/blocks/symbol/symbol.js +1 -0
- package/dist/blocks/textarea/textarea.js +1 -0
- package/dist/components/render-block/block-styles.js +1 -0
- package/dist/components/render-block/render-block.js +7 -6
- package/dist/components/render-block/render-component.js +2 -2
- package/dist/components/render-block/render-repeated-block.js +1 -0
- package/dist/components/render-blocks.js +1 -0
- package/dist/components/render-content/builder-editing.js +1 -0
- package/dist/components/render-content/components/render-styles.js +2 -1
- package/dist/components/render-content/render-content.js +36 -3
- package/dist/components/render-content/wrap-component-ref.js +5 -0
- package/dist/components/render-content-variants/helpers.js +137 -0
- package/dist/components/render-content-variants/render-content-variants.js +73 -0
- package/dist/components/render-inlined-styles.js +2 -12
- package/dist/constants/sdk-version.js +1 -1
- package/dist/functions/get-content/generate-content-url.js +2 -2
- package/dist/functions/get-content/generate-content-url.test.js +15 -0
- package/dist/functions/get-content/index.js +36 -19
- package/dist/helpers/ab-tests.js +123 -6
- package/dist/helpers/canTrack.js +6 -0
- package/dist/helpers/cookie.js +9 -2
- package/dist/helpers/logger.js +2 -1
- package/dist/index.js +17 -10
- package/package.json +1 -1
- package/src/blocks/button/button.jsx +1 -0
- package/src/blocks/columns/columns.jsx +1 -0
- package/src/blocks/custom-code/custom-code.jsx +1 -0
- package/src/blocks/embed/embed.jsx +1 -0
- package/src/blocks/form/form.jsx +1 -0
- package/src/blocks/fragment/fragment.jsx +1 -0
- package/src/blocks/img/img.jsx +1 -0
- package/src/blocks/input/input.jsx +1 -0
- package/src/blocks/raw-text/raw-text.jsx +1 -0
- package/src/blocks/section/section.jsx +1 -0
- package/src/blocks/select/select.jsx +1 -0
- package/src/blocks/submit-button/submit-button.jsx +1 -0
- package/src/blocks/symbol/symbol.jsx +1 -0
- package/src/blocks/textarea/textarea.jsx +1 -0
- package/src/components/render-block/block-styles.jsx +1 -0
- package/src/components/render-block/render-block.jsx +8 -7
- package/src/components/render-block/render-component.jsx +3 -4
- package/src/components/render-block/render-repeated-block.jsx +1 -0
- package/src/components/render-blocks.jsx +1 -0
- package/src/components/render-content/builder-editing.jsx +1 -0
- package/src/components/render-content/components/render-styles.jsx +4 -3
- package/src/components/render-content/render-content.jsx +42 -2
- package/src/components/render-content/wrap-component-ref.js +4 -0
- package/src/components/render-content-variants/helpers.js +139 -0
- package/src/components/render-content-variants/render-content-variants.jsx +101 -0
- package/src/components/render-inlined-styles.jsx +2 -22
- package/src/constants/sdk-version.js +1 -1
- package/src/functions/get-content/generate-content-url.js +2 -1
- package/src/functions/get-content/generate-content-url.test.js +15 -0
- package/src/functions/get-content/index.js +35 -18
- package/src/helpers/ab-tests.js +132 -10
- package/src/helpers/canTrack.js +5 -0
- package/src/helpers/cookie.js +9 -2
- package/src/helpers/logger.js +2 -1
- package/src/index.js +18 -7
- package/src/functions/get-content/ab-testing.js +0 -99
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { isBrowser } from "../../functions/is-browser";
|
|
2
|
+
const getVariants = (content) => Object.values((content == null ? void 0 : content.variations) || {});
|
|
3
|
+
const checkShouldRunVariants = ({
|
|
4
|
+
canTrack,
|
|
5
|
+
content
|
|
6
|
+
}) => {
|
|
7
|
+
const hasVariants = getVariants(content).length > 0;
|
|
8
|
+
if (!hasVariants) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
if (!canTrack) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (isBrowser()) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
function bldrAbTest(contentId, variants, isHydrationTarget2) {
|
|
20
|
+
function getAndSetVariantId() {
|
|
21
|
+
function setCookie(name, value, days) {
|
|
22
|
+
let expires = "";
|
|
23
|
+
if (days) {
|
|
24
|
+
const date = new Date();
|
|
25
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1e3);
|
|
26
|
+
expires = "; expires=" + date.toUTCString();
|
|
27
|
+
}
|
|
28
|
+
document.cookie = name + "=" + (value || "") + expires + "; path=/; Secure; SameSite=None";
|
|
29
|
+
}
|
|
30
|
+
function getCookie(name) {
|
|
31
|
+
const nameEQ = name + "=";
|
|
32
|
+
const ca = document.cookie.split(";");
|
|
33
|
+
for (let i = 0; i < ca.length; i++) {
|
|
34
|
+
let c = ca[i];
|
|
35
|
+
while (c.charAt(0) === " ")
|
|
36
|
+
c = c.substring(1, c.length);
|
|
37
|
+
if (c.indexOf(nameEQ) === 0)
|
|
38
|
+
return c.substring(nameEQ.length, c.length);
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const cookieName = `builder.tests.${contentId}`;
|
|
43
|
+
const variantInCookie = getCookie(cookieName);
|
|
44
|
+
const availableIDs = variants.map((vr) => vr.id).concat(contentId);
|
|
45
|
+
if (variantInCookie && availableIDs.includes(variantInCookie)) {
|
|
46
|
+
return variantInCookie;
|
|
47
|
+
}
|
|
48
|
+
let n = 0;
|
|
49
|
+
const random = Math.random();
|
|
50
|
+
for (let i = 0; i < variants.length; i++) {
|
|
51
|
+
const variant = variants[i];
|
|
52
|
+
const testRatio = variant.testRatio;
|
|
53
|
+
n += testRatio;
|
|
54
|
+
if (random < n) {
|
|
55
|
+
setCookie(cookieName, variant.id);
|
|
56
|
+
return variant.id;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
setCookie(cookieName, contentId);
|
|
60
|
+
return contentId;
|
|
61
|
+
}
|
|
62
|
+
const winningVariantId = getAndSetVariantId();
|
|
63
|
+
const styleEl = document.getElementById(`variants-styles-${contentId}`);
|
|
64
|
+
if (isHydrationTarget2) {
|
|
65
|
+
styleEl.remove();
|
|
66
|
+
const thisScriptEl = document.getElementById(`variants-script-${contentId}`);
|
|
67
|
+
thisScriptEl == null ? void 0 : thisScriptEl.remove();
|
|
68
|
+
} else {
|
|
69
|
+
const newStyleStr = variants.concat({ id: contentId }).filter((variant) => variant.id !== winningVariantId).map((value) => {
|
|
70
|
+
return `.variant-${value.id} { display: none; }
|
|
71
|
+
`;
|
|
72
|
+
}).join("");
|
|
73
|
+
styleEl.innerHTML = newStyleStr;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function bldrCntntScrpt(variantContentId, defaultContentId, isHydrationTarget2) {
|
|
77
|
+
if (!navigator.cookieEnabled) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
function getCookie(name) {
|
|
81
|
+
const nameEQ = name + "=";
|
|
82
|
+
const ca = document.cookie.split(";");
|
|
83
|
+
for (let i = 0; i < ca.length; i++) {
|
|
84
|
+
let c = ca[i];
|
|
85
|
+
while (c.charAt(0) === " ")
|
|
86
|
+
c = c.substring(1, c.length);
|
|
87
|
+
if (c.indexOf(nameEQ) === 0)
|
|
88
|
+
return c.substring(nameEQ.length, c.length);
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const cookieName = `builder.tests.${defaultContentId}`;
|
|
93
|
+
const variantId = getCookie(cookieName);
|
|
94
|
+
const parentDiv = document.querySelector(`[builder-content-id="${variantContentId}"]`);
|
|
95
|
+
const variantIsDefaultContent = variantContentId === defaultContentId;
|
|
96
|
+
if (variantId === variantContentId) {
|
|
97
|
+
if (variantIsDefaultContent) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
parentDiv == null ? void 0 : parentDiv.removeAttribute("hidden");
|
|
101
|
+
parentDiv == null ? void 0 : parentDiv.removeAttribute("aria-hidden");
|
|
102
|
+
} else {
|
|
103
|
+
if (variantIsDefaultContent) {
|
|
104
|
+
if (isHydrationTarget2) {
|
|
105
|
+
parentDiv == null ? void 0 : parentDiv.remove();
|
|
106
|
+
} else {
|
|
107
|
+
parentDiv == null ? void 0 : parentDiv.setAttribute("hidden", "true");
|
|
108
|
+
parentDiv == null ? void 0 : parentDiv.setAttribute("aria-hidden", "true");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const isHydrationTarget = (target) => target === "react" || target === "reactNative" || target === "vue3" || target === "vue2";
|
|
116
|
+
const AB_TEST_FN_NAME = "bldrAbTest";
|
|
117
|
+
const CONTENT_FN_NAME = "bldrCntntScrpt";
|
|
118
|
+
const getVariantsScriptString = (variants, contentId) => {
|
|
119
|
+
const fnStr = bldrAbTest.toString().replace(/\s+/g, " ");
|
|
120
|
+
const fnStr2 = bldrCntntScrpt.toString().replace(/\s+/g, " ");
|
|
121
|
+
return `
|
|
122
|
+
const ${AB_TEST_FN_NAME} = ${fnStr}
|
|
123
|
+
const ${CONTENT_FN_NAME} = ${fnStr2}
|
|
124
|
+
${AB_TEST_FN_NAME}("${contentId}", ${JSON.stringify(variants)}, ${isHydrationTarget})
|
|
125
|
+
`;
|
|
126
|
+
};
|
|
127
|
+
const getRenderContentScriptString = ({
|
|
128
|
+
parentContentId,
|
|
129
|
+
contentId
|
|
130
|
+
}) => {
|
|
131
|
+
return `
|
|
132
|
+
${CONTENT_FN_NAME}("${contentId}", "${parentContentId}", ${isHydrationTarget})`;
|
|
133
|
+
};
|
|
134
|
+
export {
|
|
135
|
+
checkShouldRunVariants,
|
|
136
|
+
getRenderContentScriptString,
|
|
137
|
+
getVariants,
|
|
138
|
+
getVariantsScriptString
|
|
139
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import {
|
|
4
|
+
FlatList,
|
|
5
|
+
ScrollView,
|
|
6
|
+
View,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
Image,
|
|
9
|
+
Text,
|
|
10
|
+
} from "react-native";
|
|
11
|
+
import { useState } from "react";
|
|
12
|
+
import {
|
|
13
|
+
checkShouldRunVariants,
|
|
14
|
+
getVariants,
|
|
15
|
+
getVariantsScriptString,
|
|
16
|
+
} from "./helpers";
|
|
17
|
+
import RenderContent from "../render-content/render-content";
|
|
18
|
+
import { getDefaultCanTrack } from "../../helpers/canTrack";
|
|
19
|
+
import RenderInlinedStyles from "../render-inlined-styles";
|
|
20
|
+
import { handleABTestingSync } from "../../helpers/ab-tests";
|
|
21
|
+
|
|
22
|
+
function RenderContentVariants(props) {
|
|
23
|
+
const [variantScriptStr, setVariantScriptStr] = useState(() =>
|
|
24
|
+
getVariantsScriptString(
|
|
25
|
+
getVariants(props.content).map((value) => ({
|
|
26
|
+
id: value.id,
|
|
27
|
+
testRatio: value.testRatio,
|
|
28
|
+
})),
|
|
29
|
+
props.content?.id || ""
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const [shouldRenderVariants, setShouldRenderVariants] = useState(() =>
|
|
34
|
+
checkShouldRunVariants({
|
|
35
|
+
canTrack: getDefaultCanTrack(props.canTrack),
|
|
36
|
+
content: props.content,
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const [hideVariantsStyleString, setHideVariantsStyleString] = useState(() =>
|
|
41
|
+
getVariants(props.content)
|
|
42
|
+
.map((value) => `.variant-${value.id} { display: none; } `)
|
|
43
|
+
.join("")
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const [contentToRender, setContentToRender] = useState(() =>
|
|
47
|
+
checkShouldRunVariants({
|
|
48
|
+
canTrack: getDefaultCanTrack(props.canTrack),
|
|
49
|
+
content: props.content,
|
|
50
|
+
})
|
|
51
|
+
? props.content
|
|
52
|
+
: handleABTestingSync({
|
|
53
|
+
item: props.content,
|
|
54
|
+
canTrack: getDefaultCanTrack(props.canTrack),
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<>
|
|
60
|
+
{shouldRenderVariants ? (
|
|
61
|
+
<>
|
|
62
|
+
<RenderInlinedStyles
|
|
63
|
+
id={`variants-styles-${props.content?.id}`}
|
|
64
|
+
styles={hideVariantsStyleString}
|
|
65
|
+
/>
|
|
66
|
+
<View
|
|
67
|
+
id={`variants-script-${props.content?.id}`}
|
|
68
|
+
dangerouslySetInnerHTML={{ __html: variantScriptStr }}
|
|
69
|
+
/>
|
|
70
|
+
{getVariants(props.content)?.map((variant) => (
|
|
71
|
+
<RenderContent
|
|
72
|
+
key={variant.id}
|
|
73
|
+
content={variant}
|
|
74
|
+
apiKey={props.apiKey}
|
|
75
|
+
apiVersion={props.apiVersion}
|
|
76
|
+
canTrack={props.canTrack}
|
|
77
|
+
customComponents={props.customComponents}
|
|
78
|
+
hideContent={true}
|
|
79
|
+
parentContentId={props.content?.id}
|
|
80
|
+
isSsrAbTest={shouldRenderVariants}
|
|
81
|
+
/>
|
|
82
|
+
))}
|
|
83
|
+
</>
|
|
84
|
+
) : null}
|
|
85
|
+
|
|
86
|
+
<RenderContent
|
|
87
|
+
model={props.model}
|
|
88
|
+
content={contentToRender}
|
|
89
|
+
apiKey={props.apiKey}
|
|
90
|
+
apiVersion={props.apiVersion}
|
|
91
|
+
canTrack={props.canTrack}
|
|
92
|
+
customComponents={props.customComponents}
|
|
93
|
+
classNameProp={`variant-${props.content?.id}`}
|
|
94
|
+
parentContentId={props.content?.id}
|
|
95
|
+
isSsrAbTest={shouldRenderVariants}
|
|
96
|
+
/>
|
|
97
|
+
</>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export default RenderContentVariants;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import * as React from "react";
|
|
2
3
|
import {
|
|
3
4
|
FlatList,
|
|
@@ -7,31 +8,10 @@ import {
|
|
|
7
8
|
Image,
|
|
8
9
|
Text,
|
|
9
10
|
} from "react-native";
|
|
10
|
-
import { TARGET } from "../constants/target.js";
|
|
11
11
|
|
|
12
12
|
function RenderInlinedStyles(props) {
|
|
13
|
-
function tag() {
|
|
14
|
-
// NOTE: we have to obfuscate the name of the tag due to a limitation in the svelte-preprocessor plugin.
|
|
15
|
-
// https://github.com/sveltejs/vite-plugin-svelte/issues/315#issuecomment-1109000027
|
|
16
|
-
return "sty" + "le";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function injectedStyleScript() {
|
|
20
|
-
return `<${tag()}>${props.styles}</${tag()}>`;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
13
|
return (
|
|
24
|
-
|
|
25
|
-
{TARGET === "svelte" || TARGET === "qwik" ? (
|
|
26
|
-
<>
|
|
27
|
-
<View dangerouslySetInnerHTML={{ __html: props.styles }} />
|
|
28
|
-
</>
|
|
29
|
-
) : (
|
|
30
|
-
<View>
|
|
31
|
-
<Text>{props.styles}</Text>
|
|
32
|
-
</View>
|
|
33
|
-
)}
|
|
34
|
-
</>
|
|
14
|
+
<View dangerouslySetInnerHTML={{ __html: props.styles }} id={props.id} />
|
|
35
15
|
);
|
|
36
16
|
}
|
|
37
17
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.
|
|
1
|
+
export const SDK_VERSION = "0.4.0"
|
|
@@ -29,6 +29,7 @@ const generateContentUrl = (options) => {
|
|
|
29
29
|
model,
|
|
30
30
|
apiKey,
|
|
31
31
|
includeRefs = true,
|
|
32
|
+
enrich,
|
|
32
33
|
locale,
|
|
33
34
|
apiVersion = DEFAULT_API_VERSION
|
|
34
35
|
} = options;
|
|
@@ -38,7 +39,7 @@ const generateContentUrl = (options) => {
|
|
|
38
39
|
if (!["v2", "v3"].includes(apiVersion)) {
|
|
39
40
|
throw new Error(`Invalid apiVersion: expected 'v2' or 'v3', received '${apiVersion}'`);
|
|
40
41
|
}
|
|
41
|
-
const url = new URL(`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}`);
|
|
42
|
+
const url = new URL(`https://cdn.builder.io/api/${apiVersion}/content/${model}?apiKey=${apiKey}&limit=${limit}&noTraverse=${noTraverse}&includeRefs=${includeRefs}${locale ? `&locale=${locale}` : ""}${enrich ? `&enrich=${enrich}` : ""}`);
|
|
42
43
|
const queryOptions = __spreadValues(__spreadValues({}, getBuilderSearchParamsFromWindow()), normalizeSearchParams(options.options || {}));
|
|
43
44
|
const flattened = flatten(queryOptions);
|
|
44
45
|
for (const key in flattened) {
|
|
@@ -79,4 +79,19 @@ describe("Generate Content URL", () => {
|
|
|
79
79
|
});
|
|
80
80
|
}).toThrow(`Invalid apiVersion: expected 'v2' or 'v3', received 'INVALID_API_VERSION'`);
|
|
81
81
|
});
|
|
82
|
+
test("generate content url with enrich option true", () => {
|
|
83
|
+
const output = generateContentUrl({
|
|
84
|
+
apiKey: testKey,
|
|
85
|
+
model: testModel,
|
|
86
|
+
enrich: true
|
|
87
|
+
});
|
|
88
|
+
expect(output).toMatchSnapshot();
|
|
89
|
+
});
|
|
90
|
+
test("generate content url with enrich option not present", () => {
|
|
91
|
+
const output = generateContentUrl({
|
|
92
|
+
apiKey: testKey,
|
|
93
|
+
model: testModel
|
|
94
|
+
});
|
|
95
|
+
expect(output).toMatchSnapshot();
|
|
96
|
+
});
|
|
82
97
|
});
|
|
@@ -37,40 +37,56 @@ var __async = (__this, __arguments, generator) => {
|
|
|
37
37
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
|
+
import { TARGET } from "../../constants/target.js";
|
|
41
|
+
import { handleABTesting } from "../../helpers/ab-tests.js";
|
|
42
|
+
import { getDefaultCanTrack } from "../../helpers/canTrack.js";
|
|
40
43
|
import { logger } from "../../helpers/logger.js";
|
|
41
44
|
import { fetch } from "../get-fetch.js";
|
|
42
|
-
import {
|
|
45
|
+
import { isBrowser } from "../is-browser.js";
|
|
43
46
|
import { generateContentUrl } from "./generate-content-url.js";
|
|
47
|
+
const checkContentHasResults = (content) => "results" in content;
|
|
44
48
|
function getContent(options) {
|
|
45
49
|
return __async(this, null, function* () {
|
|
46
50
|
const allContent = yield getAllContent(__spreadProps(__spreadValues({}, options), { limit: 1 }));
|
|
47
|
-
if (allContent &&
|
|
48
|
-
return
|
|
51
|
+
if (allContent && checkContentHasResults(allContent)) {
|
|
52
|
+
return allContent.results[0] || null;
|
|
49
53
|
}
|
|
50
54
|
return null;
|
|
51
55
|
});
|
|
52
56
|
}
|
|
57
|
+
const fetchContent = (options) => __async(void 0, null, function* () {
|
|
58
|
+
const url = generateContentUrl(options);
|
|
59
|
+
const res = yield fetch(url.href);
|
|
60
|
+
const content = yield res.json();
|
|
61
|
+
return content;
|
|
62
|
+
});
|
|
63
|
+
const processContentResult = (options, content) => __async(void 0, null, function* () {
|
|
64
|
+
const canTrack = getDefaultCanTrack(options.canTrack);
|
|
65
|
+
if (!canTrack)
|
|
66
|
+
return content;
|
|
67
|
+
if (!(isBrowser() || TARGET === "reactNative"))
|
|
68
|
+
return content;
|
|
69
|
+
try {
|
|
70
|
+
const newResults = [];
|
|
71
|
+
for (const item of content.results) {
|
|
72
|
+
newResults.push(yield handleABTesting({ item, canTrack }));
|
|
73
|
+
}
|
|
74
|
+
content.results = newResults;
|
|
75
|
+
} catch (e) {
|
|
76
|
+
logger.error("Could not process A/B tests. ", e);
|
|
77
|
+
}
|
|
78
|
+
return content;
|
|
79
|
+
});
|
|
53
80
|
function getAllContent(options) {
|
|
54
81
|
return __async(this, null, function* () {
|
|
55
82
|
try {
|
|
56
83
|
const url = generateContentUrl(options);
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
if ("status" in content && !("results" in content)) {
|
|
84
|
+
const content = yield fetchContent(options);
|
|
85
|
+
if (!checkContentHasResults(content)) {
|
|
60
86
|
logger.error("Error fetching data. ", { url, content, options });
|
|
61
87
|
return content;
|
|
62
88
|
}
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
if (canTrack && Array.isArray(content.results)) {
|
|
66
|
-
for (const item of content.results) {
|
|
67
|
-
yield handleABTesting({ item, canTrack });
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
} catch (e) {
|
|
71
|
-
logger.error("Could not setup A/B testing. ", e);
|
|
72
|
-
}
|
|
73
|
-
return content;
|
|
89
|
+
return processContentResult(options, content);
|
|
74
90
|
} catch (error) {
|
|
75
91
|
logger.error("Error fetching data. ", error);
|
|
76
92
|
return null;
|
|
@@ -79,5 +95,6 @@ function getAllContent(options) {
|
|
|
79
95
|
}
|
|
80
96
|
export {
|
|
81
97
|
getAllContent,
|
|
82
|
-
getContent
|
|
98
|
+
getContent,
|
|
99
|
+
processContentResult
|
|
83
100
|
};
|
package/src/helpers/ab-tests.js
CHANGED
|
@@ -1,16 +1,138 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
import { getCookie, getCookieSync, setCookie } from "./cookie.js";
|
|
38
|
+
import { checkIsDefined } from "../helpers/nullable.js";
|
|
39
|
+
import { logger } from "./logger.js";
|
|
40
|
+
const BUILDER_STORE_PREFIX = "builder.tests";
|
|
3
41
|
const getContentTestKey = (id) => `${BUILDER_STORE_PREFIX}.${id}`;
|
|
4
|
-
const getContentVariationCookie = ({
|
|
5
|
-
|
|
6
|
-
canTrack
|
|
7
|
-
}) => getCookie({ name: getContentTestKey(contentId), canTrack });
|
|
42
|
+
const getContentVariationCookie = ({ contentId }) => getCookie({ name: getContentTestKey(contentId), canTrack: true });
|
|
43
|
+
const getContentVariationCookieSync = ({ contentId }) => getCookieSync({ name: getContentTestKey(contentId), canTrack: true });
|
|
8
44
|
const setContentVariationCookie = ({
|
|
9
45
|
contentId,
|
|
10
|
-
canTrack,
|
|
11
46
|
value
|
|
12
|
-
}) => setCookie({ name: getContentTestKey(contentId), value, canTrack });
|
|
47
|
+
}) => setCookie({ name: getContentTestKey(contentId), value, canTrack: true });
|
|
48
|
+
const checkIsBuilderContentWithVariations = (item) => checkIsDefined(item.id) && checkIsDefined(item.variations) && Object.keys(item.variations).length > 0;
|
|
49
|
+
const getRandomVariationId = ({
|
|
50
|
+
id,
|
|
51
|
+
variations
|
|
52
|
+
}) => {
|
|
53
|
+
var _a;
|
|
54
|
+
let n = 0;
|
|
55
|
+
const random = Math.random();
|
|
56
|
+
for (const id2 in variations) {
|
|
57
|
+
const testRatio = (_a = variations[id2]) == null ? void 0 : _a.testRatio;
|
|
58
|
+
n += testRatio;
|
|
59
|
+
if (random < n) {
|
|
60
|
+
return id2;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return id;
|
|
64
|
+
};
|
|
65
|
+
const getAndSetVariantId = (args) => {
|
|
66
|
+
const randomVariationId = getRandomVariationId(args);
|
|
67
|
+
setContentVariationCookie({
|
|
68
|
+
contentId: args.id,
|
|
69
|
+
value: randomVariationId
|
|
70
|
+
}).catch((err) => {
|
|
71
|
+
logger.error("could not store A/B test variation: ", err);
|
|
72
|
+
});
|
|
73
|
+
return randomVariationId;
|
|
74
|
+
};
|
|
75
|
+
const getTestFields = ({
|
|
76
|
+
item,
|
|
77
|
+
testGroupId
|
|
78
|
+
}) => {
|
|
79
|
+
const variationValue = item.variations[testGroupId];
|
|
80
|
+
if (testGroupId === item.id || !variationValue) {
|
|
81
|
+
return {
|
|
82
|
+
testVariationId: item.id,
|
|
83
|
+
testVariationName: "Default"
|
|
84
|
+
};
|
|
85
|
+
} else {
|
|
86
|
+
return {
|
|
87
|
+
data: variationValue.data,
|
|
88
|
+
testVariationId: variationValue.id,
|
|
89
|
+
testVariationName: variationValue.name || (variationValue.id === item.id ? "Default" : "")
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const handleABTestingSync = ({
|
|
94
|
+
item,
|
|
95
|
+
canTrack
|
|
96
|
+
}) => {
|
|
97
|
+
if (!canTrack) {
|
|
98
|
+
return item;
|
|
99
|
+
}
|
|
100
|
+
if (!item) {
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
if (!checkIsBuilderContentWithVariations(item)) {
|
|
104
|
+
return item;
|
|
105
|
+
}
|
|
106
|
+
const testGroupId = getContentVariationCookieSync({
|
|
107
|
+
contentId: item.id
|
|
108
|
+
}) || getAndSetVariantId({
|
|
109
|
+
variations: item.variations,
|
|
110
|
+
id: item.id
|
|
111
|
+
});
|
|
112
|
+
const variationValue = getTestFields({ item, testGroupId });
|
|
113
|
+
return __spreadValues(__spreadValues({}, item), variationValue);
|
|
114
|
+
};
|
|
115
|
+
const handleABTesting = (_0) => __async(void 0, [_0], function* ({
|
|
116
|
+
item,
|
|
117
|
+
canTrack
|
|
118
|
+
}) {
|
|
119
|
+
if (!canTrack) {
|
|
120
|
+
return item;
|
|
121
|
+
}
|
|
122
|
+
if (!checkIsBuilderContentWithVariations(item)) {
|
|
123
|
+
return item;
|
|
124
|
+
}
|
|
125
|
+
const cookieValue = yield getContentVariationCookie({
|
|
126
|
+
contentId: item.id
|
|
127
|
+
});
|
|
128
|
+
const testGroupId = cookieValue || getAndSetVariantId({
|
|
129
|
+
variations: item.variations,
|
|
130
|
+
id: item.id
|
|
131
|
+
});
|
|
132
|
+
const variationValue = getTestFields({ item, testGroupId });
|
|
133
|
+
return __spreadValues(__spreadValues({}, item), variationValue);
|
|
134
|
+
});
|
|
13
135
|
export {
|
|
14
|
-
|
|
15
|
-
|
|
136
|
+
handleABTesting,
|
|
137
|
+
handleABTestingSync
|
|
16
138
|
};
|
package/src/helpers/cookie.js
CHANGED
|
@@ -18,6 +18,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
18
18
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
|
+
import { logger } from "./logger.js";
|
|
21
22
|
import Storage from "react-native-storage";
|
|
22
23
|
import { isBrowser } from "../functions/is-browser.js";
|
|
23
24
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
@@ -46,7 +47,10 @@ const getCookie = (_0) => __async(void 0, [_0], function* ({
|
|
|
46
47
|
const data = yield storage.load({ key: getStorageName(parsedName) });
|
|
47
48
|
return data.value;
|
|
48
49
|
} catch (err) {
|
|
49
|
-
|
|
50
|
+
if ((err == null ? void 0 : err.name) !== "NotFoundError") {
|
|
51
|
+
logger.warn("[COOKIE] GET error: ", (err == null ? void 0 : err.message) || err);
|
|
52
|
+
}
|
|
53
|
+
return void 0;
|
|
50
54
|
}
|
|
51
55
|
});
|
|
52
56
|
const setCookie = (_0) => __async(void 0, [_0], function* ({
|
|
@@ -61,7 +65,10 @@ const setCookie = (_0) => __async(void 0, [_0], function* ({
|
|
|
61
65
|
}
|
|
62
66
|
yield storage.save({ key: getStorageName(name), data: { value }, expires });
|
|
63
67
|
} catch (err) {
|
|
64
|
-
|
|
68
|
+
if ((err == null ? void 0 : err.name) !== "NotFoundError") {
|
|
69
|
+
logger.warn("[COOKIE] SET error: ", (err == null ? void 0 : err.message) || err);
|
|
70
|
+
}
|
|
71
|
+
return void 0;
|
|
65
72
|
}
|
|
66
73
|
});
|
|
67
74
|
export {
|
package/src/helpers/logger.js
CHANGED
|
@@ -2,7 +2,8 @@ const MSG_PREFIX = "[Builder.io]: ";
|
|
|
2
2
|
const logger = {
|
|
3
3
|
log: (...message) => console.log(MSG_PREFIX, ...message),
|
|
4
4
|
error: (...message) => console.error(MSG_PREFIX, ...message),
|
|
5
|
-
warn: (...message) => console.warn(MSG_PREFIX, ...message)
|
|
5
|
+
warn: (...message) => console.warn(MSG_PREFIX, ...message),
|
|
6
|
+
debug: (...message) => console.debug(MSG_PREFIX, ...message)
|
|
6
7
|
};
|
|
7
8
|
export {
|
|
8
9
|
logger
|
package/src/index.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
export * from "./index-helpers/top-of-file.js";
|
|
2
2
|
export * from "./index-helpers/blocks-exports.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
import { isEditing } from "./functions/is-editing.js";
|
|
4
|
+
import { isPreviewing } from "./functions/is-previewing.js";
|
|
5
|
+
import { createRegisterComponentMessage } from "./functions/register-component.js";
|
|
6
|
+
import { register } from "./functions/register.js";
|
|
7
|
+
import { setEditorSettings } from "./functions/set-editor-settings.js";
|
|
8
|
+
import {
|
|
9
|
+
getAllContent,
|
|
10
|
+
getContent,
|
|
11
|
+
processContentResult
|
|
12
|
+
} from "./functions/get-content/index.js";
|
|
10
13
|
import { track } from "./functions/track/index.js";
|
|
11
14
|
export {
|
|
15
|
+
createRegisterComponentMessage,
|
|
16
|
+
getAllContent,
|
|
17
|
+
getContent,
|
|
18
|
+
isEditing,
|
|
19
|
+
isPreviewing,
|
|
20
|
+
processContentResult,
|
|
21
|
+
register,
|
|
22
|
+
setEditorSettings,
|
|
12
23
|
track
|
|
13
24
|
};
|