@kameleoon/react-sdk 2.0.0 → 4.0.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 +322 -53
- package/package.json +3 -2
- package/CHANGELOG.md +0 -61
- package/dist/Feature.d.ts +0 -11
- package/dist/Feature.js +0 -26
- package/dist/Feature.js.map +0 -1
- package/dist/KameleoonContext.d.ts +0 -3
- package/dist/KameleoonContext.js +0 -6
- package/dist/KameleoonContext.js.map +0 -1
- package/dist/KameleoonProvider.d.ts +0 -14
- package/dist/KameleoonProvider.js +0 -25
- package/dist/KameleoonProvider.js.map +0 -1
- package/dist/ProviderError.d.ts +0 -3
- package/dist/ProviderError.js +0 -29
- package/dist/ProviderError.js.map +0 -1
- package/dist/compose.d.ts +0 -8
- package/dist/compose.js +0 -33
- package/dist/compose.js.map +0 -1
- package/dist/constants.d.ts +0 -14
- package/dist/constants.js +0 -20
- package/dist/constants.js.map +0 -1
- package/dist/index.d.ts +0 -37
- package/dist/index.js +0 -85
- package/dist/index.js.map +0 -1
- package/dist/kameleoonClient.d.ts +0 -16
- package/dist/kameleoonClient.js +0 -17
- package/dist/kameleoonClient.js.map +0 -1
- package/dist/stories/Form.d.ts +0 -22
- package/dist/stories/Form.js +0 -98
- package/dist/stories/Form.js.map +0 -1
- package/dist/stories/FormHOC.d.ts +0 -2
- package/dist/stories/FormHOC.js +0 -41
- package/dist/stories/FormHOC.js.map +0 -1
- package/dist/stories/FormHook.d.ts +0 -2
- package/dist/stories/FormHook.js +0 -66
- package/dist/stories/FormHook.js.map +0 -1
- package/dist/stories/FormRenderProps.d.ts +0 -3
- package/dist/stories/FormRenderProps.js +0 -24
- package/dist/stories/FormRenderProps.js.map +0 -1
- package/dist/stories/Radio.d.ts +0 -8
- package/dist/stories/Radio.js +0 -9
- package/dist/stories/Radio.js.map +0 -1
- package/dist/stories/constants.d.ts +0 -14
- package/dist/stories/constants.js +0 -19
- package/dist/stories/constants.js.map +0 -1
- package/dist/types.d.ts +0 -24
- package/dist/types.js +0 -4
- package/dist/types.js.map +0 -1
- package/dist/useActivateFeature.d.ts +0 -12
- package/dist/useActivateFeature.js +0 -23
- package/dist/useActivateFeature.js.map +0 -1
- package/dist/useAddData.d.ts +0 -14
- package/dist/useAddData.js +0 -33
- package/dist/useAddData.js.map +0 -1
- package/dist/useBrowser.d.ts +0 -13
- package/dist/useBrowser.js +0 -22
- package/dist/useBrowser.js.map +0 -1
- package/dist/useConversion.d.ts +0 -14
- package/dist/useConversion.js +0 -18
- package/dist/useConversion.js.map +0 -1
- package/dist/useCustomData.d.ts +0 -13
- package/dist/useCustomData.js +0 -18
- package/dist/useCustomData.js.map +0 -1
- package/dist/useFeature.d.ts +0 -5
- package/dist/useFeature.js +0 -83
- package/dist/useFeature.js.map +0 -1
- package/dist/useFeatureVariable.d.ts +0 -13
- package/dist/useFeatureVariable.js +0 -36
- package/dist/useFeatureVariable.js.map +0 -1
- package/dist/useFlush.d.ts +0 -11
- package/dist/useFlush.js +0 -19
- package/dist/useFlush.js.map +0 -1
- package/dist/useInterest.d.ts +0 -12
- package/dist/useInterest.js +0 -16
- package/dist/useInterest.js.map +0 -1
- package/dist/useKameleoon.d.ts +0 -10
- package/dist/useKameleoon.js +0 -20
- package/dist/useKameleoon.js.map +0 -1
- package/dist/usePageView.d.ts +0 -14
- package/dist/usePageView.js +0 -18
- package/dist/usePageView.js.map +0 -1
- package/dist/useTrackingConversion.d.ts +0 -13
- package/dist/useTrackingConversion.js +0 -19
- package/dist/useTrackingConversion.js.map +0 -1
- package/dist/useTriggerExperiment.d.ts +0 -15
- package/dist/useTriggerExperiment.js +0 -22
- package/dist/useTriggerExperiment.js.map +0 -1
- package/dist/useVariationAssociatedData.d.ts +0 -14
- package/dist/useVariationAssociatedData.js +0 -21
- package/dist/useVariationAssociatedData.js.map +0 -1
- package/dist/useVisitorCode.d.ts +0 -12
- package/dist/useVisitorCode.js +0 -19
- package/dist/useVisitorCode.js.map +0 -1
- package/dist/withActivateFeature.d.ts +0 -15
- package/dist/withActivateFeature.js +0 -36
- package/dist/withActivateFeature.js.map +0 -1
- package/dist/withAddData.d.ts +0 -16
- package/dist/withAddData.js +0 -49
- package/dist/withAddData.js.map +0 -1
- package/dist/withBrowser.d.ts +0 -16
- package/dist/withBrowser.js +0 -35
- package/dist/withBrowser.js.map +0 -1
- package/dist/withConversion.d.ts +0 -17
- package/dist/withConversion.js +0 -31
- package/dist/withConversion.js.map +0 -1
- package/dist/withCustomData.d.ts +0 -16
- package/dist/withCustomData.js +0 -31
- package/dist/withCustomData.js.map +0 -1
- package/dist/withFeature.d.ts +0 -6
- package/dist/withFeature.js +0 -31
- package/dist/withFeature.js.map +0 -1
- package/dist/withFeatureVariable.d.ts +0 -15
- package/dist/withFeatureVariable.js +0 -28
- package/dist/withFeatureVariable.js.map +0 -1
- package/dist/withFlush.d.ts +0 -14
- package/dist/withFlush.js +0 -36
- package/dist/withFlush.js.map +0 -1
- package/dist/withInterest.d.ts +0 -15
- package/dist/withInterest.js +0 -31
- package/dist/withInterest.js.map +0 -1
- package/dist/withKameleoon.d.ts +0 -13
- package/dist/withKameleoon.js +0 -33
- package/dist/withKameleoon.js.map +0 -1
- package/dist/withPageView.d.ts +0 -17
- package/dist/withPageView.js +0 -31
- package/dist/withPageView.js.map +0 -1
- package/dist/withTrackingConversion.d.ts +0 -16
- package/dist/withTrackingConversion.js +0 -36
- package/dist/withTrackingConversion.js.map +0 -1
- package/dist/withTriggerExperiment.d.ts +0 -17
- package/dist/withTriggerExperiment.js +0 -38
- package/dist/withTriggerExperiment.js.map +0 -1
- package/dist/withVariationAssociatedData.d.ts +0 -16
- package/dist/withVariationAssociatedData.js +0 -38
- package/dist/withVariationAssociatedData.js.map +0 -1
- package/dist/withVisitorCode.d.ts +0 -15
- package/dist/withVisitorCode.js +0 -36
- package/dist/withVisitorCode.js.map +0 -1
package/README.md
CHANGED
|
@@ -137,26 +137,43 @@ Returns the status of a feature flag and its variables.
|
|
|
137
137
|
- `visitorCode: string` (optional) - unique identifier of the user.
|
|
138
138
|
|
|
139
139
|
#### Returns
|
|
140
|
-
- `
|
|
140
|
+
- `errors: KameleoonException[]` - an array of errors thrown as a result of getting variables or feature flag status.
|
|
141
|
+
- `feature` - an object containing feature flag status and variables
|
|
141
142
|
- `isActive: boolean` - feature flag status.
|
|
142
143
|
- `variables: FeatureVariableType[]` - feature flag variables.
|
|
143
144
|
|
|
145
|
+
#### Exceptions
|
|
146
|
+
- `KameleoonException.NotTargeted` - Exception indicating that the current visitor / user did not trigger the required targeting conditions for this feature. The targeting conditions are defined via Kameleoon's segment builder.
|
|
147
|
+
- `KameleoonException.FeatureConfigurationNotFound` - Exception indicating that the requested feature ID has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side (but code implementing the feature is already deployed on the web-application's side).
|
|
148
|
+
|
|
144
149
|
|
|
145
150
|
In this examples, if the feature flag with the `red-button` key is enabled, theme of button will set to red.
|
|
146
151
|
### `useFeature`
|
|
147
152
|
#### Example
|
|
148
153
|
```jsx
|
|
149
154
|
import { Button } from '@kameleoon/ui';
|
|
150
|
-
import { useFeature, useVisitorCode } from '@kameleoon/react-sdk';
|
|
155
|
+
import { useFeature, useVisitorCode, KameleoonException } from '@kameleoon/react-sdk';
|
|
151
156
|
|
|
152
157
|
function MyComponent(): JSX.Element {
|
|
153
158
|
const { getVisitorCode } = useVisitorCode();
|
|
154
|
-
const {
|
|
159
|
+
const { feature, errors } = useFeature({
|
|
155
160
|
featureKey: 'red-button',
|
|
156
161
|
variableKeys: { production: 'red-button' },
|
|
157
162
|
visitorCode: getVisitorCode('example.com'),
|
|
158
163
|
});
|
|
159
164
|
|
|
165
|
+
for (const error of errors) {
|
|
166
|
+
if (error.type === KameleoonException.NotTargeted) {
|
|
167
|
+
// Handle error
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (error.type === KameleoonException.FeatureConfigurationNotFound) {
|
|
171
|
+
// Handle error
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const { isActive, variables } = feature;
|
|
176
|
+
|
|
160
177
|
return <Button theme={isActive ? 'red' : 'green'} />;
|
|
161
178
|
}
|
|
162
179
|
```
|
|
@@ -165,12 +182,34 @@ function MyComponent(): JSX.Element {
|
|
|
165
182
|
#### Example
|
|
166
183
|
```jsx
|
|
167
184
|
import { Button } from '@kameleoon/ui';
|
|
168
|
-
import { withFeature } from '@kameleoon/react-sdk';
|
|
185
|
+
import { withFeature, KameleoonException } from '@kameleoon/react-sdk';
|
|
169
186
|
|
|
170
187
|
class MyComponent extends React.Component {
|
|
188
|
+
state = {
|
|
189
|
+
isActive: false,
|
|
190
|
+
variables: [],
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
componentDidMount() {
|
|
194
|
+
const { feature, errors } = this.props;
|
|
195
|
+
|
|
196
|
+
for (const error of errors) {
|
|
197
|
+
if (error.type === KameleoonException.NotTargeted) {
|
|
198
|
+
// Handle error
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (error.type === KameleoonException.FeatureConfigurationNotFound) {
|
|
202
|
+
// Handle error
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const { isActive, variables } = feature;
|
|
207
|
+
|
|
208
|
+
this.setState({ isActive, variables });
|
|
209
|
+
}
|
|
210
|
+
|
|
171
211
|
render() {
|
|
172
|
-
|
|
173
|
-
return <Button theme={isActive ? 'red' : 'green'} />;
|
|
212
|
+
return <Button theme={this.isActive ? 'red' : 'green'} />;
|
|
174
213
|
}
|
|
175
214
|
}
|
|
176
215
|
|
|
@@ -189,7 +228,9 @@ export default withFeature({
|
|
|
189
228
|
- `visitorCode: string` - unique identifier of the user.
|
|
190
229
|
|
|
191
230
|
#### Returns
|
|
192
|
-
|
|
231
|
+
A wrapped component with the following props:
|
|
232
|
+
- `errors: KameleoonException[]` - an array of errors thrown as a result of getting variables or feature flag status.
|
|
233
|
+
- `feature` - an object containing feature flag status and variables
|
|
193
234
|
- `isActive: boolean` - feature flag status.
|
|
194
235
|
- `variables: FeatureVariableType[]` - feature flag variables.
|
|
195
236
|
|
|
@@ -199,18 +240,44 @@ import { Button } from '@kameleoon/ui';
|
|
|
199
240
|
import { Feature } from '@kameleoon/react-sdk';
|
|
200
241
|
|
|
201
242
|
class MyComponent extends React.Component {
|
|
243
|
+
state = {
|
|
244
|
+
isActive: false,
|
|
245
|
+
variables: [],
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
componentDidMount() {
|
|
249
|
+
const { feature, errors } = this.props;
|
|
250
|
+
|
|
251
|
+
for (const error of errors) {
|
|
252
|
+
if (error.type === KameleoonException.NotTargeted) {
|
|
253
|
+
// Handle error
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (error.type === KameleoonException.FeatureConfigurationNotFound) {
|
|
257
|
+
// Handle error
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const { isActive, variables } = feature;
|
|
262
|
+
|
|
263
|
+
this.setState({ isActive, variables });
|
|
264
|
+
}
|
|
265
|
+
|
|
202
266
|
render() {
|
|
203
|
-
|
|
204
|
-
return <Button theme={isActive ? 'red' : 'green'} />;
|
|
267
|
+
return <Button theme={this.isActive ? 'red' : 'green'} />;
|
|
205
268
|
}
|
|
206
269
|
}
|
|
207
270
|
|
|
208
|
-
class
|
|
271
|
+
class MyComponentWrapper extends React.Component {
|
|
209
272
|
render() {
|
|
210
273
|
return(
|
|
211
|
-
<Feature
|
|
212
|
-
|
|
213
|
-
|
|
274
|
+
<Feature
|
|
275
|
+
featureKey="red-button"
|
|
276
|
+
variableKeys={{production: "red-button"}}
|
|
277
|
+
visitorCode="280295"
|
|
278
|
+
>
|
|
279
|
+
(({ feature, errors }) => (
|
|
280
|
+
<MyComponent feature={feature} errors={errors} />
|
|
214
281
|
))
|
|
215
282
|
</Feature>;
|
|
216
283
|
)
|
|
@@ -304,7 +371,7 @@ You have to make sure that proper error handling is set up in your code as shown
|
|
|
304
371
|
##### Please Note:
|
|
305
372
|
_By convention, the reference (original variation) always has an ID equal to **0**._
|
|
306
373
|
|
|
307
|
-
#### Exceptions
|
|
374
|
+
#### Exceptions
|
|
308
375
|
- `KameleoonException.NotTargeted` - Exception indicating that the current visitor / user did not trigger the required targeting conditions for this experiment. The targeting conditions are defined via Kameleoon's segment builder.
|
|
309
376
|
- `KameleoonException.NotActivated` - Exception indicating that the current visitor / user triggered the experiment (met the targeting conditions), but did not activate it. The most common reason for that is that part of the traffic has been excluded from the experiment and should not be tracked.
|
|
310
377
|
- `KameleoonException.ExperimentConfigurationNotFound` - Exception indicating that the requested experiment ID has not been found in the internal configuration of the SDK. This is usually normal and means that the experiment has not yet been started on Kameleoon's side (but code triggering / implementing variations is already deployed on the web-application's side).
|
|
@@ -320,20 +387,33 @@ _By convention, the reference (original variation) always has an ID equal to **0
|
|
|
320
387
|
### `useTriggerExperiment`
|
|
321
388
|
#### Returns
|
|
322
389
|
- A callback function `getVariationId()`.
|
|
390
|
+
- An `error` object containing `name`, `type` and `message` strings, certain type of exception can be handled using `error.type` and `KameleoonException`. If there is no errors on the client `error` will be `null`.
|
|
323
391
|
|
|
324
392
|
#### Example
|
|
325
393
|
```jsx
|
|
326
394
|
import { useEffect } from 'react';
|
|
327
|
-
import { useTriggerExperiment, useVisitorCode } from '@kameleoon/react-sdk';
|
|
395
|
+
import { useTriggerExperiment, useVisitorCode, KameleoonException } from '@kameleoon/react-sdk';
|
|
328
396
|
|
|
329
397
|
function MyComponent(): JSX.Element {
|
|
330
|
-
const { getVariationId } = useTriggerExperiment();
|
|
398
|
+
const { getVariationId, error } = useTriggerExperiment();
|
|
331
399
|
const { getVisitorCode } = useVisitorCode();
|
|
332
400
|
|
|
333
401
|
useEffect(() => {
|
|
334
402
|
const visitorCode = getVisitorCode('example.com');
|
|
335
403
|
const experimentId = 12341;
|
|
336
404
|
|
|
405
|
+
if (error?.type === KameleoonException.ExperimentConfigurationNotFound) {
|
|
406
|
+
// Handle exception
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (error?.type === KameleoonException.NotTargeted) {
|
|
410
|
+
// Handle exception
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (error?.type === KameleoonException.NotActivated) {
|
|
414
|
+
// Handle exception
|
|
415
|
+
}
|
|
416
|
+
|
|
337
417
|
const variationId = getVariationId(visitorCode, experimentId);
|
|
338
418
|
}, []);
|
|
339
419
|
|
|
@@ -350,14 +430,26 @@ function MyComponent(): JSX.Element {
|
|
|
350
430
|
|
|
351
431
|
#### Example
|
|
352
432
|
```jsx
|
|
353
|
-
import { withVisitorCode, withTriggerExperiment, compose } from '@kameleoon/react-sdk';
|
|
433
|
+
import { withVisitorCode, withTriggerExperiment, compose, KameleoonException } from '@kameleoon/react-sdk';
|
|
354
434
|
|
|
355
435
|
class MyComponent extends React.Component {
|
|
356
436
|
componentDidMount() {
|
|
357
|
-
const { getVisitorCode, getVariationId } = this.props;
|
|
437
|
+
const { getVisitorCode, getVariationId, triggerExperimentError } = this.props;
|
|
358
438
|
const visitorCode = getVisitorCode('example.com');
|
|
359
439
|
const experimentId = 230243;
|
|
360
440
|
|
|
441
|
+
if (triggerExperimentError?.type === KameleoonException.ExperimentConfigurationNotFound) {
|
|
442
|
+
// Handle exception
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (triggerExperimentError?.type === KameleoonException.NotTargeted) {
|
|
446
|
+
// Handle exception
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (triggerExperimentError?.type === KameleoonException.NotActivated) {
|
|
450
|
+
// Handle exception
|
|
451
|
+
}
|
|
452
|
+
|
|
361
453
|
const variationId = getVariationId(visitorCode, experimentId);
|
|
362
454
|
}
|
|
363
455
|
|
|
@@ -370,16 +462,16 @@ export default compose(withVisitorCode, withTriggerExperiment)(MyComponent);
|
|
|
370
462
|
## Activate feature
|
|
371
463
|
A callback function `hasFeature()` which validates if user has been associated with this feature. If it is fails, callback returns value **false**, otherwise **true**.
|
|
372
464
|
|
|
373
|
-
If feature flag is not activated, `KameleoonException.FeatureConfigurationNotFound` exception will be thrown. Please, make sure to handle
|
|
465
|
+
If feature flag is not activated, `KameleoonException.FeatureConfigurationNotFound` exception will be thrown. Please, make sure to handle the error properly.
|
|
374
466
|
|
|
375
|
-
#### Exceptions
|
|
467
|
+
#### Exceptions
|
|
376
468
|
- `KameleoonException.NotTargeted` - Exception indicating that the current visitor / user did not trigger the required targeting conditions for this feature. The targeting conditions are defined via Kameleoon's segment builder.
|
|
377
469
|
- `KameleoonException.FeatureConfigurationNotFound` - Exception indicating that the requested feature ID has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side (but code implementing the feature is already deployed on the web-application's side).
|
|
378
470
|
|
|
379
471
|
#### `hasFeature()`
|
|
380
472
|
##### Arguments
|
|
381
|
-
- `visitorCode: string` - unique identifier of the user.
|
|
382
473
|
- `featureKey: string | number` - unique identifier or key of the feature you want to expose to a user.
|
|
474
|
+
- `visitorCode: string` (optional) - unique identifier of the user.
|
|
383
475
|
|
|
384
476
|
##### Returns
|
|
385
477
|
- A `value: boolean` of the feature that is registered for a given `visitorCode`.
|
|
@@ -387,21 +479,30 @@ If feature flag is not activated, `KameleoonException.FeatureConfigurationNotFou
|
|
|
387
479
|
### `useActivateFeature`
|
|
388
480
|
#### Returns
|
|
389
481
|
- A callback function `hasFeature()`.
|
|
482
|
+
- An `error` object containing `name`, `type` and `message` strings, certain type of exception can be handled using `error.type` and `KameleoonException`. If there is no errors on the client `error` will be `null`.
|
|
390
483
|
|
|
391
484
|
#### Example
|
|
392
485
|
```jsx
|
|
393
486
|
import { useEffect } from 'react';
|
|
394
|
-
import { useActivateFeature, useVisitorCode } from '@kameleoon/react-sdk';
|
|
487
|
+
import { useActivateFeature, useVisitorCode, KameleoonException } from '@kameleoon/react-sdk';
|
|
395
488
|
|
|
396
489
|
function MyComponent(): JSX.Element {
|
|
397
|
-
const { hasFeature } = useActivateFeature();
|
|
490
|
+
const { hasFeature, error } = useActivateFeature();
|
|
398
491
|
const { getVisitorCode } = useVisitorCode();
|
|
399
492
|
|
|
400
493
|
useEffect(() => {
|
|
401
494
|
const visitorCode = getVisitorCode('example.com');
|
|
402
495
|
const featureKey = 'example-feature-key';
|
|
403
496
|
|
|
404
|
-
|
|
497
|
+
if (error?.type === KameleoonException.FeatureConfigurationNotFound) {
|
|
498
|
+
// Handle exception
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (error?.type === KameleoonException.NotTargeted) {
|
|
502
|
+
// Handle exception
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const feature = hasFeature(featureKey, visitorCode);
|
|
405
506
|
}, []);
|
|
406
507
|
|
|
407
508
|
...
|
|
@@ -410,22 +511,30 @@ function MyComponent(): JSX.Element {
|
|
|
410
511
|
|
|
411
512
|
### `withActivateFeature`
|
|
412
513
|
#### Arguments
|
|
413
|
-
- `Component: React.Component` - component which will be enhanced with the prop `hasFeature()`.
|
|
514
|
+
- `Component: React.Component` - component which will be enhanced with the prop `hasFeature()` and `activateFeatureError`.
|
|
414
515
|
|
|
415
516
|
#### Returns
|
|
416
517
|
- A wrapped component with additional props as described above.
|
|
417
518
|
|
|
418
519
|
#### Example
|
|
419
520
|
```jsx
|
|
420
|
-
import { withVisitorCode, withActivateFeature, compose } from '@kameleoon/react-sdk';
|
|
521
|
+
import { withVisitorCode, withActivateFeature, KameleoonException, compose } from '@kameleoon/react-sdk';
|
|
421
522
|
|
|
422
523
|
class MyComponent extends React.Component {
|
|
423
524
|
componentDidMount() {
|
|
424
|
-
const { getVisitorCode, hasFeature } = this.props;
|
|
525
|
+
const { getVisitorCode, hasFeature, activateFeatureError } = this.props;
|
|
425
526
|
const visitorCode = getVisitorCode('example.com');
|
|
426
527
|
const featureKey = 'example-feature-key';
|
|
427
528
|
|
|
428
|
-
|
|
529
|
+
if (activateFeatureError?.type === KameleoonException.FeatureConfigurationNotFound) {
|
|
530
|
+
// Handle exception
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (activateFeatureError?.type === KameleoonException.NotTargeted) {
|
|
534
|
+
// Handle exception
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const feature = hasFeature(featureKey, visitorCode);
|
|
429
538
|
}
|
|
430
539
|
|
|
431
540
|
...
|
|
@@ -435,11 +544,11 @@ export default compose(withVisitorCode, withActivateFeature)(MyComponent);
|
|
|
435
544
|
```
|
|
436
545
|
|
|
437
546
|
## Obtain variation associated data
|
|
438
|
-
A callback function `getVariationAssociatedData()` which retrieves JSON data
|
|
547
|
+
A callback function `getVariationAssociatedData()` which retrieves JSON data associated with a variation. The JSON data usually represents some metadata of the variation, and can be configured on our web application interface or via our Automation API.
|
|
439
548
|
|
|
440
|
-
This
|
|
549
|
+
This callback function takes the `variationId` as a parameter and will return the data as a JavaScript object. It will throw an exception `(KameleoonException.VariationConfigurationNotFound)` if the `variationId` is wrong or corresponds to an experiment that is not yet online.
|
|
441
550
|
|
|
442
|
-
#### Exceptions
|
|
551
|
+
#### Exceptions
|
|
443
552
|
- `KameleoonException.VariationConfigurationNotFound` - Exception indicating that the requested variation ID has not been found in the internal configuration of the SDK. This is usually normal and means that the variation's corresponding experiment has not yet been activated on Kameleoon's side.
|
|
444
553
|
|
|
445
554
|
#### `getVariationAssociatedData()`
|
|
@@ -453,17 +562,23 @@ This calllback function takes the `variationId` as a parameter and will return t
|
|
|
453
562
|
### `useVariationAssociatedData`
|
|
454
563
|
#### Returns
|
|
455
564
|
- A callback function `getVariationAssociatedData()`.
|
|
565
|
+
- An `error` object containing `name`, `type` and `message` strings, certain type of exception can be handled using `error.type` and `KameleoonException`. If there is no errors on the client `error` will be `null`.
|
|
456
566
|
|
|
457
567
|
#### Example
|
|
458
568
|
```jsx
|
|
459
569
|
import { useEffect } from 'react';
|
|
460
|
-
import { useVariationAssociatedData } from '@kameleoon/react-sdk';
|
|
570
|
+
import { useVariationAssociatedData, KameleoonException } from '@kameleoon/react-sdk';
|
|
461
571
|
|
|
462
572
|
function MyComponent(): JSX.Element {
|
|
463
|
-
const { getVariationAssociatedData } = useVariationAssociatedData();
|
|
573
|
+
const { getVariationAssociatedData, error } = useVariationAssociatedData();
|
|
464
574
|
|
|
465
575
|
useEffect(() => {
|
|
466
576
|
const variationId = 280295;
|
|
577
|
+
|
|
578
|
+
if (error?.type === KameleoonException.VariationConfigurationNotFound) {
|
|
579
|
+
// Handle error
|
|
580
|
+
}
|
|
581
|
+
|
|
467
582
|
const data = getVariationAssociatedData(variationId);
|
|
468
583
|
}, []);
|
|
469
584
|
|
|
@@ -479,12 +594,17 @@ function MyComponent(): JSX.Element {
|
|
|
479
594
|
|
|
480
595
|
#### Example
|
|
481
596
|
```jsx
|
|
482
|
-
import { withVariationAssociatedData } from '@kameleoon/react-sdk';
|
|
597
|
+
import { withVariationAssociatedData, KameleoonException } from '@kameleoon/react-sdk';
|
|
483
598
|
|
|
484
599
|
class MyComponent extends React.Component {
|
|
485
600
|
componentDidMount() {
|
|
486
|
-
const { getVariationAssociatedData } = this.props;
|
|
601
|
+
const { getVariationAssociatedData, variationAssociatedDataError } = this.props;
|
|
487
602
|
const variationId = 280295;
|
|
603
|
+
|
|
604
|
+
if (variationAssociatedDataError?.type === KameleoonException.VariationConfigurationNotFound) {
|
|
605
|
+
// Handle error
|
|
606
|
+
}
|
|
607
|
+
|
|
488
608
|
const data = getVariationAssociatedData(variationId);
|
|
489
609
|
}
|
|
490
610
|
|
|
@@ -494,6 +614,74 @@ class MyComponent extends React.Component {
|
|
|
494
614
|
export default withVariationAssociatedData(MyComponent);
|
|
495
615
|
```
|
|
496
616
|
|
|
617
|
+
## Obtain data from remote source
|
|
618
|
+
An callback function `retrieveDataFromRemoteSource()` can be used to retrieve data using specific key and siteCode from Kameleoon provider. The Data is stored on a remote Kameleoon server. Usually data will be stored on our remote servers via the use of our Data API. This method, along with the availability of our highly scalable servers for this purpose, provides a convenient way to quickly store massive amounts of data that can be later retrieved for each of your visitors / users.
|
|
619
|
+
|
|
620
|
+
Note that since a server call is required, this mechanism is asynchronous, make sure to properly handle promise which will be returned from a function call.
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
#### Exceptions Thrown
|
|
624
|
+
- Local Error will be thrown if remote source data couldn't be accessed or retrieved data contains an empty JSON.
|
|
625
|
+
|
|
626
|
+
#### `retrieveDataFromRemoteSource()`
|
|
627
|
+
|
|
628
|
+
##### Arguments
|
|
629
|
+
- `key: string` - unique key for the current siteCode used to store data on the remote source (can be created via `POST` request to Kameleoon Data API). This field is mandatory.
|
|
630
|
+
|
|
631
|
+
##### Returns
|
|
632
|
+
- JSON object with the data posted to Kameleoon Data API.
|
|
633
|
+
|
|
634
|
+
#### Types
|
|
635
|
+
- `RemoteSourceDataType` can be used for handling data type.
|
|
636
|
+
|
|
637
|
+
### `useRetrieveDataFromRemoteSource`
|
|
638
|
+
#### Returns
|
|
639
|
+
- A callback function `retrieveDataFromRemoteSource()`.
|
|
640
|
+
|
|
641
|
+
#### Example
|
|
642
|
+
```jsx
|
|
643
|
+
import { useEffect, useCallback } from 'react';
|
|
644
|
+
import { useRetrieveDataFromRemoteSource, RemoteSourceDataType } from '@kameleoon/react-sdk';
|
|
645
|
+
|
|
646
|
+
function MyComponent(): JSX.Element {
|
|
647
|
+
const { retrieveDataFromRemoteSource } = useRetrieveDataFromRemoteSource();
|
|
648
|
+
|
|
649
|
+
const processRetrievedData = useCallback(async () => {
|
|
650
|
+
const data: RemoteSourceDataType = await retrieveDataFromRemoteSource('example-key');
|
|
651
|
+
// Your code
|
|
652
|
+
}, [retrieveDataFromRemoteSource]);
|
|
653
|
+
|
|
654
|
+
useEffect(() => {
|
|
655
|
+
processRetrievedData();
|
|
656
|
+
}, [processRetrievedData]);
|
|
657
|
+
|
|
658
|
+
...
|
|
659
|
+
}
|
|
660
|
+
```
|
|
661
|
+
### `withRetrieveDataFromRemoteSource`
|
|
662
|
+
#### Arguments
|
|
663
|
+
- `Component: React.Component` - component which will be enhanced with the prop `retrieveDataFromRemoteSource()`.
|
|
664
|
+
|
|
665
|
+
#### Returns
|
|
666
|
+
- A wrapped component with additional props as described above.
|
|
667
|
+
|
|
668
|
+
#### Example
|
|
669
|
+
```jsx
|
|
670
|
+
import { withRetrieveDataFromRemoteSource, RemoteSourceDataType } from '@kameleoon/react-sdk';
|
|
671
|
+
|
|
672
|
+
class MyComponent extends React.Component {
|
|
673
|
+
async componentDidMount() {
|
|
674
|
+
const { retrieveDataFromRemoteSource } = this.props;
|
|
675
|
+
const data: RemoteSourceDataType = await getVariationAssociatedData('example-key');
|
|
676
|
+
// Your code
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
...
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
export default withRetrieveDataFromRemoteSource(MyComponent);
|
|
683
|
+
```
|
|
684
|
+
|
|
497
685
|
## Obtain feature variable
|
|
498
686
|
A callback function `getFeatureVariable()` which retrieves a feature variable.
|
|
499
687
|
|
|
@@ -730,7 +918,6 @@ The `flush()` callback function is non-blocking as the server call is made async
|
|
|
730
918
|
#### Example
|
|
731
919
|
```jsx
|
|
732
920
|
import { useEffect } from 'react';
|
|
733
|
-
import { Button } from '@kameleoon/ui';
|
|
734
921
|
import {
|
|
735
922
|
useAddData,
|
|
736
923
|
useBrowser,
|
|
@@ -794,6 +981,86 @@ export default compose(
|
|
|
794
981
|
)(MyComponent);
|
|
795
982
|
```
|
|
796
983
|
|
|
984
|
+
## Run when ready
|
|
985
|
+
In certain scenarios when working with Kameleoon API it's important to make sure that the client was initialized properly within the certain timeout. It's especially important while using `triggerExperiment()` or `trackConversion()`. For these cases it's possible to use `runWhenReady()` function, retrieved by `useRunWhenReady` hook or `withRunWhenReady` high-order component.
|
|
986
|
+
|
|
987
|
+
The `runWhenReady()` function makes sure that Kameleoon Client will be initialized properly using HTTP call withing the specified timeout.
|
|
988
|
+
|
|
989
|
+
#### `runWhenReady()`
|
|
990
|
+
##### Arguments
|
|
991
|
+
- `successCallback: () => void` - callback which will be executed on successful client initialization.
|
|
992
|
+
- `errorCallback: () => void` - callback which will be executed if client wasn't able to initialize during the timeout.
|
|
993
|
+
- `timeout?: number` - timeout which is given to perform HTTP call for initialization in ms (by default: 2000 ms).
|
|
994
|
+
|
|
995
|
+
#### Example
|
|
996
|
+
```jsx
|
|
997
|
+
import { useEffect, useCallback, useState } from 'react';
|
|
998
|
+
import { useRunWhenReady, useTriggerExperiment } from '@kameleoon/react-sdk';
|
|
999
|
+
|
|
1000
|
+
function MyComponent(): JSX.Element {
|
|
1001
|
+
const { runWhenReady } = useRunWhenReady();
|
|
1002
|
+
const { getVariationId } = useTriggerExperiment();
|
|
1003
|
+
|
|
1004
|
+
const [variationId, setVariationId] = useState<number>(0);
|
|
1005
|
+
|
|
1006
|
+
const getVariationSuccessCallback = useCallback(() => {
|
|
1007
|
+
const id = getVariationId('user_id', 12345);
|
|
1008
|
+
setVariationId(id);
|
|
1009
|
+
}, [getVariationId, isRenderProps]);
|
|
1010
|
+
|
|
1011
|
+
const getVariationErrorCallback = useCallback(() => {
|
|
1012
|
+
throw new Error(
|
|
1013
|
+
"Couldn't get server configuration from HTTP request in a specified time",
|
|
1014
|
+
);
|
|
1015
|
+
}, []);
|
|
1016
|
+
|
|
1017
|
+
useEffect(() => {
|
|
1018
|
+
runWhenReady(
|
|
1019
|
+
getVariationSuccessCallback,
|
|
1020
|
+
getVariationErrorCallback,
|
|
1021
|
+
1000,
|
|
1022
|
+
);
|
|
1023
|
+
}, [runWhenReady, getVariationSuccessCallback, getVariationErrorCallback]);
|
|
1024
|
+
|
|
1025
|
+
...
|
|
1026
|
+
}
|
|
1027
|
+
```
|
|
1028
|
+
|
|
1029
|
+
### `withRunWhenReady`
|
|
1030
|
+
#### Arguments
|
|
1031
|
+
- `Component: React.Component` - component which will be enhanced with the prop `runWhenReady()`.
|
|
1032
|
+
|
|
1033
|
+
#### Example
|
|
1034
|
+
```jsx
|
|
1035
|
+
import { useEffect, useCallback, useState } from 'react';
|
|
1036
|
+
import { withRunWhenReady, withTriggerExperiment } from '@kameleoon/react-sdk';
|
|
1037
|
+
|
|
1038
|
+
class MyComponent extends React.Component {
|
|
1039
|
+
variationSuccessCallback(): void {
|
|
1040
|
+
const id = this.props.getVariationId('user_id', 12345);
|
|
1041
|
+
setVariationId(id);
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
variationErrorCallback(): void {
|
|
1045
|
+
const id = this.props.getVariationId('user_id', 12345);
|
|
1046
|
+
setVariationId(id);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
componentDidMount() {
|
|
1050
|
+
this.props.runWhenReady(this.variationSuccessCallback, this.variationErrorCallback, 1000);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
...
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
export default compose(
|
|
1057
|
+
withRunWhenReady,
|
|
1058
|
+
withTriggerExperiment,
|
|
1059
|
+
)(MyComponent);
|
|
1060
|
+
```
|
|
1061
|
+
|
|
1062
|
+
#### Returns
|
|
1063
|
+
- A wrapped component with additional props as described above.
|
|
797
1064
|
|
|
798
1065
|
## Browser
|
|
799
1066
|
A callback function `addBrowser()` adds browser type.
|
|
@@ -1079,43 +1346,44 @@ export default compose(
|
|
|
1079
1346
|
)(MyComponent);
|
|
1080
1347
|
```
|
|
1081
1348
|
|
|
1082
|
-
##
|
|
1083
|
-
A callback function `
|
|
1349
|
+
## Device
|
|
1350
|
+
A callback function `addDevice()` adds device.
|
|
1084
1351
|
|
|
1085
|
-
#### `
|
|
1352
|
+
#### `addDevice`
|
|
1086
1353
|
##### Arguments
|
|
1087
|
-
- `
|
|
1354
|
+
- `device: DeviceType` - device type: Phone, Tablet, Desktop.
|
|
1355
|
+
|
|
1088
1356
|
|
|
1089
1357
|
##### Returns
|
|
1090
|
-
- An instance of `
|
|
1358
|
+
- An instance of `Device: IDevice`.
|
|
1091
1359
|
|
|
1092
|
-
### `
|
|
1360
|
+
### `useDevice`
|
|
1093
1361
|
#### Returns
|
|
1094
|
-
- A callback function `
|
|
1362
|
+
- A callback function `addDevice()`.
|
|
1095
1363
|
|
|
1096
1364
|
#### Example
|
|
1097
1365
|
```jsx
|
|
1098
1366
|
import { useEffect } from 'react';
|
|
1099
|
-
import { useAddData,
|
|
1367
|
+
import { useAddData, useDevice, DeviceType, useVisitorCode } from '@kameleoon/react-sdk';
|
|
1100
1368
|
|
|
1101
1369
|
function MyComponent(): JSX.Element {
|
|
1102
1370
|
const { addData } = useAddData();
|
|
1103
1371
|
const { getVisitorCode } = useVisitorCode();
|
|
1104
|
-
const {
|
|
1372
|
+
const { addDevice } = useDevice();
|
|
1105
1373
|
|
|
1106
1374
|
useEffect(() => {
|
|
1107
1375
|
const visitorCode = getVisitorCode('example.com');
|
|
1108
1376
|
|
|
1109
|
-
addData(visitorCode, [
|
|
1377
|
+
addData(visitorCode, [addDevice(DeviceType.Desktop)]);
|
|
1110
1378
|
}, []);
|
|
1111
1379
|
|
|
1112
1380
|
...
|
|
1113
1381
|
}
|
|
1114
1382
|
```
|
|
1115
1383
|
|
|
1116
|
-
### `
|
|
1384
|
+
### `withDevice`
|
|
1117
1385
|
#### Arguments
|
|
1118
|
-
- `Component: React.Component` - component which will be enhanced with the prop `
|
|
1386
|
+
- `Component: React.Component` - component which will be enhanced with the prop `addDevice()`.
|
|
1119
1387
|
|
|
1120
1388
|
#### Returns
|
|
1121
1389
|
- A wrapped component with additional props as described above.
|
|
@@ -1125,16 +1393,17 @@ function MyComponent(): JSX.Element {
|
|
|
1125
1393
|
import {
|
|
1126
1394
|
withVisitorCode,
|
|
1127
1395
|
withAddData,
|
|
1128
|
-
|
|
1396
|
+
withDevice,
|
|
1397
|
+
DeviceType,
|
|
1129
1398
|
compose,
|
|
1130
1399
|
} from '@kameleoon/react-sdk';
|
|
1131
1400
|
|
|
1132
1401
|
class MyComponent extends React.Component {
|
|
1133
1402
|
componentDidMount() {
|
|
1134
|
-
const { addData,
|
|
1403
|
+
const { addData, addDevice, getVisitorCode } = this.props;
|
|
1135
1404
|
const visitorCode = getVisitorCode('example.com');
|
|
1136
1405
|
|
|
1137
|
-
addData(visitorCode, [
|
|
1406
|
+
addData(visitorCode, [addDevice(DeviceType.Desktop)]);
|
|
1138
1407
|
}
|
|
1139
1408
|
|
|
1140
1409
|
...
|
|
@@ -1142,7 +1411,7 @@ class MyComponent extends React.Component {
|
|
|
1142
1411
|
|
|
1143
1412
|
export default compose(
|
|
1144
1413
|
withAddData,
|
|
1145
|
-
|
|
1414
|
+
withDevice,
|
|
1146
1415
|
withVisitorCode,
|
|
1147
1416
|
)(MyComponent);
|
|
1148
1417
|
```
|
|
@@ -1174,4 +1443,4 @@ class MyComponent extends React.Component {
|
|
|
1174
1443
|
}
|
|
1175
1444
|
|
|
1176
1445
|
export default compose(withVisitorCode, withTriggerExperiment)(MyComponent);
|
|
1177
|
-
```
|
|
1446
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kameleoon/react-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Kameleoon React SDK",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"build": "npm run clear && tsc",
|
|
13
13
|
"deploy": "npm publish --tag latest --access public",
|
|
14
14
|
"release": "../../scripts/release.sh react-sdk",
|
|
15
|
+
"update-documentation": "../../scripts/update_documentation.sh react",
|
|
15
16
|
"test": "jest",
|
|
16
17
|
"test:watch": "jest --watch",
|
|
17
18
|
"test:coverage": "jest --coverage",
|
|
@@ -34,7 +35,7 @@
|
|
|
34
35
|
},
|
|
35
36
|
"dependencies": {
|
|
36
37
|
"@types/validator": "^13.6.3",
|
|
37
|
-
"kameleoon-client-javascript": "^1.0.
|
|
38
|
+
"kameleoon-client-javascript": "^1.0.9",
|
|
38
39
|
"validator": "^13.6.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|