@builder.io/sdk-svelte 0.0.1-30 → 0.0.1-31

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@builder.io/sdk-svelte",
3
3
  "description": "Builder.io SDK for Svelte",
4
- "version": "0.0.1-30",
4
+ "version": "0.0.1-31",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "src"
@@ -24,10 +24,10 @@
24
24
 
25
25
  export let content;
26
26
  export let data;
27
+ export let canTrack;
27
28
  export let context;
28
29
  export let customComponents;
29
30
  export let model;
30
- export let canTrack;
31
31
  export let apiKey;
32
32
 
33
33
  const processMessage = function processMessage(event) {
@@ -73,9 +73,12 @@
73
73
  };
74
74
 
75
75
  const onClick = function onClick(_event) {
76
- if (useContent() && canTrack !== false) {
77
- track("click", {
76
+ if (useContent()) {
77
+ track({
78
+ type: "click",
79
+ canTrack: canTrackToUse(),
78
80
  contentId: useContent().id,
81
+ orgId: apiKey,
79
82
  });
80
83
  }
81
84
  };
@@ -138,6 +141,10 @@
138
141
  return mergedContent;
139
142
  };
140
143
 
144
+ $: canTrackToUse = () => {
145
+ return canTrack || true;
146
+ };
147
+
141
148
  $: contentState = () => {
142
149
  return { ...content?.data?.state, ...data, ...overrideState };
143
150
  };
@@ -212,9 +219,12 @@
212
219
  );
213
220
  }
214
221
 
215
- if (useContent() && canTrack !== false) {
216
- track("impression", {
222
+ if (useContent()) {
223
+ track({
224
+ type: "impression",
225
+ canTrack: canTrackToUse(),
217
226
  contentId: useContent().id,
227
+ orgId: apiKey,
218
228
  });
219
229
  } // override normal content in preview mode
220
230
 
@@ -1,7 +1,74 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
1
32
  import { TARGET } from "../constants/target.js";
33
+ import { getSessionId } from "../helpers/sessionId.js";
34
+ import { getVisitorId } from "../helpers/visitorId.js";
2
35
  import { isBrowser } from "./is-browser.js";
3
36
  import { isEditing } from "./is-editing.js";
4
- function track(event, properties) {
37
+ const getTrackingEventData = ({ canTrack }) => {
38
+ if (!canTrack) {
39
+ return { visitorId: void 0, sessionId: void 0 };
40
+ }
41
+ const sessionId = getSessionId({ canTrack });
42
+ const visitorId = getVisitorId({ canTrack });
43
+ return {
44
+ sessionId,
45
+ visitorId
46
+ };
47
+ };
48
+ const createEvent = (_a) => {
49
+ var _b = _a, {
50
+ type: eventType,
51
+ canTrack,
52
+ orgId,
53
+ contentId
54
+ } = _b, properties = __objRest(_b, [
55
+ "type",
56
+ "canTrack",
57
+ "orgId",
58
+ "contentId"
59
+ ]);
60
+ return {
61
+ type: eventType,
62
+ data: __spreadProps(__spreadValues(__spreadValues({}, properties), getTrackingEventData({ canTrack })), {
63
+ ownerId: orgId,
64
+ contentId
65
+ })
66
+ };
67
+ };
68
+ function track(eventProps) {
69
+ if (!eventProps.canTrack) {
70
+ return;
71
+ }
5
72
  if (isEditing()) {
6
73
  return;
7
74
  }
@@ -10,7 +77,9 @@ function track(event, properties) {
10
77
  }
11
78
  return fetch(`https://builder.io/api/v1/track`, {
12
79
  method: "POST",
13
- body: JSON.stringify({ events: [{ type: event, data: properties }] }),
80
+ body: JSON.stringify({
81
+ events: [createEvent(eventProps)]
82
+ }),
14
83
  headers: {
15
84
  "content-type": "application/json"
16
85
  },
@@ -0,0 +1,59 @@
1
+ import { isBrowser } from "../functions/is-browser";
2
+ import { getTopLevelDomain } from "./url";
3
+ const getCookie = ({
4
+ name,
5
+ canTrack
6
+ }) => {
7
+ var _a;
8
+ try {
9
+ if (!canTrack) {
10
+ return void 0;
11
+ }
12
+ return (_a = document.cookie.split("; ").find((row) => row.startsWith(`${name}=`))) == null ? void 0 : _a.split("=")[1];
13
+ } catch (err) {
14
+ console.debug("[COOKIE] GET error: ", err);
15
+ }
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 = ({
41
+ name,
42
+ value,
43
+ expires,
44
+ canTrack
45
+ }) => {
46
+ try {
47
+ if (!canTrack) {
48
+ return void 0;
49
+ }
50
+ const cookie = createCookieString({ name, value, expires });
51
+ document.cookie = cookie;
52
+ } catch (err) {
53
+ console.warn("[COOKIE] SET error: ", err);
54
+ }
55
+ };
56
+ export {
57
+ getCookie,
58
+ setCookie
59
+ };
@@ -0,0 +1,34 @@
1
+ import { isBrowser } from "../functions/is-browser";
2
+ const getLocalStorage = () => isBrowser() && typeof localStorage !== "undefined" ? localStorage : void 0;
3
+ const getLocalStorageItem = ({
4
+ key,
5
+ canTrack
6
+ }) => {
7
+ var _a;
8
+ try {
9
+ if (canTrack) {
10
+ return (_a = getLocalStorage()) == null ? void 0 : _a.getItem(key);
11
+ }
12
+ return void 0;
13
+ } catch (err) {
14
+ console.debug("[LocalStorage] GET error: ", err);
15
+ }
16
+ };
17
+ const setLocalStorageItem = ({
18
+ key,
19
+ canTrack,
20
+ value
21
+ }) => {
22
+ var _a;
23
+ try {
24
+ if (canTrack) {
25
+ (_a = getLocalStorage()) == null ? void 0 : _a.setItem(key, value);
26
+ }
27
+ } catch (err) {
28
+ console.debug("[LocalStorage] SET error: ", err);
29
+ }
30
+ };
31
+ export {
32
+ getLocalStorageItem,
33
+ setLocalStorageItem
34
+ };
@@ -0,0 +1,4 @@
1
+ const checkIsDefined = (maybeT) => maybeT !== null && maybeT !== void 0;
2
+ export {
3
+ checkIsDefined
4
+ };
@@ -0,0 +1,26 @@
1
+ import { getCookie, setCookie } from "./cookie";
2
+ import { checkIsDefined } from "./nullable";
3
+ import { uuid } from "./uuid";
4
+ const SESSION_LOCAL_STORAGE_KEY = "builderSessionId";
5
+ const getSessionId = ({ canTrack }) => {
6
+ if (!canTrack) {
7
+ return void 0;
8
+ }
9
+ const sessionId = getCookie({ name: SESSION_LOCAL_STORAGE_KEY, canTrack });
10
+ if (checkIsDefined(sessionId)) {
11
+ return sessionId;
12
+ } else {
13
+ const newSessionId = createSessionId();
14
+ setSessionId({ id: newSessionId, canTrack });
15
+ }
16
+ };
17
+ const createSessionId = () => uuid();
18
+ const setSessionId = ({
19
+ id,
20
+ canTrack
21
+ }) => setCookie({ name: SESSION_LOCAL_STORAGE_KEY, value: id, canTrack });
22
+ export {
23
+ createSessionId,
24
+ getSessionId,
25
+ setSessionId
26
+ };
@@ -0,0 +1,5 @@
1
+ const MINUTE_TO_MILLESECONDS_MULTIPLIER = 6e4;
2
+ const getCurrentDatePlusMinutes = (minutes = 30) => new Date(Date.now() + minutes * MINUTE_TO_MILLESECONDS_MULTIPLIER);
3
+ export {
4
+ getCurrentDatePlusMinutes
5
+ };
@@ -0,0 +1,10 @@
1
+ const getTopLevelDomain = (host) => {
2
+ const parts = host.split(".");
3
+ if (parts.length > 2) {
4
+ return parts.slice(1).join(".");
5
+ }
6
+ return host;
7
+ };
8
+ export {
9
+ getTopLevelDomain
10
+ };
@@ -0,0 +1,15 @@
1
+ import { getTopLevelDomain } from "./url.js";
2
+ describe("getTopLevelDomain", () => {
3
+ test("handles root domain", () => {
4
+ const output = getTopLevelDomain("example.com");
5
+ expect(output).toBe("example.com");
6
+ });
7
+ test("handles subdomain", () => {
8
+ const output = getTopLevelDomain("wwww.example.com");
9
+ expect(output).toBe("example.com");
10
+ });
11
+ test("handles subdomain with long suffix", () => {
12
+ const output = getTopLevelDomain("www.example.co.uk");
13
+ expect(output).toBe("example.co.uk");
14
+ });
15
+ });
@@ -0,0 +1,13 @@
1
+ function uuidv4() {
2
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
3
+ const r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
4
+ return v.toString(16);
5
+ });
6
+ }
7
+ function uuid() {
8
+ return uuidv4().replace(/-/g, "");
9
+ }
10
+ export {
11
+ uuid,
12
+ uuidv4
13
+ };
@@ -0,0 +1,33 @@
1
+ import { getLocalStorageItem, setLocalStorageItem } from "./localStorage";
2
+ import { checkIsDefined } from "./nullable";
3
+ import { uuid } from "./uuid";
4
+ const VISITOR_LOCAL_STORAGE_KEY = "builderVisitorId";
5
+ const getVisitorId = ({ canTrack }) => {
6
+ if (!canTrack) {
7
+ return void 0;
8
+ }
9
+ const visitorId = getLocalStorageItem({
10
+ key: VISITOR_LOCAL_STORAGE_KEY,
11
+ canTrack
12
+ });
13
+ if (checkIsDefined(visitorId)) {
14
+ return visitorId;
15
+ } else {
16
+ const newVisitorId = createVisitorId();
17
+ setVisitorId({ id: newVisitorId, canTrack });
18
+ }
19
+ };
20
+ const createVisitorId = () => uuid();
21
+ const setVisitorId = ({
22
+ id,
23
+ canTrack
24
+ }) => setLocalStorageItem({
25
+ key: VISITOR_LOCAL_STORAGE_KEY,
26
+ value: id,
27
+ canTrack
28
+ });
29
+ export {
30
+ createVisitorId,
31
+ getVisitorId,
32
+ setVisitorId
33
+ };
File without changes