@sanity/personalization-plugin 2.1.0-growthbook.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 CHANGED
@@ -25,8 +25,6 @@ Once configured you can query the values using the ids of the experiment and var
25
25
  - [Release new version](#release-new-version)
26
26
  - [License](#license-1)
27
27
 
28
- For Specific information about the growthbookFieldLevel export see its [readme](/growthbook.md)
29
-
30
28
  ## Installation
31
29
 
32
30
  ```sh
@@ -85,8 +83,8 @@ export default defineConfig({
85
83
 
86
84
  This will register two new fields to the schema., based on the setting passed intto `fields:`
87
85
 
88
- - `experimentString` an Object field with `string` field called `default`, a `string` field called `experimentId` and an array field of type:
89
- - `varirantsString` an object field with a `string` field called `value`, a string field called `variantId`, a `string` field called `experimentId`.
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`.
90
88
 
91
89
  Use the experiment field in your schema like this:
92
90
 
@@ -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
- "experimentValue": "test-1",
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.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  import {ArrayOfObjectsInputProps} from 'sanity'
2
- import {Dispatch} from 'react'
3
2
  import {FieldDefinition} from 'sanity'
4
3
  import {ObjectField} from 'sanity'
5
4
  import {Path} from 'sanity'
@@ -7,27 +6,30 @@ import {Plugin as Plugin_2} from 'sanity'
7
6
  import {PreviewProps} from 'sanity'
8
7
  import {SanityClient} from 'sanity'
9
8
  import {SchemaType} from 'sanity'
10
- import {SetStateAction} from 'react'
11
9
 
12
10
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
13
- objectName: string
11
+ variantName: string
12
+ variantId: string
13
+ experimentId: string
14
14
  }
15
15
 
16
16
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
17
17
  experiments: ExperimentType[]
18
- setSecret: Dispatch<SetStateAction<string | undefined>>
19
- secret: string | undefined
20
18
  }
21
19
 
22
20
  export declare type ExperimentGeneric<T> = {
23
21
  _type: string
24
22
  default?: T
25
23
  experimentValue?: string
26
- variants?: Array<
27
- {
28
- _key: string
29
- } & VariantGeneric<T>
30
- >
24
+ [key: string]:
25
+ | Array<
26
+ {
27
+ _key: string
28
+ } & VariantGeneric<T>
29
+ >
30
+ | string
31
+ | T
32
+ | undefined
31
33
  }
32
34
 
33
35
  export declare type ExperimentType = {
@@ -40,10 +42,13 @@ export declare const fieldLevelExperiments: Plugin_2<FieldPluginConfig>
40
42
 
41
43
  export declare type FieldPluginConfig = {
42
44
  fields: (string | FieldDefinition)[]
43
- experiments:
44
- | ExperimentType[]
45
- | ((client: SanityClient, secret?: string) => Promise<ExperimentType[]>)
45
+ experiments: ExperimentType[] | ((client: SanityClient) => Promise<ExperimentType[]>)
46
46
  apiVersion?: string
47
+ experimentNameOverride?: string
48
+ variantNameOverride?: string
49
+ variantId?: string
50
+ variantArrayName?: string
51
+ experimentId?: string
47
52
  }
48
53
 
49
54
  /**
@@ -51,218 +56,18 @@ export declare type FieldPluginConfig = {
51
56
  */
52
57
  export declare function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
53
58
 
54
- export declare type GrowthbookABConfig = {
55
- fields: (string | FieldDefinition)[]
56
- environment: string
57
- baseUrl?: string
58
- project?: string
59
- convertBooleans?: boolean
60
- }
61
-
62
- export declare type GrowthbookExperiment = {
63
- id: string
64
- dateCreated: string
65
- dateUpdated: string
66
- name: string
67
- project: string
68
- hypothesis: string
69
- description: string
70
- tags: [string]
71
- owner: string
72
- archived: boolean
73
- status: string
74
- autoRefresh: boolean
75
- hashAttribute: string
76
- fallbackAttribute: string
77
- hashVersion: number
78
- disableStickyBucketing: boolean
79
- bucketVersion: number
80
- minBucketVersion: number
81
- variations: [
82
- {
83
- variationId: string
84
- key: string
85
- name: string
86
- description: string
87
- screenshots: [string]
88
- },
89
- ]
90
- phases: [
91
- {
92
- name: string
93
- dateStarted: string
94
- dateEnded: string
95
- reasonForStopping: string
96
- seed: string
97
- coverage: 0
98
- trafficSplit: [
99
- {
100
- variationId: string
101
- weight: 0
102
- },
103
- ]
104
- namespace: {
105
- namespaceId: string
106
- range: []
107
- }
108
- targetingCondition: string
109
- savedGroupTargeting: [
110
- {
111
- matchType: string
112
- savedGroups: [string]
113
- },
114
- ]
115
- },
116
- ]
117
- settings: {
118
- datasourceId: string
119
- assignmentQueryId: string
120
- experimentId: string
121
- segmentId: string
122
- queryFilter: string
123
- inProgressConversions: string
124
- attributionModel: string
125
- statsEngine: string
126
- regressionAdjustmentEnabled: boolean
127
- goals: [
128
- {
129
- metricId: string
130
- overrides: {
131
- delayHours: 0
132
- windowHours: 0
133
- window: string
134
- winRiskThreshold: 0
135
- loseRiskThreshold: 0
136
- }
137
- },
138
- ]
139
- secondaryMetrics: [
140
- {
141
- metricId: string
142
- overrides: {
143
- delayHours: 0
144
- windowHours: 0
145
- window: string
146
- winRiskThreshold: 0
147
- loseRiskThreshold: 0
148
- }
149
- },
150
- ]
151
- guardrails: [
152
- {
153
- metricId: string
154
- overrides: {
155
- delayHours: 0
156
- windowHours: 0
157
- window: string
158
- winRiskThreshold: 0
159
- loseRiskThreshold: 0
160
- }
161
- },
162
- ]
163
- activationMetric: {
164
- metricId: string
165
- overrides: {
166
- delayHours: 0
167
- windowHours: 0
168
- window: string
169
- winRiskThreshold: 0
170
- loseRiskThreshold: 0
171
- }
172
- }
173
- }
174
- resultSummary: {
175
- status: string
176
- winner: string
177
- conclusions: string
178
- releasedVariationId: string
179
- excludeFromPayload: boolean
180
- }
181
- }
182
-
183
- export declare type GrowthbookFeature = {
184
- id: string
185
- dateCreated: string
186
- dateUpdated: string
187
- archived: boolean
188
- description: string
189
- owner: string
190
- project: string
191
- valueType: string
192
- defaultValue: string
193
- tags: string[]
194
- environments: {
195
- [key: string]: {
196
- enabled: boolean
197
- defaultValue: string
198
- rules: {
199
- description: string
200
- condition: string
201
- savedGroupTargeting: {
202
- matchType: string
203
- savedGroups: string[]
204
- }[]
205
- id: string
206
- enabled: boolean
207
- type: string
208
- value: string
209
- variations: {
210
- value: string
211
- variationId: string
212
- }[]
213
- }[]
214
- definition: string
215
- draft: {
216
- enabled: boolean
217
- defaultValue: string
218
- rules: {
219
- description: string
220
- condition: string
221
- savedGroupTargeting: {
222
- matchType: string
223
- savedGroups: string[]
224
- }[]
225
- id: string
226
- enabled: boolean
227
- type: string
228
- value: string
229
- variations: {
230
- value: string
231
- variationId: string
232
- }[]
233
- }[]
234
- definition: string
235
- }
236
- }
237
- }
238
- prerequisites: {
239
- parentId: string
240
- parentCondition: string
241
- }[]
242
- revision: {
243
- version: number
244
- comment: string
245
- date: string
246
- publishedBy: string
247
- }
248
- }
249
-
250
- export declare const growthbookFieldLevel: Plugin_2<GrowthbookABConfig>
251
-
252
59
  export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
253
60
  path: Path
254
61
  }
255
62
 
256
63
  export declare type VariantGeneric<T> = {
64
+ [key: string]: string | T | undefined
257
65
  _type: string
258
- variantId?: string
259
- experimentId?: string
260
66
  value?: T
261
67
  }
262
68
 
263
69
  export declare type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
264
- experiment: string
265
- variant: string
70
+ [key: string]: string
266
71
  value: any
267
72
  }
268
73
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import {ArrayOfObjectsInputProps} from 'sanity'
2
- import {Dispatch} from 'react'
3
2
  import {FieldDefinition} from 'sanity'
4
3
  import {ObjectField} from 'sanity'
5
4
  import {Path} from 'sanity'
@@ -7,27 +6,30 @@ import {Plugin as Plugin_2} from 'sanity'
7
6
  import {PreviewProps} from 'sanity'
8
7
  import {SanityClient} from 'sanity'
9
8
  import {SchemaType} from 'sanity'
10
- import {SetStateAction} from 'react'
11
9
 
12
10
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
13
- objectName: string
11
+ variantName: string
12
+ variantId: string
13
+ experimentId: string
14
14
  }
15
15
 
16
16
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
17
17
  experiments: ExperimentType[]
18
- setSecret: Dispatch<SetStateAction<string | undefined>>
19
- secret: string | undefined
20
18
  }
21
19
 
22
20
  export declare type ExperimentGeneric<T> = {
23
21
  _type: string
24
22
  default?: T
25
23
  experimentValue?: string
26
- variants?: Array<
27
- {
28
- _key: string
29
- } & VariantGeneric<T>
30
- >
24
+ [key: string]:
25
+ | Array<
26
+ {
27
+ _key: string
28
+ } & VariantGeneric<T>
29
+ >
30
+ | string
31
+ | T
32
+ | undefined
31
33
  }
32
34
 
33
35
  export declare type ExperimentType = {
@@ -40,10 +42,13 @@ export declare const fieldLevelExperiments: Plugin_2<FieldPluginConfig>
40
42
 
41
43
  export declare type FieldPluginConfig = {
42
44
  fields: (string | FieldDefinition)[]
43
- experiments:
44
- | ExperimentType[]
45
- | ((client: SanityClient, secret?: string) => Promise<ExperimentType[]>)
45
+ experiments: ExperimentType[] | ((client: SanityClient) => Promise<ExperimentType[]>)
46
46
  apiVersion?: string
47
+ experimentNameOverride?: string
48
+ variantNameOverride?: string
49
+ variantId?: string
50
+ variantArrayName?: string
51
+ experimentId?: string
47
52
  }
48
53
 
49
54
  /**
@@ -51,218 +56,18 @@ export declare type FieldPluginConfig = {
51
56
  */
52
57
  export declare function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
53
58
 
54
- export declare type GrowthbookABConfig = {
55
- fields: (string | FieldDefinition)[]
56
- environment: string
57
- baseUrl?: string
58
- project?: string
59
- convertBooleans?: boolean
60
- }
61
-
62
- export declare type GrowthbookExperiment = {
63
- id: string
64
- dateCreated: string
65
- dateUpdated: string
66
- name: string
67
- project: string
68
- hypothesis: string
69
- description: string
70
- tags: [string]
71
- owner: string
72
- archived: boolean
73
- status: string
74
- autoRefresh: boolean
75
- hashAttribute: string
76
- fallbackAttribute: string
77
- hashVersion: number
78
- disableStickyBucketing: boolean
79
- bucketVersion: number
80
- minBucketVersion: number
81
- variations: [
82
- {
83
- variationId: string
84
- key: string
85
- name: string
86
- description: string
87
- screenshots: [string]
88
- },
89
- ]
90
- phases: [
91
- {
92
- name: string
93
- dateStarted: string
94
- dateEnded: string
95
- reasonForStopping: string
96
- seed: string
97
- coverage: 0
98
- trafficSplit: [
99
- {
100
- variationId: string
101
- weight: 0
102
- },
103
- ]
104
- namespace: {
105
- namespaceId: string
106
- range: []
107
- }
108
- targetingCondition: string
109
- savedGroupTargeting: [
110
- {
111
- matchType: string
112
- savedGroups: [string]
113
- },
114
- ]
115
- },
116
- ]
117
- settings: {
118
- datasourceId: string
119
- assignmentQueryId: string
120
- experimentId: string
121
- segmentId: string
122
- queryFilter: string
123
- inProgressConversions: string
124
- attributionModel: string
125
- statsEngine: string
126
- regressionAdjustmentEnabled: boolean
127
- goals: [
128
- {
129
- metricId: string
130
- overrides: {
131
- delayHours: 0
132
- windowHours: 0
133
- window: string
134
- winRiskThreshold: 0
135
- loseRiskThreshold: 0
136
- }
137
- },
138
- ]
139
- secondaryMetrics: [
140
- {
141
- metricId: string
142
- overrides: {
143
- delayHours: 0
144
- windowHours: 0
145
- window: string
146
- winRiskThreshold: 0
147
- loseRiskThreshold: 0
148
- }
149
- },
150
- ]
151
- guardrails: [
152
- {
153
- metricId: string
154
- overrides: {
155
- delayHours: 0
156
- windowHours: 0
157
- window: string
158
- winRiskThreshold: 0
159
- loseRiskThreshold: 0
160
- }
161
- },
162
- ]
163
- activationMetric: {
164
- metricId: string
165
- overrides: {
166
- delayHours: 0
167
- windowHours: 0
168
- window: string
169
- winRiskThreshold: 0
170
- loseRiskThreshold: 0
171
- }
172
- }
173
- }
174
- resultSummary: {
175
- status: string
176
- winner: string
177
- conclusions: string
178
- releasedVariationId: string
179
- excludeFromPayload: boolean
180
- }
181
- }
182
-
183
- export declare type GrowthbookFeature = {
184
- id: string
185
- dateCreated: string
186
- dateUpdated: string
187
- archived: boolean
188
- description: string
189
- owner: string
190
- project: string
191
- valueType: string
192
- defaultValue: string
193
- tags: string[]
194
- environments: {
195
- [key: string]: {
196
- enabled: boolean
197
- defaultValue: string
198
- rules: {
199
- description: string
200
- condition: string
201
- savedGroupTargeting: {
202
- matchType: string
203
- savedGroups: string[]
204
- }[]
205
- id: string
206
- enabled: boolean
207
- type: string
208
- value: string
209
- variations: {
210
- value: string
211
- variationId: string
212
- }[]
213
- }[]
214
- definition: string
215
- draft: {
216
- enabled: boolean
217
- defaultValue: string
218
- rules: {
219
- description: string
220
- condition: string
221
- savedGroupTargeting: {
222
- matchType: string
223
- savedGroups: string[]
224
- }[]
225
- id: string
226
- enabled: boolean
227
- type: string
228
- value: string
229
- variations: {
230
- value: string
231
- variationId: string
232
- }[]
233
- }[]
234
- definition: string
235
- }
236
- }
237
- }
238
- prerequisites: {
239
- parentId: string
240
- parentCondition: string
241
- }[]
242
- revision: {
243
- version: number
244
- comment: string
245
- date: string
246
- publishedBy: string
247
- }
248
- }
249
-
250
- export declare const growthbookFieldLevel: Plugin_2<GrowthbookABConfig>
251
-
252
59
  export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
253
60
  path: Path
254
61
  }
255
62
 
256
63
  export declare type VariantGeneric<T> = {
64
+ [key: string]: string | T | undefined
257
65
  _type: string
258
- variantId?: string
259
- experimentId?: string
260
66
  value?: T
261
67
  }
262
68
 
263
69
  export declare type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
264
- experiment: string
265
- variant: string
70
+ [key: string]: string
266
71
  value: any
267
72
  }
268
73