@builder.io/sdk-svelte 5.1.0 → 5.1.1

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.
Files changed (41) hide show
  1. package/lib/browser/components/block/components/block-wrapper.svelte +6 -0
  2. package/lib/browser/components/block/components/interactive-element.svelte +6 -0
  3. package/lib/browser/constants/sdk-version.d.ts +1 -1
  4. package/lib/browser/constants/sdk-version.js +1 -1
  5. package/lib/browser/functions/evaluate/evaluate.d.ts +1 -1
  6. package/lib/browser/functions/evaluate/evaluate.js +2 -2
  7. package/lib/browser/functions/evaluate/helpers.d.ts +9 -1
  8. package/lib/browser/functions/evaluate/helpers.js +21 -2
  9. package/lib/browser/functions/get-block-actions-handler.d.ts +2 -0
  10. package/lib/browser/functions/get-block-actions-handler.js +2 -1
  11. package/lib/browser/functions/get-block-actions.d.ts +2 -0
  12. package/lib/edge/components/block/components/block-wrapper.svelte +6 -0
  13. package/lib/edge/components/block/components/interactive-element.svelte +6 -0
  14. package/lib/edge/constants/sdk-version.d.ts +1 -1
  15. package/lib/edge/constants/sdk-version.js +1 -1
  16. package/lib/edge/functions/evaluate/evaluate.d.ts +1 -1
  17. package/lib/edge/functions/evaluate/evaluate.js +2 -2
  18. package/lib/edge/functions/evaluate/helpers.d.ts +9 -1
  19. package/lib/edge/functions/evaluate/helpers.js +21 -2
  20. package/lib/edge/functions/get-block-actions-handler.d.ts +2 -0
  21. package/lib/edge/functions/get-block-actions-handler.js +2 -1
  22. package/lib/edge/functions/get-block-actions.d.ts +2 -0
  23. package/lib/node/components/block/components/block-wrapper.svelte +6 -0
  24. package/lib/node/components/block/components/interactive-element.svelte +6 -0
  25. package/lib/node/constants/sdk-version.d.ts +1 -1
  26. package/lib/node/constants/sdk-version.js +1 -1
  27. package/lib/node/functions/evaluate/evaluate.d.ts +1 -1
  28. package/lib/node/functions/evaluate/evaluate.js +2 -2
  29. package/lib/node/functions/evaluate/helpers.d.ts +9 -1
  30. package/lib/node/functions/evaluate/helpers.js +21 -2
  31. package/lib/node/functions/get-block-actions-handler.d.ts +2 -0
  32. package/lib/node/functions/get-block-actions-handler.js +2 -1
  33. package/lib/node/functions/get-block-actions.d.ts +2 -0
  34. package/package.json +1 -1
  35. package/src/components/block/components/block-wrapper.svelte +6 -0
  36. package/src/components/block/components/interactive-element.svelte +6 -0
  37. package/src/constants/sdk-version.ts +1 -1
  38. package/src/functions/evaluate/evaluate.ts +3 -2
  39. package/src/functions/evaluate/helpers.ts +29 -2
  40. package/src/functions/get-block-actions-handler.ts +4 -1
  41. package/src/functions/get-block-actions.ts +2 -0
@@ -24,5 +24,11 @@ export let context;
24
24
  localState: $context.localState,
25
25
  context: $context.context,
26
26
  stripPrefix: true,
27
+ trackingContext: {
28
+ apiKey: $context.apiKey,
29
+ canTrack: $context.canTrack ?? true,
30
+ contentId: $context.content?.id,
31
+ variationId: $context.content?.testVariationId,
32
+ },
27
33
  })}><slot /></DynamicRenderer
28
34
  >
@@ -24,6 +24,12 @@ $: attributes = () => {
24
24
  rootSetState: $context.rootSetState,
25
25
  localState: $context.localState,
26
26
  context: $context.context,
27
+ trackingContext: {
28
+ apiKey: $context.apiKey,
29
+ canTrack: $context.canTrack ?? true,
30
+ contentId: $context.content?.id,
31
+ variationId: $context.content?.testVariationId,
32
+ },
27
33
  }),
28
34
  }
29
35
  : {};
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "5.1.0";
1
+ export declare const SDK_VERSION = "5.1.1";
@@ -1 +1 @@
1
- export const SDK_VERSION = "5.1.0";
1
+ export const SDK_VERSION = "5.1.1";
@@ -1,5 +1,5 @@
1
1
  import type { EvaluatorArgs } from './helpers';
2
2
  type EvalValue = unknown;
3
3
  export declare const getSimpleExpressionGetPath: (code: string) => string | undefined;
4
- export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression }: EvaluatorArgs): EvalValue;
4
+ export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression, trackingContext }: EvaluatorArgs): EvalValue;
5
5
  export {};
@@ -16,7 +16,7 @@ const VIRTUAL_INDEX_REGEX = /(\s)*var(\s)+_virtual_index(\s)*=(\s)*state(?<getPa
16
16
  export const getSimpleExpressionGetPath = (code) => {
17
17
  return STATE_GETTER_REGEX.exec(code.trim())?.groups?.getPath?.slice(1) || VIRTUAL_INDEX_REGEX.exec(code.trim())?.groups?.getPath?.slice(1);
18
18
  };
19
- export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true }) {
19
+ export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true, trackingContext }) {
20
20
  if (code.trim() === '') {
21
21
  return undefined;
22
22
  }
@@ -37,7 +37,7 @@ export function evaluate({ code, context, localState, rootState, rootSetState, e
37
37
  code: parseCode(code, {
38
38
  isExpression
39
39
  }),
40
- builder: getBuilderGlobals(),
40
+ builder: getBuilderGlobals(trackingContext),
41
41
  context,
42
42
  event,
43
43
  rootSetState,
@@ -1,14 +1,22 @@
1
1
  import type { BuilderContextInterface, BuilderRenderState } from '../../context/types';
2
2
  import { getUserAttributes } from '../track/helpers';
3
+ export type TrackingContext = {
4
+ apiKey: string | null;
5
+ canTrack?: boolean;
6
+ contentId?: string;
7
+ variationId?: string;
8
+ };
3
9
  export type EvaluatorArgs = Omit<ExecutorArgs, 'builder' | 'event'> & {
4
10
  event?: Event;
5
11
  isExpression?: boolean;
12
+ trackingContext?: TrackingContext;
6
13
  };
7
14
  export type BuilderGlobals = {
8
15
  isEditing: boolean | undefined;
9
16
  isBrowser: boolean | undefined;
10
17
  isServer: boolean | undefined;
11
18
  getUserAttributes: typeof getUserAttributes;
19
+ trackConversion: (amount?: number, customProperties?: Record<string, any>) => void;
12
20
  };
13
21
  export type ExecutorArgs = Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'> & {
14
22
  code: string;
@@ -20,7 +28,7 @@ export type FunctionArguments = ReturnType<typeof getFunctionArguments>;
20
28
  export declare const getFunctionArguments: ({ builder, context, event, state }: Pick<ExecutorArgs, "context" | "builder" | "event"> & {
21
29
  state: BuilderRenderState;
22
30
  }) => [string, Event | BuilderRenderState | import("../../context/types").BuilderRenderContext | BuilderGlobals | undefined][];
23
- export declare const getBuilderGlobals: () => BuilderGlobals;
31
+ export declare const getBuilderGlobals: (trackingContext?: TrackingContext) => BuilderGlobals;
24
32
  export declare const parseCode: (code: string, { isExpression }: Pick<EvaluatorArgs, 'isExpression'>) => string;
25
33
  export declare function flattenState({ rootState, localState, rootSetState }: {
26
34
  rootState: Record<string | symbol, any>;
@@ -1,6 +1,7 @@
1
1
  import { isBrowser } from '../is-browser';
2
2
  import { isEditing } from '../is-editing';
3
3
  import { getUserAttributes } from '../track/helpers';
4
+ import { _track } from '../track/index';
4
5
  export const getFunctionArguments = ({ builder, context, event, state }) => {
5
6
  return Object.entries({
6
7
  state,
@@ -11,11 +12,29 @@ export const getFunctionArguments = ({ builder, context, event, state }) => {
11
12
  event
12
13
  });
13
14
  };
14
- export const getBuilderGlobals = () => ({
15
+ export const getBuilderGlobals = (trackingContext) => ({
15
16
  isEditing: isEditing(),
16
17
  isBrowser: isBrowser(),
17
18
  isServer: !isBrowser(),
18
- getUserAttributes: () => getUserAttributes()
19
+ getUserAttributes: () => getUserAttributes(),
20
+ trackConversion: (amount, customProperties) => {
21
+ if (!trackingContext?.apiKey || trackingContext?.canTrack === false) {
22
+ return;
23
+ }
24
+ _track({
25
+ type: 'conversion',
26
+ apiKey: trackingContext.apiKey,
27
+ canTrack: trackingContext.canTrack ?? true,
28
+ contentId: trackingContext.contentId,
29
+ variationId: trackingContext.variationId !== trackingContext.contentId ? trackingContext.variationId : undefined,
30
+ metadata: {
31
+ ...(customProperties || {}),
32
+ ...(amount !== undefined ? {
33
+ amount
34
+ } : {})
35
+ }
36
+ });
37
+ }
19
38
  });
20
39
  export const parseCode = (code, { isExpression = true }) => {
21
40
  // Be able to handle simple expressions like "state.foo" or "1 + 1"
@@ -1,7 +1,9 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  type Options = {
4
5
  block: BuilderBlock;
6
+ trackingContext?: TrackingContext;
5
7
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>;
6
8
  type EventHandler = (event: Event) => any;
7
9
  export declare const createEventHandler: (value: string, options: Options) => EventHandler;
@@ -6,5 +6,6 @@ export const createEventHandler = (value, options) => event => evaluate({
6
6
  rootState: options.rootState,
7
7
  rootSetState: options.rootSetState,
8
8
  event,
9
- isExpression: false
9
+ isExpression: false,
10
+ trackingContext: options.trackingContext
10
11
  });
@@ -1,10 +1,12 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  type Actions = {
4
5
  [index: string]: (event: Event) => any;
5
6
  };
6
7
  export declare function getBlockActions(options: {
7
8
  block: BuilderBlock;
8
9
  stripPrefix?: boolean;
10
+ trackingContext?: TrackingContext;
9
11
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): Actions;
10
12
  export {};
@@ -24,5 +24,11 @@ export let context;
24
24
  localState: $context.localState,
25
25
  context: $context.context,
26
26
  stripPrefix: true,
27
+ trackingContext: {
28
+ apiKey: $context.apiKey,
29
+ canTrack: $context.canTrack ?? true,
30
+ contentId: $context.content?.id,
31
+ variationId: $context.content?.testVariationId,
32
+ },
27
33
  })}><slot /></DynamicRenderer
28
34
  >
@@ -24,6 +24,12 @@ $: attributes = () => {
24
24
  rootSetState: $context.rootSetState,
25
25
  localState: $context.localState,
26
26
  context: $context.context,
27
+ trackingContext: {
28
+ apiKey: $context.apiKey,
29
+ canTrack: $context.canTrack ?? true,
30
+ contentId: $context.content?.id,
31
+ variationId: $context.content?.testVariationId,
32
+ },
27
33
  }),
28
34
  }
29
35
  : {};
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "5.1.0";
1
+ export declare const SDK_VERSION = "5.1.1";
@@ -1 +1 @@
1
- export const SDK_VERSION = "5.1.0";
1
+ export const SDK_VERSION = "5.1.1";
@@ -1,5 +1,5 @@
1
1
  import type { EvaluatorArgs } from './helpers';
2
2
  type EvalValue = unknown;
3
3
  export declare const getSimpleExpressionGetPath: (code: string) => string | undefined;
4
- export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression }: EvaluatorArgs): EvalValue;
4
+ export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression, trackingContext }: EvaluatorArgs): EvalValue;
5
5
  export {};
@@ -16,7 +16,7 @@ const VIRTUAL_INDEX_REGEX = /(\s)*var(\s)+_virtual_index(\s)*=(\s)*state(?<getPa
16
16
  export const getSimpleExpressionGetPath = (code) => {
17
17
  return STATE_GETTER_REGEX.exec(code.trim())?.groups?.getPath?.slice(1) || VIRTUAL_INDEX_REGEX.exec(code.trim())?.groups?.getPath?.slice(1);
18
18
  };
19
- export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true }) {
19
+ export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true, trackingContext }) {
20
20
  if (code.trim() === '') {
21
21
  return undefined;
22
22
  }
@@ -37,7 +37,7 @@ export function evaluate({ code, context, localState, rootState, rootSetState, e
37
37
  code: parseCode(code, {
38
38
  isExpression
39
39
  }),
40
- builder: getBuilderGlobals(),
40
+ builder: getBuilderGlobals(trackingContext),
41
41
  context,
42
42
  event,
43
43
  rootSetState,
@@ -1,14 +1,22 @@
1
1
  import type { BuilderContextInterface, BuilderRenderState } from '../../context/types';
2
2
  import { getUserAttributes } from '../track/helpers';
3
+ export type TrackingContext = {
4
+ apiKey: string | null;
5
+ canTrack?: boolean;
6
+ contentId?: string;
7
+ variationId?: string;
8
+ };
3
9
  export type EvaluatorArgs = Omit<ExecutorArgs, 'builder' | 'event'> & {
4
10
  event?: Event;
5
11
  isExpression?: boolean;
12
+ trackingContext?: TrackingContext;
6
13
  };
7
14
  export type BuilderGlobals = {
8
15
  isEditing: boolean | undefined;
9
16
  isBrowser: boolean | undefined;
10
17
  isServer: boolean | undefined;
11
18
  getUserAttributes: typeof getUserAttributes;
19
+ trackConversion: (amount?: number, customProperties?: Record<string, any>) => void;
12
20
  };
13
21
  export type ExecutorArgs = Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'> & {
14
22
  code: string;
@@ -20,7 +28,7 @@ export type FunctionArguments = ReturnType<typeof getFunctionArguments>;
20
28
  export declare const getFunctionArguments: ({ builder, context, event, state }: Pick<ExecutorArgs, "context" | "builder" | "event"> & {
21
29
  state: BuilderRenderState;
22
30
  }) => [string, Event | BuilderRenderState | import("../../context/types").BuilderRenderContext | BuilderGlobals | undefined][];
23
- export declare const getBuilderGlobals: () => BuilderGlobals;
31
+ export declare const getBuilderGlobals: (trackingContext?: TrackingContext) => BuilderGlobals;
24
32
  export declare const parseCode: (code: string, { isExpression }: Pick<EvaluatorArgs, 'isExpression'>) => string;
25
33
  export declare function flattenState({ rootState, localState, rootSetState }: {
26
34
  rootState: Record<string | symbol, any>;
@@ -1,6 +1,7 @@
1
1
  import { isBrowser } from '../is-browser';
2
2
  import { isEditing } from '../is-editing';
3
3
  import { getUserAttributes } from '../track/helpers';
4
+ import { _track } from '../track/index';
4
5
  export const getFunctionArguments = ({ builder, context, event, state }) => {
5
6
  return Object.entries({
6
7
  state,
@@ -11,11 +12,29 @@ export const getFunctionArguments = ({ builder, context, event, state }) => {
11
12
  event
12
13
  });
13
14
  };
14
- export const getBuilderGlobals = () => ({
15
+ export const getBuilderGlobals = (trackingContext) => ({
15
16
  isEditing: isEditing(),
16
17
  isBrowser: isBrowser(),
17
18
  isServer: !isBrowser(),
18
- getUserAttributes: () => getUserAttributes()
19
+ getUserAttributes: () => getUserAttributes(),
20
+ trackConversion: (amount, customProperties) => {
21
+ if (!trackingContext?.apiKey || trackingContext?.canTrack === false) {
22
+ return;
23
+ }
24
+ _track({
25
+ type: 'conversion',
26
+ apiKey: trackingContext.apiKey,
27
+ canTrack: trackingContext.canTrack ?? true,
28
+ contentId: trackingContext.contentId,
29
+ variationId: trackingContext.variationId !== trackingContext.contentId ? trackingContext.variationId : undefined,
30
+ metadata: {
31
+ ...(customProperties || {}),
32
+ ...(amount !== undefined ? {
33
+ amount
34
+ } : {})
35
+ }
36
+ });
37
+ }
19
38
  });
20
39
  export const parseCode = (code, { isExpression = true }) => {
21
40
  // Be able to handle simple expressions like "state.foo" or "1 + 1"
@@ -1,7 +1,9 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  type Options = {
4
5
  block: BuilderBlock;
6
+ trackingContext?: TrackingContext;
5
7
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>;
6
8
  type EventHandler = (event: Event) => any;
7
9
  export declare const createEventHandler: (value: string, options: Options) => EventHandler;
@@ -6,5 +6,6 @@ export const createEventHandler = (value, options) => event => evaluate({
6
6
  rootState: options.rootState,
7
7
  rootSetState: options.rootSetState,
8
8
  event,
9
- isExpression: false
9
+ isExpression: false,
10
+ trackingContext: options.trackingContext
10
11
  });
@@ -1,10 +1,12 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  type Actions = {
4
5
  [index: string]: (event: Event) => any;
5
6
  };
6
7
  export declare function getBlockActions(options: {
7
8
  block: BuilderBlock;
8
9
  stripPrefix?: boolean;
10
+ trackingContext?: TrackingContext;
9
11
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): Actions;
10
12
  export {};
@@ -24,5 +24,11 @@ export let context;
24
24
  localState: $context.localState,
25
25
  context: $context.context,
26
26
  stripPrefix: true,
27
+ trackingContext: {
28
+ apiKey: $context.apiKey,
29
+ canTrack: $context.canTrack ?? true,
30
+ contentId: $context.content?.id,
31
+ variationId: $context.content?.testVariationId,
32
+ },
27
33
  })}><slot /></DynamicRenderer
28
34
  >
@@ -24,6 +24,12 @@ $: attributes = () => {
24
24
  rootSetState: $context.rootSetState,
25
25
  localState: $context.localState,
26
26
  context: $context.context,
27
+ trackingContext: {
28
+ apiKey: $context.apiKey,
29
+ canTrack: $context.canTrack ?? true,
30
+ contentId: $context.content?.id,
31
+ variationId: $context.content?.testVariationId,
32
+ },
27
33
  }),
28
34
  }
29
35
  : {};
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "5.1.0";
1
+ export declare const SDK_VERSION = "5.1.1";
@@ -1 +1 @@
1
- export const SDK_VERSION = "5.1.0";
1
+ export const SDK_VERSION = "5.1.1";
@@ -1,5 +1,5 @@
1
1
  import type { EvaluatorArgs } from './helpers';
2
2
  type EvalValue = unknown;
3
3
  export declare const getSimpleExpressionGetPath: (code: string) => string | undefined;
4
- export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression }: EvaluatorArgs): EvalValue;
4
+ export declare function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression, trackingContext }: EvaluatorArgs): EvalValue;
5
5
  export {};
@@ -16,7 +16,7 @@ const VIRTUAL_INDEX_REGEX = /(\s)*var(\s)+_virtual_index(\s)*=(\s)*state(?<getPa
16
16
  export const getSimpleExpressionGetPath = (code) => {
17
17
  return STATE_GETTER_REGEX.exec(code.trim())?.groups?.getPath?.slice(1) || VIRTUAL_INDEX_REGEX.exec(code.trim())?.groups?.getPath?.slice(1);
18
18
  };
19
- export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true }) {
19
+ export function evaluate({ code, context, localState, rootState, rootSetState, event, isExpression = true, trackingContext }) {
20
20
  if (code.trim() === '') {
21
21
  return undefined;
22
22
  }
@@ -37,7 +37,7 @@ export function evaluate({ code, context, localState, rootState, rootSetState, e
37
37
  code: parseCode(code, {
38
38
  isExpression
39
39
  }),
40
- builder: getBuilderGlobals(),
40
+ builder: getBuilderGlobals(trackingContext),
41
41
  context,
42
42
  event,
43
43
  rootSetState,
@@ -1,14 +1,22 @@
1
1
  import type { BuilderContextInterface, BuilderRenderState } from '../../context/types';
2
2
  import { getUserAttributes } from '../track/helpers';
3
+ export type TrackingContext = {
4
+ apiKey: string | null;
5
+ canTrack?: boolean;
6
+ contentId?: string;
7
+ variationId?: string;
8
+ };
3
9
  export type EvaluatorArgs = Omit<ExecutorArgs, 'builder' | 'event'> & {
4
10
  event?: Event;
5
11
  isExpression?: boolean;
12
+ trackingContext?: TrackingContext;
6
13
  };
7
14
  export type BuilderGlobals = {
8
15
  isEditing: boolean | undefined;
9
16
  isBrowser: boolean | undefined;
10
17
  isServer: boolean | undefined;
11
18
  getUserAttributes: typeof getUserAttributes;
19
+ trackConversion: (amount?: number, customProperties?: Record<string, any>) => void;
12
20
  };
13
21
  export type ExecutorArgs = Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'> & {
14
22
  code: string;
@@ -20,7 +28,7 @@ export type FunctionArguments = ReturnType<typeof getFunctionArguments>;
20
28
  export declare const getFunctionArguments: ({ builder, context, event, state }: Pick<ExecutorArgs, "context" | "builder" | "event"> & {
21
29
  state: BuilderRenderState;
22
30
  }) => [string, Event | BuilderRenderState | import("../../context/types").BuilderRenderContext | BuilderGlobals | undefined][];
23
- export declare const getBuilderGlobals: () => BuilderGlobals;
31
+ export declare const getBuilderGlobals: (trackingContext?: TrackingContext) => BuilderGlobals;
24
32
  export declare const parseCode: (code: string, { isExpression }: Pick<EvaluatorArgs, 'isExpression'>) => string;
25
33
  export declare function flattenState({ rootState, localState, rootSetState }: {
26
34
  rootState: Record<string | symbol, any>;
@@ -1,6 +1,7 @@
1
1
  import { isBrowser } from '../is-browser';
2
2
  import { isEditing } from '../is-editing';
3
3
  import { getUserAttributes } from '../track/helpers';
4
+ import { _track } from '../track/index';
4
5
  export const getFunctionArguments = ({ builder, context, event, state }) => {
5
6
  return Object.entries({
6
7
  state,
@@ -11,11 +12,29 @@ export const getFunctionArguments = ({ builder, context, event, state }) => {
11
12
  event
12
13
  });
13
14
  };
14
- export const getBuilderGlobals = () => ({
15
+ export const getBuilderGlobals = (trackingContext) => ({
15
16
  isEditing: isEditing(),
16
17
  isBrowser: isBrowser(),
17
18
  isServer: !isBrowser(),
18
- getUserAttributes: () => getUserAttributes()
19
+ getUserAttributes: () => getUserAttributes(),
20
+ trackConversion: (amount, customProperties) => {
21
+ if (!trackingContext?.apiKey || trackingContext?.canTrack === false) {
22
+ return;
23
+ }
24
+ _track({
25
+ type: 'conversion',
26
+ apiKey: trackingContext.apiKey,
27
+ canTrack: trackingContext.canTrack ?? true,
28
+ contentId: trackingContext.contentId,
29
+ variationId: trackingContext.variationId !== trackingContext.contentId ? trackingContext.variationId : undefined,
30
+ metadata: {
31
+ ...(customProperties || {}),
32
+ ...(amount !== undefined ? {
33
+ amount
34
+ } : {})
35
+ }
36
+ });
37
+ }
19
38
  });
20
39
  export const parseCode = (code, { isExpression = true }) => {
21
40
  // Be able to handle simple expressions like "state.foo" or "1 + 1"
@@ -1,7 +1,9 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  type Options = {
4
5
  block: BuilderBlock;
6
+ trackingContext?: TrackingContext;
5
7
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>;
6
8
  type EventHandler = (event: Event) => any;
7
9
  export declare const createEventHandler: (value: string, options: Options) => EventHandler;
@@ -6,5 +6,6 @@ export const createEventHandler = (value, options) => event => evaluate({
6
6
  rootState: options.rootState,
7
7
  rootSetState: options.rootSetState,
8
8
  event,
9
- isExpression: false
9
+ isExpression: false,
10
+ trackingContext: options.trackingContext
10
11
  });
@@ -1,10 +1,12 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  type Actions = {
4
5
  [index: string]: (event: Event) => any;
5
6
  };
6
7
  export declare function getBlockActions(options: {
7
8
  block: BuilderBlock;
8
9
  stripPrefix?: boolean;
10
+ trackingContext?: TrackingContext;
9
11
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): Actions;
10
12
  export {};
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": "5.1.0",
4
+ "version": "5.1.1",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "lib",
@@ -37,5 +37,11 @@
37
37
  localState: $context.localState,
38
38
  context: $context.context,
39
39
  stripPrefix: true,
40
+ trackingContext: {
41
+ apiKey: $context.apiKey,
42
+ canTrack: $context.canTrack ?? true,
43
+ contentId: $context.content?.id,
44
+ variationId: $context.content?.testVariationId,
45
+ },
40
46
  })}><slot /></DynamicRenderer
41
47
  >
@@ -40,6 +40,12 @@
40
40
  rootSetState: $context.rootSetState,
41
41
  localState: $context.localState,
42
42
  context: $context.context,
43
+ trackingContext: {
44
+ apiKey: $context.apiKey,
45
+ canTrack: $context.canTrack ?? true,
46
+ contentId: $context.content?.id,
47
+ variationId: $context.content?.testVariationId,
48
+ },
43
49
  }),
44
50
  }
45
51
  : {};
@@ -1 +1 @@
1
- export const SDK_VERSION = "5.1.0"
1
+ export const SDK_VERSION = "5.1.1"
@@ -27,7 +27,8 @@ export function evaluate({
27
27
  rootState,
28
28
  rootSetState,
29
29
  event,
30
- isExpression = true
30
+ isExpression = true,
31
+ trackingContext
31
32
  }: EvaluatorArgs): EvalValue {
32
33
  if (code.trim() === '') {
33
34
  return undefined;
@@ -50,7 +51,7 @@ export function evaluate({
50
51
  code: parseCode(code, {
51
52
  isExpression
52
53
  }),
53
- builder: getBuilderGlobals(),
54
+ builder: getBuilderGlobals(trackingContext),
54
55
  context,
55
56
  event,
56
57
  rootSetState,
@@ -2,15 +2,24 @@ import type { BuilderContextInterface, BuilderRenderState } from '../../context/
2
2
  import { isBrowser } from '../is-browser';
3
3
  import { isEditing } from '../is-editing';
4
4
  import { getUserAttributes } from '../track/helpers';
5
+ import { _track } from '../track/index';
6
+ export type TrackingContext = {
7
+ apiKey: string | null;
8
+ canTrack?: boolean;
9
+ contentId?: string;
10
+ variationId?: string;
11
+ };
5
12
  export type EvaluatorArgs = Omit<ExecutorArgs, 'builder' | 'event'> & {
6
13
  event?: Event;
7
14
  isExpression?: boolean;
15
+ trackingContext?: TrackingContext;
8
16
  };
9
17
  export type BuilderGlobals = {
10
18
  isEditing: boolean | undefined;
11
19
  isBrowser: boolean | undefined;
12
20
  isServer: boolean | undefined;
13
21
  getUserAttributes: typeof getUserAttributes;
22
+ trackConversion: (amount?: number, customProperties?: Record<string, any>) => void;
14
23
  };
15
24
  export type ExecutorArgs = Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'> & {
16
25
  code: string;
@@ -36,11 +45,29 @@ export const getFunctionArguments = ({
36
45
  event
37
46
  });
38
47
  };
39
- export const getBuilderGlobals = (): BuilderGlobals => ({
48
+ export const getBuilderGlobals = (trackingContext?: TrackingContext): BuilderGlobals => ({
40
49
  isEditing: isEditing(),
41
50
  isBrowser: isBrowser(),
42
51
  isServer: !isBrowser(),
43
- getUserAttributes: () => getUserAttributes()
52
+ getUserAttributes: () => getUserAttributes(),
53
+ trackConversion: (amount?: number, customProperties?: Record<string, any>) => {
54
+ if (!trackingContext?.apiKey || trackingContext?.canTrack === false) {
55
+ return;
56
+ }
57
+ _track({
58
+ type: 'conversion',
59
+ apiKey: trackingContext.apiKey,
60
+ canTrack: trackingContext.canTrack ?? true,
61
+ contentId: trackingContext.contentId,
62
+ variationId: trackingContext.variationId !== trackingContext.contentId ? trackingContext.variationId : undefined,
63
+ metadata: {
64
+ ...(customProperties || {}),
65
+ ...(amount !== undefined ? {
66
+ amount
67
+ } : {})
68
+ }
69
+ });
70
+ }
44
71
  });
45
72
  export const parseCode = (code: string, {
46
73
  isExpression = true
@@ -1,8 +1,10 @@
1
1
  import type { BuilderContextInterface } from '../context/types';
2
2
  import type { BuilderBlock } from '../types/builder-block';
3
+ import type { TrackingContext } from './evaluate/helpers';
3
4
  import { evaluate } from './evaluate/index';
4
5
  type Options = {
5
6
  block: BuilderBlock;
7
+ trackingContext?: TrackingContext;
6
8
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>;
7
9
  type EventHandler = (event: Event) => any;
8
10
  export const createEventHandler = (value: string, options: Options): EventHandler => event => evaluate({
@@ -12,5 +14,6 @@ export const createEventHandler = (value: string, options: Options): EventHandle
12
14
  rootState: options.rootState,
13
15
  rootSetState: options.rootSetState,
14
16
  event,
15
- isExpression: false
17
+ isExpression: false,
18
+ trackingContext: options.trackingContext
16
19
  })
@@ -1,6 +1,7 @@
1
1
  import { TARGET } from '../constants/target';
2
2
  import type { BuilderContextInterface } from '../context/types';
3
3
  import type { BuilderBlock } from '../types/builder-block';
4
+ import type { TrackingContext } from './evaluate/helpers';
4
5
  import { getEventHandlerName } from './event-handler-name';
5
6
  import { createEventHandler } from './get-block-actions-handler';
6
7
  type Actions = {
@@ -9,6 +10,7 @@ type Actions = {
9
10
  export function getBlockActions(options: {
10
11
  block: BuilderBlock;
11
12
  stripPrefix?: boolean;
13
+ trackingContext?: TrackingContext;
12
14
  } & Pick<BuilderContextInterface, 'localState' | 'context' | 'rootState' | 'rootSetState'>): Actions {
13
15
  const obj: Actions = {};
14
16
  const optionActions = options.block.actions ?? {};