@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.
Files changed (138) hide show
  1. package/README.md +322 -53
  2. package/package.json +3 -2
  3. package/CHANGELOG.md +0 -61
  4. package/dist/Feature.d.ts +0 -11
  5. package/dist/Feature.js +0 -26
  6. package/dist/Feature.js.map +0 -1
  7. package/dist/KameleoonContext.d.ts +0 -3
  8. package/dist/KameleoonContext.js +0 -6
  9. package/dist/KameleoonContext.js.map +0 -1
  10. package/dist/KameleoonProvider.d.ts +0 -14
  11. package/dist/KameleoonProvider.js +0 -25
  12. package/dist/KameleoonProvider.js.map +0 -1
  13. package/dist/ProviderError.d.ts +0 -3
  14. package/dist/ProviderError.js +0 -29
  15. package/dist/ProviderError.js.map +0 -1
  16. package/dist/compose.d.ts +0 -8
  17. package/dist/compose.js +0 -33
  18. package/dist/compose.js.map +0 -1
  19. package/dist/constants.d.ts +0 -14
  20. package/dist/constants.js +0 -20
  21. package/dist/constants.js.map +0 -1
  22. package/dist/index.d.ts +0 -37
  23. package/dist/index.js +0 -85
  24. package/dist/index.js.map +0 -1
  25. package/dist/kameleoonClient.d.ts +0 -16
  26. package/dist/kameleoonClient.js +0 -17
  27. package/dist/kameleoonClient.js.map +0 -1
  28. package/dist/stories/Form.d.ts +0 -22
  29. package/dist/stories/Form.js +0 -98
  30. package/dist/stories/Form.js.map +0 -1
  31. package/dist/stories/FormHOC.d.ts +0 -2
  32. package/dist/stories/FormHOC.js +0 -41
  33. package/dist/stories/FormHOC.js.map +0 -1
  34. package/dist/stories/FormHook.d.ts +0 -2
  35. package/dist/stories/FormHook.js +0 -66
  36. package/dist/stories/FormHook.js.map +0 -1
  37. package/dist/stories/FormRenderProps.d.ts +0 -3
  38. package/dist/stories/FormRenderProps.js +0 -24
  39. package/dist/stories/FormRenderProps.js.map +0 -1
  40. package/dist/stories/Radio.d.ts +0 -8
  41. package/dist/stories/Radio.js +0 -9
  42. package/dist/stories/Radio.js.map +0 -1
  43. package/dist/stories/constants.d.ts +0 -14
  44. package/dist/stories/constants.js +0 -19
  45. package/dist/stories/constants.js.map +0 -1
  46. package/dist/types.d.ts +0 -24
  47. package/dist/types.js +0 -4
  48. package/dist/types.js.map +0 -1
  49. package/dist/useActivateFeature.d.ts +0 -12
  50. package/dist/useActivateFeature.js +0 -23
  51. package/dist/useActivateFeature.js.map +0 -1
  52. package/dist/useAddData.d.ts +0 -14
  53. package/dist/useAddData.js +0 -33
  54. package/dist/useAddData.js.map +0 -1
  55. package/dist/useBrowser.d.ts +0 -13
  56. package/dist/useBrowser.js +0 -22
  57. package/dist/useBrowser.js.map +0 -1
  58. package/dist/useConversion.d.ts +0 -14
  59. package/dist/useConversion.js +0 -18
  60. package/dist/useConversion.js.map +0 -1
  61. package/dist/useCustomData.d.ts +0 -13
  62. package/dist/useCustomData.js +0 -18
  63. package/dist/useCustomData.js.map +0 -1
  64. package/dist/useFeature.d.ts +0 -5
  65. package/dist/useFeature.js +0 -83
  66. package/dist/useFeature.js.map +0 -1
  67. package/dist/useFeatureVariable.d.ts +0 -13
  68. package/dist/useFeatureVariable.js +0 -36
  69. package/dist/useFeatureVariable.js.map +0 -1
  70. package/dist/useFlush.d.ts +0 -11
  71. package/dist/useFlush.js +0 -19
  72. package/dist/useFlush.js.map +0 -1
  73. package/dist/useInterest.d.ts +0 -12
  74. package/dist/useInterest.js +0 -16
  75. package/dist/useInterest.js.map +0 -1
  76. package/dist/useKameleoon.d.ts +0 -10
  77. package/dist/useKameleoon.js +0 -20
  78. package/dist/useKameleoon.js.map +0 -1
  79. package/dist/usePageView.d.ts +0 -14
  80. package/dist/usePageView.js +0 -18
  81. package/dist/usePageView.js.map +0 -1
  82. package/dist/useTrackingConversion.d.ts +0 -13
  83. package/dist/useTrackingConversion.js +0 -19
  84. package/dist/useTrackingConversion.js.map +0 -1
  85. package/dist/useTriggerExperiment.d.ts +0 -15
  86. package/dist/useTriggerExperiment.js +0 -22
  87. package/dist/useTriggerExperiment.js.map +0 -1
  88. package/dist/useVariationAssociatedData.d.ts +0 -14
  89. package/dist/useVariationAssociatedData.js +0 -21
  90. package/dist/useVariationAssociatedData.js.map +0 -1
  91. package/dist/useVisitorCode.d.ts +0 -12
  92. package/dist/useVisitorCode.js +0 -19
  93. package/dist/useVisitorCode.js.map +0 -1
  94. package/dist/withActivateFeature.d.ts +0 -15
  95. package/dist/withActivateFeature.js +0 -36
  96. package/dist/withActivateFeature.js.map +0 -1
  97. package/dist/withAddData.d.ts +0 -16
  98. package/dist/withAddData.js +0 -49
  99. package/dist/withAddData.js.map +0 -1
  100. package/dist/withBrowser.d.ts +0 -16
  101. package/dist/withBrowser.js +0 -35
  102. package/dist/withBrowser.js.map +0 -1
  103. package/dist/withConversion.d.ts +0 -17
  104. package/dist/withConversion.js +0 -31
  105. package/dist/withConversion.js.map +0 -1
  106. package/dist/withCustomData.d.ts +0 -16
  107. package/dist/withCustomData.js +0 -31
  108. package/dist/withCustomData.js.map +0 -1
  109. package/dist/withFeature.d.ts +0 -6
  110. package/dist/withFeature.js +0 -31
  111. package/dist/withFeature.js.map +0 -1
  112. package/dist/withFeatureVariable.d.ts +0 -15
  113. package/dist/withFeatureVariable.js +0 -28
  114. package/dist/withFeatureVariable.js.map +0 -1
  115. package/dist/withFlush.d.ts +0 -14
  116. package/dist/withFlush.js +0 -36
  117. package/dist/withFlush.js.map +0 -1
  118. package/dist/withInterest.d.ts +0 -15
  119. package/dist/withInterest.js +0 -31
  120. package/dist/withInterest.js.map +0 -1
  121. package/dist/withKameleoon.d.ts +0 -13
  122. package/dist/withKameleoon.js +0 -33
  123. package/dist/withKameleoon.js.map +0 -1
  124. package/dist/withPageView.d.ts +0 -17
  125. package/dist/withPageView.js +0 -31
  126. package/dist/withPageView.js.map +0 -1
  127. package/dist/withTrackingConversion.d.ts +0 -16
  128. package/dist/withTrackingConversion.js +0 -36
  129. package/dist/withTrackingConversion.js.map +0 -1
  130. package/dist/withTriggerExperiment.d.ts +0 -17
  131. package/dist/withTriggerExperiment.js +0 -38
  132. package/dist/withTriggerExperiment.js.map +0 -1
  133. package/dist/withVariationAssociatedData.d.ts +0 -16
  134. package/dist/withVariationAssociatedData.js +0 -38
  135. package/dist/withVariationAssociatedData.js.map +0 -1
  136. package/dist/withVisitorCode.d.ts +0 -15
  137. package/dist/withVisitorCode.js +0 -36
  138. 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
- - `featureResult: IFeature`
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 { isActive } = useFeature({
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
- const { isActive, variables } = this.props;
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
- - A wrapped component with the following props:
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
- const { isActive, variables } = this.props;
204
- return <Button theme={isActive ? 'red' : 'green'} />;
267
+ return <Button theme={this.isActive ? 'red' : 'green'} />;
205
268
  }
206
269
  }
207
270
 
208
- class MyCompWrapper extends React.Component {
271
+ class MyComponentWrapper extends React.Component {
209
272
  render() {
210
273
  return(
211
- <Feature featureKey="red-button" variableKeys={{production: "red-button"}} visitorCode="280295">
212
- (({isActive, variables}) => (
213
- <MyComponent isActive={isActive} variables={variables} />
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 Thrown
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 properly the error.
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 Thrown
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
- const feature = hasFeature(visitorCode, featureKey);
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
- const feature = hasFeature(visitorCode, featureKey);
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 assiciated 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.
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 calllback 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.
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 Thrown
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
- ## Interest
1083
- A callback function `addInterest()` adds interest.
1349
+ ## Device
1350
+ A callback function `addDevice()` adds device.
1084
1351
 
1085
- #### `addInterest`
1352
+ #### `addDevice`
1086
1353
  ##### Arguments
1087
- - `index: number` - index of interest.
1354
+ - `device: DeviceType` - device type: Phone, Tablet, Desktop.
1355
+
1088
1356
 
1089
1357
  ##### Returns
1090
- - An instance of `Interest: IInterest`.
1358
+ - An instance of `Device: IDevice`.
1091
1359
 
1092
- ### `useInterest`
1360
+ ### `useDevice`
1093
1361
  #### Returns
1094
- - A callback function `addInterest()`.
1362
+ - A callback function `addDevice()`.
1095
1363
 
1096
1364
  #### Example
1097
1365
  ```jsx
1098
1366
  import { useEffect } from 'react';
1099
- import { useAddData, useInterest, useVisitorCode } from '@kameleoon/react-sdk';
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 { addInterest } = useInterest();
1372
+ const { addDevice } = useDevice();
1105
1373
 
1106
1374
  useEffect(() => {
1107
1375
  const visitorCode = getVisitorCode('example.com');
1108
1376
 
1109
- addData(visitorCode, [addInterest(0)]);
1377
+ addData(visitorCode, [addDevice(DeviceType.Desktop)]);
1110
1378
  }, []);
1111
1379
 
1112
1380
  ...
1113
1381
  }
1114
1382
  ```
1115
1383
 
1116
- ### `withInterest`
1384
+ ### `withDevice`
1117
1385
  #### Arguments
1118
- - `Component: React.Component` - component which will be enhanced with the prop `addInterest()`.
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
- withInterest,
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, addInterest, getVisitorCode } = this.props;
1403
+ const { addData, addDevice, getVisitorCode } = this.props;
1135
1404
  const visitorCode = getVisitorCode('example.com');
1136
1405
 
1137
- addData(visitorCode, [addInterest(0)]);
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
- withInterest,
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": "2.0.0",
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.6",
38
+ "kameleoon-client-javascript": "^1.0.9",
38
39
  "validator": "^13.6.0"
39
40
  },
40
41
  "devDependencies": {