@builder.io/sdk-react-native 0.0.5 → 0.0.6-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/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-react-native",
3
3
  "description": "Builder.io SDK for React Native",
4
- "version": "0.0.5",
4
+ "version": "0.0.6-0",
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",
11
13
  "react-native-render-html": "^6.3.4",
14
+ "react-native-storage": "^1.0.1",
12
15
  "react-native-video": "^5.1.1"
13
16
  },
14
17
  "peerDependencies": {
@@ -1,38 +1,84 @@
1
- const handleABTesting = (item, testGroups) => {
2
- if (item.variations && Object.keys(item.variations).length) {
3
- const testGroup = item.id ? testGroups[item.id] : void 0;
4
- const variationValue = testGroup ? item.variations[testGroup] : void 0;
5
- if (testGroup && variationValue) {
6
- item.data = variationValue.data;
7
- item.testVariationId = variationValue.id;
8
- item.testVariationName = variationValue.name;
9
- } else {
10
- let n = 0;
11
- const random = Math.random();
12
- let set = false;
13
- for (const id in item.variations) {
14
- const variation = item.variations[id];
15
- const testRatio = variation.testRatio;
16
- n += testRatio;
17
- if (random < n) {
18
- const variationName = variation.name || (variation.id === item.id ? "Default variation" : "");
19
- set = true;
20
- Object.assign(item, {
21
- data: variation.data,
22
- testVariationId: variation.id,
23
- testVariationName: variationName
24
- });
25
- }
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);
26
8
  }
27
- if (!set) {
28
- Object.assign(item, {
29
- testVariationId: item.id,
30
- testVariationName: "Default"
31
- });
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
32
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 {
22
+ getContentVariationCookie,
23
+ setContentVariationCookie
24
+ } from "../../helpers/ab-tests.js";
25
+ const getRandomTestGroupId = (variations) => {
26
+ let n = 0;
27
+ const random = Math.random();
28
+ for (const id in variations) {
29
+ const testRatio = variations[id].testRatio;
30
+ n += testRatio;
31
+ if (random < n) {
32
+ return id;
33
33
  }
34
34
  }
35
+ return void 0;
35
36
  };
37
+ const getContentVariation = (_0) => __async(void 0, [_0], function* ({
38
+ item,
39
+ orgId
40
+ }) {
41
+ if (!item.id || !item.variations || Object.keys(item.variations).length === 0) {
42
+ return;
43
+ }
44
+ const testGroupId = yield getContentVariationCookie({
45
+ canTrack: true,
46
+ contentId: item.id,
47
+ orgId
48
+ });
49
+ console.log("found test group id", testGroupId);
50
+ const variationValue = testGroupId ? item.variations[testGroupId] : void 0;
51
+ if (variationValue) {
52
+ return variationValue;
53
+ } else {
54
+ const randomTestGroupId = getRandomTestGroupId(item.variations);
55
+ if (randomTestGroupId) {
56
+ yield setContentVariationCookie({
57
+ contentId: item.id,
58
+ value: randomTestGroupId,
59
+ canTrack: true,
60
+ orgId
61
+ });
62
+ }
63
+ const randomVariationValue = randomTestGroupId ? item.variations[randomTestGroupId] : void 0;
64
+ return randomVariationValue;
65
+ }
66
+ });
67
+ const handleABTesting = (_0) => __async(void 0, [_0], function* ({
68
+ item,
69
+ orgId
70
+ }) {
71
+ const variationValue = yield getContentVariation({ item, orgId });
72
+ const newValues = variationValue ? {
73
+ data: variationValue.data,
74
+ testVariationId: variationValue.id,
75
+ testVariationName: variationValue.name || (variationValue.id === item.id ? "Default" : "")
76
+ } : {
77
+ testVariationId: item.id,
78
+ testVariationName: "Default"
79
+ };
80
+ Object.assign(item, newValues);
81
+ });
36
82
  export {
37
83
  handleABTesting
38
84
  };
@@ -80,10 +80,8 @@ 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
- if (options.testGroups) {
84
- for (const item of content.results) {
85
- handleABTesting(item, options.testGroups);
86
- }
83
+ for (const item of content.results) {
84
+ yield handleABTesting({ item, orgId: options.apiKey });
87
85
  }
88
86
  return content;
89
87
  });
@@ -54,18 +54,21 @@ import { getSessionId } from "../helpers/sessionId.js";
54
54
  import { getVisitorId } from "../helpers/visitorId.js";
55
55
  import { isBrowser } from "./is-browser.js";
56
56
  import { isEditing } from "./is-editing.js";
57
- const getTrackingEventData = ({ canTrack }) => {
57
+ const getTrackingEventData = (_0) => __async(void 0, [_0], function* ({
58
+ canTrack,
59
+ orgId
60
+ }) {
58
61
  if (!canTrack) {
59
62
  return { visitorId: void 0, sessionId: void 0 };
60
63
  }
61
- const sessionId = getSessionId({ canTrack });
64
+ const sessionId = yield getSessionId({ canTrack, orgId });
62
65
  const visitorId = getVisitorId({ canTrack });
63
66
  return {
64
67
  sessionId,
65
68
  visitorId
66
69
  };
67
- };
68
- const createEvent = (_a) => {
70
+ });
71
+ const createEvent = (_a) => __async(void 0, null, function* () {
69
72
  var _b = _a, {
70
73
  type: eventType,
71
74
  canTrack,
@@ -79,12 +82,12 @@ const createEvent = (_a) => {
79
82
  ]);
80
83
  return {
81
84
  type: eventType,
82
- data: __spreadProps(__spreadValues(__spreadValues({}, properties), getTrackingEventData({ canTrack })), {
85
+ data: __spreadProps(__spreadValues(__spreadValues({}, properties), yield getTrackingEventData({ canTrack, orgId })), {
83
86
  ownerId: orgId,
84
87
  contentId
85
88
  })
86
89
  };
87
- };
90
+ });
88
91
  function track(eventProps) {
89
92
  return __async(this, null, function* () {
90
93
  if (!eventProps.canTrack) {
@@ -99,7 +102,7 @@ function track(eventProps) {
99
102
  return fetch(`https://builder.io/api/v1/track`, {
100
103
  method: "POST",
101
104
  body: JSON.stringify({
102
- events: [createEvent(eventProps)]
105
+ events: [yield createEvent(eventProps)]
103
106
  }),
104
107
  headers: {
105
108
  "content-type": "application/json"
@@ -0,0 +1,18 @@
1
+ import { getCookie, setCookie } from "./cookie.js";
2
+ const BUILDER_STORE_PREFIX = "builder.tests";
3
+ const getContentTestKey = (id) => `${BUILDER_STORE_PREFIX}.${id}`;
4
+ const getContentVariationCookie = ({
5
+ contentId,
6
+ canTrack,
7
+ orgId
8
+ }) => getCookie({ name: getContentTestKey(contentId), canTrack, orgId });
9
+ const setContentVariationCookie = ({
10
+ contentId,
11
+ canTrack,
12
+ value,
13
+ orgId
14
+ }) => setCookie({ name: getContentTestKey(contentId), value, canTrack, orgId });
15
+ export {
16
+ getContentVariationCookie,
17
+ setContentVariationCookie
18
+ };
@@ -1,58 +1,56 @@
1
- import { isBrowser } from "../functions/is-browser.js";
2
- import { getTopLevelDomain } from "./url.js";
3
- const getCookie = ({
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 "./storage";
22
+ const getCookie = (_0) => __async(void 0, [_0], function* ({
4
23
  name,
5
24
  canTrack
6
- }) => {
7
- var _a;
25
+ }) {
8
26
  try {
9
27
  if (!canTrack) {
10
28
  return void 0;
11
29
  }
12
- return (_a = document.cookie.split("; ").find((row) => row.startsWith(`${name}=`))) == null ? void 0 : _a.split("=")[1];
30
+ const parsedName = name.replace(/_/g, ".");
31
+ const data = yield (yield storage).load(parsedName);
32
+ console.log("data", data);
33
+ return data.value;
13
34
  } catch (err) {
14
35
  console.debug("[COOKIE] GET error: ", err);
15
36
  }
16
- };
17
- const stringifyCookie = (cookie) => cookie.map(([key, value]) => value ? `${key}=${value}` : key).join("; ");
18
- const SECURE_CONFIG = [
19
- ["secure", ""],
20
- ["SameSite", "None"]
21
- ];
22
- const createCookieString = ({
23
- name,
24
- value,
25
- expires
26
- }) => {
27
- const secure = isBrowser() ? location.protocol === "https:" : true;
28
- const secureObj = secure ? SECURE_CONFIG : [[]];
29
- const expiresObj = expires ? [["expires", expires.toUTCString()]] : [[]];
30
- const cookieValue = [
31
- [name, value],
32
- ...expiresObj,
33
- ["path", "/"],
34
- ["domain", getTopLevelDomain(window.location.hostname)],
35
- ...secureObj
36
- ];
37
- const cookie = stringifyCookie(cookieValue);
38
- return cookie;
39
- };
40
- const setCookie = ({
37
+ });
38
+ const setCookie = (_0) => __async(void 0, [_0], function* ({
41
39
  name,
42
40
  value,
43
41
  expires,
44
42
  canTrack
45
- }) => {
43
+ }) {
46
44
  try {
47
45
  if (!canTrack) {
48
46
  return void 0;
49
47
  }
50
- const cookie = createCookieString({ name, value, expires });
51
- document.cookie = cookie;
48
+ yield (yield storage).save({ key: name, data: { value }, expires });
49
+ console.log("data", yield storage);
52
50
  } catch (err) {
53
51
  console.warn("[COOKIE] SET error: ", err);
54
52
  }
55
- };
53
+ });
56
54
  export {
57
55
  getCookie,
58
56
  setCookie
File without changes
@@ -1,24 +1,49 @@
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
+ };
1
21
  import { getCookie, setCookie } from "./cookie.js";
2
22
  import { checkIsDefined } from "./nullable.js";
3
23
  import { uuid } from "./uuid.js";
4
24
  const SESSION_LOCAL_STORAGE_KEY = "builderSessionId";
5
- const getSessionId = ({ canTrack }) => {
25
+ const getSessionId = (_0) => __async(void 0, [_0], function* ({ canTrack, orgId }) {
6
26
  if (!canTrack) {
7
27
  return void 0;
8
28
  }
9
- const sessionId = getCookie({ name: SESSION_LOCAL_STORAGE_KEY, canTrack });
29
+ const sessionId = yield getCookie({
30
+ name: SESSION_LOCAL_STORAGE_KEY,
31
+ canTrack,
32
+ orgId
33
+ });
10
34
  if (checkIsDefined(sessionId)) {
11
35
  return sessionId;
12
36
  } else {
13
37
  const newSessionId = createSessionId();
14
- setSessionId({ id: newSessionId, canTrack });
38
+ setSessionId({ id: newSessionId, canTrack, orgId });
15
39
  }
16
- };
40
+ });
17
41
  const createSessionId = () => uuid();
18
42
  const setSessionId = ({
19
43
  id,
20
- canTrack
21
- }) => setCookie({ name: SESSION_LOCAL_STORAGE_KEY, value: id, canTrack });
44
+ canTrack,
45
+ orgId
46
+ }) => setCookie({ name: SESSION_LOCAL_STORAGE_KEY, value: id, canTrack, orgId });
22
47
  export {
23
48
  createSessionId,
24
49
  getSessionId,
@@ -0,0 +1,37 @@
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
+ };