@sanity/personalization-plugin 2.1.0-field-names.1 → 2.1.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/README.md +73 -19
- package/dist/index.js +10 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/ExperimentField.tsx +0 -1
- package/src/components/VariantInput.tsx +15 -68
- package/src/fieldExperiments.tsx +2 -0
package/README.md
CHANGED
|
@@ -83,8 +83,8 @@ export default defineConfig({
|
|
|
83
83
|
|
|
84
84
|
This will register two new fields to the schema., based on the setting passed intto `fields:`
|
|
85
85
|
|
|
86
|
-
- `experimentString` an Object field with `string` field called `default`, a `string` field called `experimentId` and an array field of type:
|
|
87
|
-
- `
|
|
86
|
+
- `experimentString` an Object field with `string` field called `default`, a `string` field called `experimentId` and an array field called `variants` of type:
|
|
87
|
+
- `variantString` an object field with a `string` field called `value`, a string field called `variantId`, a `string` field called `experimentId`.
|
|
88
88
|
|
|
89
89
|
Use the experiment field in your schema like this:
|
|
90
90
|
|
|
@@ -143,23 +143,21 @@ experiments: async () => {
|
|
|
143
143
|
const response = await fetch('https://example.com/experiments')
|
|
144
144
|
const {externalExperiments} = await response.json()
|
|
145
145
|
|
|
146
|
-
const experiments: ExperimentType[] = externalExperiments?.map(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const variants = experiment.variations?.map((variant) => {
|
|
151
|
-
return {
|
|
152
|
-
id: variant.variationId,
|
|
153
|
-
label: variant.name,
|
|
154
|
-
}
|
|
155
|
-
})
|
|
146
|
+
const experiments: ExperimentType[] = externalExperiments?.map((experiment) => {
|
|
147
|
+
const experimentId = experiment.id
|
|
148
|
+
const experimentLabel = experiment.name
|
|
149
|
+
const variants = experiment.variations?.map((variant) => {
|
|
156
150
|
return {
|
|
157
|
-
id:
|
|
158
|
-
label:
|
|
159
|
-
variants,
|
|
151
|
+
id: variant.variationId,
|
|
152
|
+
label: variant.name,
|
|
160
153
|
}
|
|
161
|
-
}
|
|
162
|
-
|
|
154
|
+
})
|
|
155
|
+
return {
|
|
156
|
+
id: experimentId,
|
|
157
|
+
label: experimentLabel,
|
|
158
|
+
variants,
|
|
159
|
+
}
|
|
160
|
+
})
|
|
163
161
|
return experiments
|
|
164
162
|
}
|
|
165
163
|
```
|
|
@@ -202,7 +200,7 @@ export default defineConfig({
|
|
|
202
200
|
|
|
203
201
|
This would also create two new fields in your schema.
|
|
204
202
|
|
|
205
|
-
- `experimentFeaturedProduct` an Object field with `reference` field called `default`, a `string` field called `experimentId` and an array field of type:
|
|
203
|
+
- `experimentFeaturedProduct` an Object field with `reference` field called `default`, a `string` field called `experimentId` and an array field called `variants` of type:
|
|
206
204
|
- `variantFeaturedProduct` an object field with a `reference` field called `value`, a string field called `variandId`, a `string` field called `experimentId`.
|
|
207
205
|
|
|
208
206
|
Note that the name key in the field gets rewritten to value and is instead used to name the object field.
|
|
@@ -242,7 +240,7 @@ The custom input contains buttons which will add new array items with the experi
|
|
|
242
240
|
```json
|
|
243
241
|
"title": {
|
|
244
242
|
"default": "asdf",
|
|
245
|
-
"
|
|
243
|
+
"experimentId": "test-1",
|
|
246
244
|
"variants": [
|
|
247
245
|
{
|
|
248
246
|
"experimentId": "test-1",
|
|
@@ -267,6 +265,62 @@ Using GROQ filters you can query for a specific experitment, with a fallback to
|
|
|
267
265
|
}
|
|
268
266
|
```
|
|
269
267
|
|
|
268
|
+
## Overwriting the experiment and variant field names
|
|
269
|
+
|
|
270
|
+
If your use case does not match exactly with experiments you can overwrite the name field names for experiment and variant in the config.
|
|
271
|
+
|
|
272
|
+
```ts
|
|
273
|
+
import {defineConfig} from 'sanity'
|
|
274
|
+
import {fieldLevelExperiments} from '@sanity/personalization-plugin'
|
|
275
|
+
|
|
276
|
+
export default defineConfig({
|
|
277
|
+
//...
|
|
278
|
+
plugins: [
|
|
279
|
+
//...
|
|
280
|
+
fieldLevelExperiments({
|
|
281
|
+
fields: ['string'],
|
|
282
|
+
experiments: [experiment1, experiment2],
|
|
283
|
+
experimentNameOverride: 'audience',
|
|
284
|
+
variantNameOverride: 'segment',
|
|
285
|
+
}),
|
|
286
|
+
],
|
|
287
|
+
})
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
This would also create two new fields in your schema.
|
|
291
|
+
|
|
292
|
+
- `audienceString` an Object field with `string` field called `default`, a `string` field called `audienceId` and an array field called `segments` of type:
|
|
293
|
+
- `segmentString` an object field with a `string` field called `value`, a string field called `segmentId`, a `string` field called `audienceId`.
|
|
294
|
+
|
|
295
|
+
the data will be stored as
|
|
296
|
+
|
|
297
|
+
```json
|
|
298
|
+
"title": {
|
|
299
|
+
"default": "asdf",
|
|
300
|
+
"audienceId": "test-1",
|
|
301
|
+
"segments": [
|
|
302
|
+
{
|
|
303
|
+
"audienceId": "test-1",
|
|
304
|
+
"value": "asdf",
|
|
305
|
+
"segmentId": "test-1-a"
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
"audienceId": "test-1",
|
|
309
|
+
"segmentId": "test-1-b",
|
|
310
|
+
"value": "qwer"
|
|
311
|
+
}
|
|
312
|
+
]
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
This will also affect the query you write to fetch data to be:
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
*[_type == "post"] {
|
|
320
|
+
"title":coalesce(title.segments[audienceId == $audience && segmentId == $segment][0].value, title.default),
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
270
324
|
## License
|
|
271
325
|
|
|
272
326
|
[MIT](LICENSE) © Jon Burbridge
|
package/dist/index.js
CHANGED
|
@@ -6506,6 +6506,14 @@ const useAddExperimentAction = (props) => {
|
|
|
6506
6506
|
[patch, subValues, aditionalChangePath]
|
|
6507
6507
|
);
|
|
6508
6508
|
return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
|
|
6509
|
+
}, VariantInput = (props) => {
|
|
6510
|
+
const defaultValue = sanity.useFormValue([props.path[0], "default"]), handleClick = () => {
|
|
6511
|
+
props.onChange(sanity.set(defaultValue, ["value"]));
|
|
6512
|
+
};
|
|
6513
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
6514
|
+
props.renderDefault(props),
|
|
6515
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) })
|
|
6516
|
+
] });
|
|
6509
6517
|
}, VariantPreview = (props) => {
|
|
6510
6518
|
const [subtitle, setSubtitle] = react.useState(void 0), [title, setTitle] = react.useState(void 0), [media, setMedia] = react.useState(void 0), client = sanity.useClient({ apiVersion: "2025-01-01" }), { experiments } = useExperimentContext(), { experiment, variant, value } = props, selectedExperiment = experiments.find((experimentItem) => experimentItem.id === experiment), selectedVariant = selectedExperiment?.variants.find((variantItem) => variantItem.id === variant);
|
|
6511
6519
|
react.useEffect(() => {
|
|
@@ -6638,7 +6646,8 @@ const createExperimentType = ({
|
|
|
6638
6646
|
title: `${variantNameOverride} array ${usedName}`,
|
|
6639
6647
|
type: "object",
|
|
6640
6648
|
components: {
|
|
6641
|
-
preview: VariantPreview
|
|
6649
|
+
preview: VariantPreview,
|
|
6650
|
+
input: VariantInput
|
|
6642
6651
|
},
|
|
6643
6652
|
fields: [
|
|
6644
6653
|
{
|