@kameleoon/react-sdk 1.2.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/LICENSE.md +165 -0
  2. package/README.md +1172 -0
  3. package/dist/Feature.d.ts +11 -0
  4. package/dist/Feature.js +26 -0
  5. package/dist/Feature.js.map +1 -0
  6. package/dist/KameleoonContext.d.ts +3 -0
  7. package/dist/KameleoonContext.js +6 -0
  8. package/dist/KameleoonContext.js.map +1 -0
  9. package/dist/KameleoonProvider.d.ts +14 -0
  10. package/dist/KameleoonProvider.js +25 -0
  11. package/dist/KameleoonProvider.js.map +1 -0
  12. package/dist/ProviderError.d.ts +3 -0
  13. package/dist/ProviderError.js +29 -0
  14. package/dist/ProviderError.js.map +1 -0
  15. package/dist/compose.d.ts +8 -0
  16. package/dist/compose.js +33 -0
  17. package/dist/compose.js.map +1 -0
  18. package/dist/constants.d.ts +13 -0
  19. package/dist/constants.js +19 -0
  20. package/dist/constants.js.map +1 -0
  21. package/dist/index.d.ts +37 -0
  22. package/dist/index.js +85 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/kameleoonClient.d.ts +16 -0
  25. package/dist/kameleoonClient.js +17 -0
  26. package/dist/kameleoonClient.js.map +1 -0
  27. package/dist/stories/Form.d.ts +22 -0
  28. package/dist/stories/Form.js +97 -0
  29. package/dist/stories/Form.js.map +1 -0
  30. package/dist/stories/FormHOC.d.ts +2 -0
  31. package/dist/stories/FormHOC.js +36 -0
  32. package/dist/stories/FormHOC.js.map +1 -0
  33. package/dist/stories/FormHook.d.ts +2 -0
  34. package/dist/stories/FormHook.js +61 -0
  35. package/dist/stories/FormHook.js.map +1 -0
  36. package/dist/stories/FormRenderProps.d.ts +3 -0
  37. package/dist/stories/FormRenderProps.js +24 -0
  38. package/dist/stories/FormRenderProps.js.map +1 -0
  39. package/dist/stories/Radio.d.ts +8 -0
  40. package/dist/stories/Radio.js +9 -0
  41. package/dist/stories/Radio.js.map +1 -0
  42. package/dist/stories/constants.d.ts +13 -0
  43. package/dist/stories/constants.js +18 -0
  44. package/dist/stories/constants.js.map +1 -0
  45. package/dist/types.d.ts +20 -0
  46. package/dist/types.js +4 -0
  47. package/dist/types.js.map +1 -0
  48. package/dist/useActivateFeature.d.ts +12 -0
  49. package/dist/useActivateFeature.js +23 -0
  50. package/dist/useActivateFeature.js.map +1 -0
  51. package/dist/useAddData.d.ts +14 -0
  52. package/dist/useAddData.js +33 -0
  53. package/dist/useAddData.js.map +1 -0
  54. package/dist/useBrowser.d.ts +13 -0
  55. package/dist/useBrowser.js +22 -0
  56. package/dist/useBrowser.js.map +1 -0
  57. package/dist/useConversion.d.ts +14 -0
  58. package/dist/useConversion.js +18 -0
  59. package/dist/useConversion.js.map +1 -0
  60. package/dist/useCustomData.d.ts +13 -0
  61. package/dist/useCustomData.js +18 -0
  62. package/dist/useCustomData.js.map +1 -0
  63. package/dist/useFeature.d.ts +5 -0
  64. package/dist/useFeature.js +75 -0
  65. package/dist/useFeature.js.map +1 -0
  66. package/dist/useFeatureVariable.d.ts +13 -0
  67. package/dist/useFeatureVariable.js +36 -0
  68. package/dist/useFeatureVariable.js.map +1 -0
  69. package/dist/useFlush.d.ts +11 -0
  70. package/dist/useFlush.js +19 -0
  71. package/dist/useFlush.js.map +1 -0
  72. package/dist/useInterest.d.ts +12 -0
  73. package/dist/useInterest.js +16 -0
  74. package/dist/useInterest.js.map +1 -0
  75. package/dist/useKameleoon.d.ts +10 -0
  76. package/dist/useKameleoon.js +20 -0
  77. package/dist/useKameleoon.js.map +1 -0
  78. package/dist/usePageView.d.ts +14 -0
  79. package/dist/usePageView.js +18 -0
  80. package/dist/usePageView.js.map +1 -0
  81. package/dist/useTrackingConversion.d.ts +13 -0
  82. package/dist/useTrackingConversion.js +19 -0
  83. package/dist/useTrackingConversion.js.map +1 -0
  84. package/dist/useTriggerExperiment.d.ts +15 -0
  85. package/dist/useTriggerExperiment.js +22 -0
  86. package/dist/useTriggerExperiment.js.map +1 -0
  87. package/dist/useVariationAssociatedData.d.ts +14 -0
  88. package/dist/useVariationAssociatedData.js +21 -0
  89. package/dist/useVariationAssociatedData.js.map +1 -0
  90. package/dist/useVisitorCode.d.ts +12 -0
  91. package/dist/useVisitorCode.js +19 -0
  92. package/dist/useVisitorCode.js.map +1 -0
  93. package/dist/withActivateFeature.d.ts +15 -0
  94. package/dist/withActivateFeature.js +37 -0
  95. package/dist/withActivateFeature.js.map +1 -0
  96. package/dist/withAddData.d.ts +16 -0
  97. package/dist/withAddData.js +49 -0
  98. package/dist/withAddData.js.map +1 -0
  99. package/dist/withBrowser.d.ts +16 -0
  100. package/dist/withBrowser.js +36 -0
  101. package/dist/withBrowser.js.map +1 -0
  102. package/dist/withConversion.d.ts +17 -0
  103. package/dist/withConversion.js +32 -0
  104. package/dist/withConversion.js.map +1 -0
  105. package/dist/withCustomData.d.ts +16 -0
  106. package/dist/withCustomData.js +32 -0
  107. package/dist/withCustomData.js.map +1 -0
  108. package/dist/withFeature.d.ts +6 -0
  109. package/dist/withFeature.js +31 -0
  110. package/dist/withFeature.js.map +1 -0
  111. package/dist/withFeatureVariable.d.ts +15 -0
  112. package/dist/withFeatureVariable.js +28 -0
  113. package/dist/withFeatureVariable.js.map +1 -0
  114. package/dist/withFlush.d.ts +14 -0
  115. package/dist/withFlush.js +36 -0
  116. package/dist/withFlush.js.map +1 -0
  117. package/dist/withInterest.d.ts +15 -0
  118. package/dist/withInterest.js +31 -0
  119. package/dist/withInterest.js.map +1 -0
  120. package/dist/withKameleoon.d.ts +13 -0
  121. package/dist/withKameleoon.js +34 -0
  122. package/dist/withKameleoon.js.map +1 -0
  123. package/dist/withPageView.d.ts +17 -0
  124. package/dist/withPageView.js +31 -0
  125. package/dist/withPageView.js.map +1 -0
  126. package/dist/withTrackingConversion.d.ts +16 -0
  127. package/dist/withTrackingConversion.js +36 -0
  128. package/dist/withTrackingConversion.js.map +1 -0
  129. package/dist/withTriggerExperiment.d.ts +17 -0
  130. package/dist/withTriggerExperiment.js +38 -0
  131. package/dist/withTriggerExperiment.js.map +1 -0
  132. package/dist/withVariationAssociatedData.d.ts +17 -0
  133. package/dist/withVariationAssociatedData.js +39 -0
  134. package/dist/withVariationAssociatedData.js.map +1 -0
  135. package/dist/withVisitorCode.d.ts +15 -0
  136. package/dist/withVisitorCode.js +36 -0
  137. package/dist/withVisitorCode.js.map +1 -0
  138. package/package.json +47 -0
package/README.md ADDED
@@ -0,0 +1,1172 @@
1
+ # Kameleoon React SDK
2
+
3
+ ### Introduction
4
+ This repository is a Kameleoon React SDK which is used to run experiments and get feature flag status and its variables. Integration of this SDK into web application is easy, and its footprint is low.
5
+
6
+ ### Requirements
7
+ This React SDK requires `React 16.8.0+`
8
+
9
+ ### Installation
10
+ Installing the React SDK can be directly achieved through an `npm` or `yarn` module:
11
+
12
+ **npm**
13
+ ```
14
+ npm install @kameleoon/react-sdk
15
+ ```
16
+
17
+ **yarn**
18
+ ```
19
+ yarn add @kameleoon/react-sdk
20
+ ```
21
+
22
+ ### Usage
23
+
24
+ ## `createClient`
25
+ A `KameleoonClient` created using `createClient()` to run experiments and retrive the status of feature flag and its variables.
26
+
27
+ #### Arguments
28
+ - `clientParams: ICreateClientParams`
29
+ - `siteCode: string` - code of the website on which experiments run. This unique code id can be found in our platform.
30
+ - `visitorCode: string` (optional) - visitor code is an optional parameter, if it is used, it automatically create custom data to check if visitor is targeted.
31
+ - `option: SDKConfiguration` (optional) - JSON object filled with configuration parameters.
32
+ - `actions_configuration_refresh_interval: number` (optional) - this specifies the refresh interval, in minutes, of the configuration for experiments and feature flags (the active experiments and feature flags are fetched from the Kameleoon servers). It means that once you launch an experiment, pause it, or stop it the changes can take (at most) the duration of this interval to be propagated in production to your servers. If not specified, the **default interval is 60 minutes**.
33
+ - `visitor_data_maximum_size: number` (optional) - this specifies the maximum amount of memory that [the map holding all the visitor data](https://developers.kameleoon.com/javascript-sdk.html#technical-considerations) (in particular custom data) can take (in MB on the browser LocalStorage). If specified, **max size is 5MB**. If not specified, the **default size is 1MB**.
34
+
35
+ #### Returns
36
+ - A `KameleoonClient` instance.
37
+
38
+ #### Example
39
+ ```jsx
40
+ import { createClient } from '@kameleoon/react-sdk';
41
+
42
+ const client = createClient({
43
+ siteCode: '0fpmcg34lg',
44
+ visitorCode: '280295',
45
+ options: {
46
+ visitor_data_maximum_size: 1,
47
+ actions_configuration_refresh_interval: 60
48
+ }
49
+ });
50
+ ```
51
+
52
+ ## `<KameleoonProvider>`
53
+ Use this provider in root level by wrapping your app to gain an access to `KameleoonClient`. This ensures your app does not flicker due to flag changes at startup time.
54
+
55
+ #### Props
56
+ - `children: ReactNode` - child elements of the provider.
57
+ - `client: KameleoonClient` - `KameleoonClient` instance created by `createClient()`.
58
+
59
+ #### Example
60
+ ```jsx
61
+ import { KameleoonProvider, createClient } from '@kameleoon/react-sdk';
62
+
63
+ const client = createClient({
64
+ siteCode: '0fpmcg34lg'
65
+ visitorCode: '280295',
66
+ options: {
67
+ visitor_data_maximum_size: 1,
68
+ actions_configuration_refresh_interval: 60
69
+ }
70
+ });
71
+
72
+ function AppWrapper(): JSX.Element {
73
+ return (
74
+ <KameleoonProvider client={client}>
75
+ <App />
76
+ </KameleoonProvider>
77
+ )
78
+ }
79
+ ```
80
+
81
+ ## Kameleoon client
82
+ These Hook and HOC validate whether they are used within `KameleoonProvider` and provide an access to `KameleoonClient`.
83
+
84
+ ### `useKameleoon`
85
+ #### Returns
86
+ - Returns `object: KameleoonHookResult`:
87
+ - `client: KameleoonClient` - an instance of `KameleoonClient` taken from context.
88
+
89
+ #### Example
90
+ ```jsx
91
+ import { useKameleoon } from '@kameleoon/react-sdk';
92
+
93
+ function MyComponent(): JSX.Element {
94
+ const { client } = useKameleoon();
95
+
96
+ ...
97
+ }
98
+ ```
99
+
100
+ ### `withKameleoon`
101
+ #### Returns
102
+ - `Component: React.Component` - component which will be enhanced with the prop `client: KameleoonClient`.
103
+
104
+ ```jsx
105
+ import { withKameleoon } from '@kameleoon/react-sdk';
106
+
107
+ class MyComponent extends React.Component {
108
+ componentDidMount() {
109
+ const { client } = this.props;
110
+ }
111
+
112
+ ...
113
+ }
114
+
115
+ export default withKameleoon(MyComponent);
116
+ ```
117
+
118
+ ## LocalStorage Key
119
+ - `KAMELEOON_SDK_LOCAL_STORAGE_KEY` - is a constant which used as a key in `LocalStorage` to store a `kameleoonTargetingData` and `kameleoonConfiguration`.
120
+
121
+ ```jsx
122
+ import { KAMELEOON_SDK_LOCAL_STORAGE_KEY } from '@kameleoon/react-sdk'
123
+ ```
124
+
125
+ ## Obtain status and variables of feature flag
126
+ Returns the status of a feature flag and its variables.
127
+
128
+ #### Arguments
129
+ - `featureParams: IFeatureParams`
130
+ - `featureKey: string | number` - unique identifier or key of the feature you want to expose to a user.
131
+ - `variableKey: string | string[]` - key of the variable.
132
+ - `visitorCode: string` (optional) - unique identifier of the user.
133
+
134
+ #### Returns
135
+ - `featureResult: IFeature`
136
+ - `isActive: boolean` - feature flag status.
137
+ - `variables: FeatureVariableType[]` - feature flag variables.
138
+
139
+
140
+ In this examples, if the feature flag with the `red-button` key is enabled, theme of button will set to red.
141
+ ### `useFeature`
142
+ #### Example
143
+ ```jsx
144
+ import { Button } from '@kameleoon/ui';
145
+ import { useFeature, useVisitorCode } from '@kameleoon/react-sdk';
146
+
147
+ function MyComponent(): JSX.Element {
148
+ const { getVisitorCode } = useVisitorCode();
149
+ const { isActive } = useFeature({
150
+ featureKey: 'red-button',
151
+ variableKey: 'red-button',
152
+ visitorCode: getVisitorCode('example.com'),
153
+ });
154
+
155
+ return <Button theme={isActive ? 'red' : 'green'} />;
156
+ }
157
+ ```
158
+
159
+ ### `withFeature`
160
+ #### Example
161
+ ```jsx
162
+ import { Button } from '@kameleoon/ui';
163
+ import { withFeature } from '@kameleoon/react-sdk';
164
+
165
+ class MyComponent extends React.Component {
166
+ render() {
167
+ const { isActive, variables } = this.props;
168
+ return <Button theme={isActive ? 'red' : 'green'} />;
169
+ }
170
+ }
171
+
172
+ export default withFeature({
173
+ featureKey: 'red-button',
174
+ variableKey: 'red-button',
175
+ visitorCode: '280295'
176
+ })(MyComponent);
177
+ ```
178
+
179
+ ### `Feature` render props
180
+ #### Props
181
+ - `children: (featureResult: IFeature) => ReactNode` - child elements of `Feature`.
182
+ - `featureKey: string | number` - unique identifier or key of the feature you want to expose to a user.
183
+ - `variableKey: string` - key of the variable.
184
+ - `visitorCode: string` - unique identifier of the user.
185
+
186
+ #### Returns
187
+ - A wrapped component with the following props:
188
+ - `isActive: boolean` - feature flag status.
189
+ - `variables: FeatureVariableType[]` - feature flag variables.
190
+
191
+ #### Example
192
+ ```jsx
193
+ import { Button } from '@kameleoon/ui';
194
+ import { Feature } from '@kameleoon/react-sdk';
195
+
196
+ class MyComponent extends React.Component {
197
+ render() {
198
+ const { isActive, variables } = this.props;
199
+ return <Button theme={isActive ? 'red' : 'green'} />;
200
+ }
201
+ }
202
+
203
+ class MyCompWrapper extends React.Component {
204
+ render() {
205
+ return(
206
+ <Feature featureKey="red-button" variableKey="red-button" visitorCode="280295">
207
+ (({isActive, variables}) => (
208
+ <MyComponent isActive={isActive} variables={variables} />
209
+ ))
210
+ </Feature>;
211
+ )
212
+ }
213
+ }
214
+ ```
215
+
216
+ ## Obtain visitorCode
217
+ A callback function `getVisitorCode()` retrieves the Kameleoon `visitorCode` for the current visitor. This is especially important when using Kameleoon in a mixed front-end and back-end environment, where user identification consistency must be guaranteed. The implementation logic is described here:
218
+
219
+ 1. First we check if a `kameleoonVisitorCode` cookie can be found. If so, we will use this as the visitor identifier.
220
+
221
+ 2. If no cookie is found, we either randomly generate a new identifier, or use the `defaultVisitorCode` argument as identifier if it is passed. This allows our customers to use their own identifiers as visitor codes, should they wish to. This can have the added benefit of matching Kameleoon visitors with their own users without any additional look-ups in a matching table.
222
+
223
+ 3. In any case, the JavaScript `kameleoonVisitorCode` cookie is set with the value. Then this identifier value is finally returned by the method.
224
+
225
+ For more information, refer to [this article](https://developers.kameleoon.com/back-front-bridge.html).
226
+
227
+ ##### Please Note:
228
+ _If you provide your own `visitorCode`, its uniqueness must be guaranteed on your end - the SDK cannot check it. Also note that the length of `visitorCode` is **limited to 255 characters**. Any excess characters will throw an exception._
229
+
230
+ #### `getVisitorCode()`
231
+
232
+ ##### Arguments
233
+ - `topLevelDomain: string` - current top level domain for the concerned site (this information is needed to set the corresponding cookie accordingly, on the top level domain).
234
+ - `defaultVisitorCode: string` (optional) - This parameter will be used as the `visitorCode` if no existing `kameleoonVisitorCode` cookie is found on the request. This field is optional, and by default a random `visitorCode` will be generated.
235
+
236
+ ##### Returns
237
+ - `visitorCode: string` - a visitorCode that will be associated with this particular user and should be used with most of the methods of the SDK.
238
+
239
+ ### `useVisitorCode`
240
+ #### Returns
241
+ - A callback function `getVisitorCode()`.
242
+
243
+ #### Example
244
+ ```jsx
245
+ import { useEffect } from 'react';
246
+ import { uuidv4 } from 'uuid';
247
+ import { useVisitorCode } from '@kameleoon/react-sdk';
248
+
249
+ function MyComponent(): JSX.Element {
250
+ const { getVisitorCode } = useVisitorCode();
251
+
252
+ useEffect(() => {
253
+ // Without defaultVisitorCode argument
254
+ const visitorCode = getVisitorCode('example.com');
255
+
256
+ // With defaultVisitorCode argument
257
+ const visitorCode = getVisitorCode('example.com', uuidv4());
258
+ }, []);
259
+
260
+ ...
261
+ }
262
+ ```
263
+ ### `withVisitorCode`
264
+ #### Arguments
265
+ - `Component: React.Component` - component which will be enhanced with the prop `getVisitorCode()`.
266
+
267
+ #### Returns
268
+ - A wrapped component with additional props as described above.
269
+
270
+ #### Example
271
+ ```jsx
272
+ import { uuidv4 } from 'uuid';
273
+ import { withVisitorCode } from '@kameleoon/react-sdk';
274
+
275
+ class MyComponent extends React.Component {
276
+ componentDidMount() {
277
+ const { getVisitorCode } = this.props;
278
+
279
+ // Without defaultVisitorCode argument
280
+ const visitorCode = getVisitorCode('example.com');
281
+
282
+ // With defaultVisitorCode argument
283
+ const visitorCode = getVisitorCode('example.com', uuidv4());
284
+ }
285
+
286
+ ...
287
+ }
288
+
289
+ export default withVisitorCode(MyComponent);
290
+ ```
291
+
292
+ ## Triggering an experiment
293
+ A callback function `getVariationId()` takes `visitorCode` and `experimentId` as mandatory arguments to register a variation for a given user.
294
+
295
+ If such a user has never been associated with any variation, the SDK returns a randomly selected variation. If a user with a given `visitorCode` is already registered with a variation, it will detect the previously registered variation and return the `variationId`.
296
+
297
+ You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.
298
+
299
+ ##### Please Note:
300
+ _By convention, the reference (original variation) always has an ID equal to **0**._
301
+
302
+ #### Exceptions Thrown
303
+ - `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.
304
+ - `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.
305
+ - `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).
306
+
307
+ #### `getVariationId()`
308
+ ##### Arguments
309
+ - `visitorCode: string` - unique identifier of the user
310
+ - `experimentId: number` - unique identifier of the experiment you want to expose to a user
311
+
312
+ ##### Returns
313
+ - `variationId: number` - ID of the variation that is registered for a given visitorCode. By convention, the reference (original variation) always has an ID equal to **0**.
314
+
315
+ ### `useTriggerExperiment`
316
+ #### Returns
317
+ - A callback function `getVariationId()`.
318
+
319
+ #### Example
320
+ ```jsx
321
+ import { useEffect } from 'react';
322
+ import { useTriggerExperiment, useVisitorCode } from '@kameleoon/react-sdk';
323
+
324
+ function MyComponent(): JSX.Element {
325
+ const { getVariationId } = useTriggerExperiment();
326
+ const { getVisitorCode } = useVisitorCode();
327
+
328
+ useEffect(() => {
329
+ const visitorCode = getVisitorCode('example.com');
330
+ const experimentId = 12341;
331
+
332
+ const variationId = getVariationId(visitorCode, experimentId);
333
+ }, []);
334
+
335
+ ...
336
+ }
337
+ ```
338
+
339
+ ### `withTriggerExperiment`
340
+ #### Arguments
341
+ - `Component: React.Component` - component which will be enhanced with the prop `getVariationId()`.
342
+
343
+ #### Returns
344
+ - A wrapped component with additional props as described above.
345
+
346
+ #### Example
347
+ ```jsx
348
+ import { withVisitorCode, withTriggerExperiment, compose } from '@kameleoon/react-sdk';
349
+
350
+ class MyComponent extends React.Component {
351
+ componentDidMount() {
352
+ const { getVisitorCode, getVariationId } = this.props;
353
+ const visitorCode = getVisitorCode('example.com');
354
+ const experimentId = 230243;
355
+
356
+ const variationId = getVariationId(visitorCode, experimentId);
357
+ }
358
+
359
+ ...
360
+ }
361
+
362
+ export default compose(withVisitorCode, withTriggerExperiment)(MyComponent);
363
+ ```
364
+
365
+ ## Activate feature
366
+ A callback function `hasFeature()` which validates if user has been associated with this feature. If it is fails, callback returns value **false**, otherwise **true**.
367
+
368
+ If feature flag is not activated, `KameleoonException.FeatureConfigurationNotFound` exception will be thrown. Please, make sure to handle properly the error.
369
+
370
+ #### Exceptions Thrown
371
+ - `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.
372
+ - `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).
373
+
374
+ #### `hasFeature()`
375
+ ##### Arguments
376
+ - `visitorCode: string` - unique identifier of the user.
377
+ - `featureKey: string | number` - unique identifier or key of the feature you want to expose to a user.
378
+
379
+ ##### Returns
380
+ - A `value: boolean` of the feature that is registered for a given `visitorCode`.
381
+
382
+ ### `useActivateFeature`
383
+ #### Returns
384
+ - A callback function `hasFeature()`.
385
+
386
+ #### Example
387
+ ```jsx
388
+ import { useEffect } from 'react';
389
+ import { useActivateFeature, useVisitorCode } from '@kameleoon/react-sdk';
390
+
391
+ function MyComponent(): JSX.Element {
392
+ const { hasFeature } = useActivateFeature();
393
+ const { getVisitorCode } = useVisitorCode();
394
+
395
+ useEffect(() => {
396
+ const visitorCode = getVisitorCode('example.com');
397
+ const featureKey = 'example-feature-key';
398
+
399
+ const feature = hasFeature(visitorCode, featureKey);
400
+ }, []);
401
+
402
+ ...
403
+ }
404
+ ```
405
+
406
+ ### `withActivateFeature`
407
+ #### Arguments
408
+ - `Component: React.Component` - component which will be enhanced with the prop `hasFeature()`.
409
+
410
+ #### Returns
411
+ - A wrapped component with additional props as described above.
412
+
413
+ #### Example
414
+ ```jsx
415
+ import { withVisitorCode, withActivateFeature, compose } from '@kameleoon/react-sdk';
416
+
417
+ class MyComponent extends React.Component {
418
+ componentDidMount() {
419
+ const { getVisitorCode, hasFeature } = this.props;
420
+ const visitorCode = getVisitorCode('example.com');
421
+ const featureKey = 'example-feature-key';
422
+
423
+ const feature = hasFeature(visitorCode, featureKey);
424
+ }
425
+
426
+ ...
427
+ }
428
+
429
+ export default compose(withVisitorCode, withActivateFeature)(MyComponent);
430
+ ```
431
+
432
+ ## Obtain variation associated data
433
+ 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.
434
+
435
+ 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.
436
+
437
+ #### Exceptions Thrown
438
+ - `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.
439
+
440
+ #### `getVariationAssociatedData()`
441
+
442
+ ##### Arguments
443
+ - `variationId: number` - unique identifier of the variation you want to obtain associated data for.
444
+
445
+ ##### Returns
446
+ - Data associated with this `variationId`.
447
+
448
+ ### `useVariationAssociatedData`
449
+ #### Returns
450
+ - A callback function `getVariationAssociatedData()`.
451
+
452
+ #### Example
453
+ ```jsx
454
+ import { useEffect } from 'react';
455
+ import { useVariationAssociatedData } from '@kameleoon/react-sdk';
456
+
457
+ function MyComponent(): JSX.Element {
458
+ const { getVariationAssociatedData } = useVariationAssociatedData();
459
+
460
+ useEffect(() => {
461
+ const variationId = 280295;
462
+ const data = getVariationAssociatedData(variationId);
463
+ }, []);
464
+
465
+ ...
466
+ }
467
+ ```
468
+ ### `withVariationAssociatedData`
469
+ #### Arguments
470
+ - `Component: React.Component` - component which will be enhanced with the prop `getVariationAssociatedData()`.
471
+
472
+ #### Returns
473
+ - A wrapped component with additional props as described above.
474
+
475
+ #### Example
476
+ ```jsx
477
+ import { withVariationAssociatedData } from '@kameleoon/react-sdk';
478
+
479
+ class MyComponent extends React.Component {
480
+ componentDidMount() {
481
+ const { getVariationAssociatedData } = this.props;
482
+ const variationId = 280295;
483
+ const data = getVariationAssociatedData(variationId);
484
+ }
485
+
486
+ ...
487
+ }
488
+
489
+ export default withVariationAssociatedData(MyComponent);
490
+ ```
491
+
492
+ ## Obtain feature variable
493
+ A callback function `getFeatureVariable()` which retrieves a feature variable.
494
+
495
+ This callback function takes two input parameters: `featureKey` and `variableKey`. It will return the data with the expected type, as defined on the web interface. It will throw an exception `(KameleoonException.FeatureConfigurationNotFound)` if the requested feature has not been found in the internal configuration of the SDK.
496
+
497
+ #### Exceptions Thrown
498
+ - `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.
499
+
500
+ #### `getFeatureVariable()`
501
+
502
+ ##### Arguments
503
+ - `featureKey: string | number` - unique identifier or key of the feature you want to obtain to a user.
504
+ - `variableKey: string` - key of the variable.
505
+ ##### Returns
506
+ - `featureVariable: FeatureFlagVariableType` - data associated with this variable for this feature flag
507
+
508
+ ### `useFeatureVariable`
509
+ #### Returns
510
+ - A callback function `getFeatureVariable()`.
511
+
512
+ #### Example
513
+ ```jsx
514
+ import { useEffect } from 'react';
515
+ import { useFeatureVariable } from '@kameleoon/react-sdk';
516
+
517
+ function MyComponent(): JSX.Element {
518
+ const { getFeatureVariable } = useFeatureVariable();
519
+
520
+ useEffect(() => {
521
+ const featureKey = 'example-feature-key';
522
+ const variableKey = 'example-variable-key';
523
+
524
+ const featureVariable = getFeatureVariable(featureKey, variableKey);
525
+ }, []);
526
+
527
+ ...
528
+ }
529
+ ```
530
+
531
+ ### `withFeatureVariable`
532
+ #### Arguments
533
+ - `Component: React.Component` - component which will be enhanced with the prop `getFeatureVariable()`.
534
+
535
+ #### Returns
536
+ - A wrapped component with additional props as described above.
537
+
538
+ #### Example
539
+ ```jsx
540
+ import { withFeatureVariable } from '@kameleoon/react-sdk';
541
+
542
+ class MyComponent extends React.Component {
543
+ componentDidMount() {
544
+ const { getFeatureVariable } = this.props;
545
+ const featureKey = 'example-feature-key';
546
+ const variableKey = 'example-variable-key';
547
+
548
+ const featureVariable = getFeatureVariable(featureKey, variableKey);
549
+ }
550
+
551
+ ...
552
+ }
553
+
554
+ export default withFeatureVariable(MyComponent);
555
+ ```
556
+
557
+ ## Tracking conversion
558
+ A callback function `trackConversion()` which tracks conversion. This callback function requires `visitorCode` and `goalId` to track conversion on this particular goal. In addition, `trackConverion()` accepts third optional argument `revenue` to track revenue. The `visitorCode` is usually identical to the one that was used when triggering the experiment.
559
+
560
+ This callback function doesn't return any value. And it is non-blocking as the server call is made asynchronously.
561
+
562
+ #### `trackConversion()`
563
+ ##### Arguments
564
+ - `visitorCode: string` - unique identifier of the user.
565
+ - `goalId: number` - key of the variable.
566
+ - `revenue: number` (optional) - key of the variable.
567
+
568
+ ### `useTrackingConversion`
569
+ #### Returns
570
+ - A callback function `trackConversion()`;
571
+
572
+ #### Example
573
+ ```jsx
574
+ import { useEffect } from 'react';
575
+ import { useTrackingConversion, useVisitorCode } from '@kameleoon/react-sdk';
576
+
577
+ function MyComponent(): JSX.Element {
578
+ const { getVisitorCode } = useVisitorCode();
579
+ const { trackConversion } = useTrackingConversion();
580
+
581
+ useEffect(() => {
582
+ const visitorCode = getVisitorCode('example.com')
583
+ const goalId = 280295;
584
+
585
+ trackConversion(visitorCode, goalId);
586
+ }, []);
587
+
588
+ ...
589
+ }
590
+ ```
591
+
592
+ ### `withTrackingConversion`
593
+ #### Arguments
594
+ - `Component: React.Component` - component which will be enhanced with the prop `trackConversion()`.
595
+
596
+ #### Returns
597
+ - A wrapped component with additional props as described above.
598
+
599
+ #### Example
600
+ ```jsx
601
+ import { withVisitorCode, withTrackingConversion, compose } from '@kameleoon/react-sdk';
602
+
603
+ class MyComponent extends React.Component {
604
+ componentDidMount() {
605
+ const { getVisitorCode, trackConversion } = this.props;
606
+ const visitorCode = getVisitorCode('example.com');
607
+ const goalId = 230234;
608
+
609
+ trackConversion(visitorCode, goalId);
610
+ }
611
+
612
+ ...
613
+ }
614
+
615
+ export default compose(withVisitorCode, withTrackingConversion)(MyComponent);
616
+ ```
617
+
618
+
619
+ ## Add data
620
+ A callback function `addData()` which adds various data to associate this data with the current user. This callback function requires `visitorCode` as a first parameter and then accepts array of various **Data Types** allowed in Kameleoon.
621
+
622
+ Note that the `addData()` doesn't return any value and doesn't interact with the Kameleoon back-end servers by itself. Instead, all declared data is saved for further sending via the `flush()` described in the next paragraph. This reduces the number of server calls made, as data is usually grouped into a single server call triggered by the execution of `flush()`.
623
+
624
+ ##### Please Note:
625
+ _The `triggerExperiment()` and `trackConversion()` callback functions also sends out previously associated data, exactly as the `flush()`._
626
+
627
+ #### `addData()`
628
+ ##### Arguments
629
+ - `visitorCode: string` - unique identifier of the user.
630
+ - `dataTypes: DataInterface[]` - custom data types.
631
+
632
+ ### `useAddData`
633
+ #### Returns
634
+ - A callback function `addData()`.
635
+
636
+ #### Example
637
+ ```jsx
638
+ import { useEffect } from 'react';
639
+ import { Button } from '@kameleoon/ui';
640
+ import {
641
+ useAddData,
642
+ useBrowser,
643
+ useCutomData,
644
+ useVisitorCode,
645
+ Browser
646
+ } from '@kameleoon/react-sdk';
647
+
648
+ function MyComponent(): JSX.Element {
649
+ const { getVisitorCode } = useVisitorCode();
650
+ const { addData } = useAddData();
651
+ const { addBrowser } = useBrowser();
652
+ const { addCustomData } = useCutomData();
653
+
654
+ useEffect(() => {
655
+ const visitorCode = getVisitorCode('example.com');
656
+
657
+ // Single data type passed
658
+ addData(visitorCode, [addBrowser(Browser.Chrome)]);
659
+
660
+ // Several data types passed
661
+ addData(visitorCode, [addBrowser(Browser.Chrome), addCustomData(1, 'some custom value')]);
662
+ }, []);
663
+
664
+ ...
665
+ }
666
+ ```
667
+
668
+ ### `withAddData`
669
+ #### Arguments
670
+ - `Component: React.Component` - component which will be enhanced with the prop `addData()`.
671
+
672
+ #### Returns
673
+ - A wrapped component with additional props as described above.
674
+
675
+ #### Example
676
+ ```jsx
677
+ import {
678
+ withAddData,
679
+ withBrowser,
680
+ withCustomData,
681
+ withVisitorCode,
682
+ Browser,
683
+ compose,
684
+ } from '@kameleoon/react-sdk';
685
+
686
+ class MyComponent extends React.Component {
687
+ componentDidMount() {
688
+ const { addData, addBrowser, addCustomData, getVisitorCode } = this.props;
689
+ const visitorCode = getVisitorCode('example.com');
690
+
691
+ // Single data type passed
692
+ addData(visitorCode, [addBrowser(Browser.Chrome)]);
693
+
694
+ // Several data types passed
695
+ addData(visitorCode, [
696
+ addBrowser(Browser.Chrome),
697
+ addCustomData(1, 'some custom value'),
698
+ ]);
699
+ }
700
+
701
+ ...
702
+ }
703
+
704
+ export default compose(
705
+ withAddData,
706
+ withBrowser,
707
+ withCustomData,
708
+ withVisitorCode,
709
+ )(MyComponent);
710
+ ```
711
+
712
+ ## Flush data
713
+ Data associated with the current user via `addData()` is not immediately sent to the server. It is stored and accumulated until it is sent automatically by the `triggerExperiment()` or `trackConversion()`, or manually by the `flush()` callback function. This allows the developer to control exactly when the data is flushed to servers. For instance, if you call the `addData()` a dozen times, it would be a waste of resources to send data to the server after each `addData()` invocation. Just call `flush()` once at the end.
714
+
715
+ The `flush()` callback function is non-blocking as the server call is made asynchronously.
716
+
717
+ #### `flush()`
718
+ ##### Arguments
719
+ - `visitorCode: string` - unique identifier of the user.
720
+
721
+ ### `useFlush`
722
+ #### Returns
723
+ - A callback function `flush()`.
724
+
725
+ #### Example
726
+ ```jsx
727
+ import { useEffect } from 'react';
728
+ import { Button } from '@kameleoon/ui';
729
+ import {
730
+ useAddData,
731
+ useBrowser,
732
+ useVisitorCode,
733
+ useFlush,
734
+ Browser
735
+ } from '@kameleoon/react-sdk';
736
+
737
+ function MyComponent(): JSX.Element {
738
+ const { getVisitorCode } = useVisitorCode();
739
+ const { addData } = useAddData();
740
+ const { addBrowser } = useBrowser();
741
+ const { flush } = useFlush();
742
+
743
+ useEffect(() => {
744
+ const visitorCode = getVisitorCode('example.com');
745
+
746
+ addData(visitorCode, [addBrowser(Browser.Chrome)]);
747
+ flush(visitorCode);
748
+ }, []);
749
+
750
+ ...
751
+ }
752
+ ```
753
+
754
+ ### `withFlush`
755
+ #### Arguments
756
+ - `Component: React.Component` - component which will be enhanced with the prop `flush()`.
757
+
758
+ #### Returns
759
+ - A wrapped component with additional props as described above.
760
+
761
+ #### Example
762
+ ```jsx
763
+ import {
764
+ withVisitorCode,
765
+ withAddData,
766
+ withBrowser,
767
+ withFlush,
768
+ Browser,
769
+ compose,
770
+ } from '@kameleoon/react-sdk';
771
+
772
+ class MyComponent extends React.Component {
773
+ componentDidMount() {
774
+ const { addData, addBrowser, flush, getVisitorCode } = this.props;
775
+ const visitorCode = getVisitorCode('example.com');
776
+
777
+ addData(visitorCode, [addBrowser(Browser.Chrome)]);
778
+ flush(visitorCode);
779
+ }
780
+
781
+ ...
782
+ }
783
+
784
+ export default compose(
785
+ withAddData,
786
+ withBrowser,
787
+ withFlush,
788
+ withVisitorCode,
789
+ )(MyComponent);
790
+ ```
791
+
792
+
793
+ ## Browser
794
+ A callback function `addBrowser()` adds browser type.
795
+
796
+ #### `addBrowser`
797
+ ##### Arguments
798
+ - `browser: Browser` - browser types: CHROME, INTERNET_EXPLORER, FIREFOX, SAFARI, OPERA, OTHER.
799
+
800
+ ##### Returns
801
+ - An instance of `Browser: IBrowser`.
802
+
803
+ ### `useBrowser`
804
+ #### Returns
805
+ - A callback function `addBrowser()`.
806
+
807
+ #### Example
808
+ ```jsx
809
+ import { useEffect } from 'react';
810
+ import { useAddData, useBrowser, useVisitorCode, Browser } from '@kameleoon/react-sdk';
811
+
812
+ function MyComponent(): JSX.Element {
813
+ const { addData } = useAddData();
814
+ const { getVisitorCode } = useVisitorCode();
815
+ const { addBrowser } = useBrowser();
816
+
817
+ useEffect(() => {
818
+ const visitorCode = getVisitorCode('example.com');
819
+
820
+ addData(visitorCode, [addBrowser(Browser.Chrome)]);
821
+ }, []);
822
+
823
+ ...
824
+ }
825
+ ```
826
+
827
+ ### `withBrowser`
828
+ #### Arguments
829
+ - `Component: React.Component` - component which will be enhanced with the prop `addBrowser()`.
830
+
831
+ #### Returns
832
+ - A wrapped component with additional props as described above.
833
+
834
+ #### Example
835
+ ```jsx
836
+ import {
837
+ withVisitorCode,
838
+ withAddData,
839
+ withBrowser,
840
+ Browser,
841
+ compose,
842
+ } from '@kameleoon/react-sdk';
843
+
844
+ class MyComponent extends React.Component {
845
+ componentDidMount() {
846
+ const { addData, addBrowser, getVisitorCode } = this.props;
847
+ const visitorCode = getVisitorCode('example.com');
848
+
849
+ addData(visitorCode, [addBrowser(Browser.Chrome)]);
850
+ }
851
+
852
+ ...
853
+ }
854
+
855
+ export default compose(
856
+ withAddData,
857
+ withBrowser,
858
+ withVisitorCode,
859
+ )(MyComponent);
860
+ ```
861
+
862
+ ## PageView
863
+ A callback function `addPageView()` adds page view.
864
+
865
+ #### `addPageView`
866
+ ##### Arguments
867
+ - `url: string` - URL of the page viewed.
868
+ - `title: string` - title of the page viewed.
869
+ - `referrer: number` (optional) - referrer of the page viewed.
870
+
871
+ ##### Returns
872
+ - An instance of `PageView: IPageView`.
873
+ ### `usePageView`
874
+ #### Returns
875
+ - A callback function `addPageView()`.
876
+
877
+ #### Example
878
+ ```jsx
879
+ import { useEffect } from 'react';
880
+ import { useAddData, usePageView, useVisitorCode } from '@kameleoon/react-sdk';
881
+
882
+ function MyComponent(): JSX.Element {
883
+ const { addData } = useAddData();
884
+ const { getVisitorCode } = useVisitorCode();
885
+ const { addPageView } = usePageView();
886
+
887
+ useEffect(() => {
888
+ const visitorCode = getVisitorCode('example.com');
889
+
890
+ addData(visitorCode, [addPageView('example.com', 'title', 3)]);
891
+ }, []);
892
+
893
+ ...
894
+ }
895
+ ```
896
+
897
+ ### `withPageView`
898
+ #### Arguments
899
+ - `Component: React.Component` - component which will be enhanced with the prop `addPageView()` which recieves the following arguments and returns instance of `PageView: IPageView`.
900
+ - `url: string` - URL of the page viewed.
901
+ - `title: string` - title of the page viewed.
902
+ - `referrer: number` (optional) - referrer of the page viewed.
903
+
904
+ #### Returns
905
+ - A wrapped component with additional props as described above.
906
+
907
+ #### Example
908
+ ```jsx
909
+ import {
910
+ withVisitorCode,
911
+ withAddData,
912
+ withPageView,
913
+ compose,
914
+ } from '@kameleoon/react-sdk';
915
+
916
+ class MyComponent extends React.Component {
917
+ componentDidMount() {
918
+ const { addData, addPageView, getVisitorCode } = this.props;
919
+ const visitorCode = getVisitorCode('example.com');
920
+
921
+ addData(visitorCode, [addPageView('example.com', 'title', 3)]);
922
+ }
923
+
924
+ ...
925
+ }
926
+
927
+ export default compose(
928
+ withAddData,
929
+ withPageView,
930
+ withVisitorCode,
931
+ )(MyComponent);
932
+ ```
933
+
934
+ ## Conversion
935
+ A callback function `addConversion()` adds conversion.
936
+
937
+ #### `addConversion`
938
+ ##### Arguments
939
+ - `goalId: number` - unique identifier of the goal.
940
+ - `revenue: number` (optional) - coversion revenue.
941
+ - `negative: boolean` (optional) - defines if the revenue is positive or negative.
942
+
943
+ ##### Returns
944
+ - An instance of `Conversion: IConversion`.
945
+
946
+ ### `useConversion`
947
+ #### Returns
948
+ - A callback function `addConversion()`.
949
+
950
+ #### Example
951
+ ```jsx
952
+ import { useEffect } from 'react';
953
+ import { useAddData, useConversion, useVisitorCode } from '@kameleoon/react-sdk';
954
+
955
+ function MyComponent(): JSX.Element {
956
+ const { addData } = useAddData();
957
+ const { getVisitorCode } = useVisitorCode();
958
+ const { addConversion } = useConversion();
959
+
960
+ useEffect(() => {
961
+ const visitorCode = getVisitorCode('example.com');
962
+
963
+ addData(visitorCode, [addConversion(32, 10, false)]);
964
+ }, []);
965
+
966
+ ...
967
+ }
968
+ ```
969
+
970
+ ### `withConversion`
971
+ #### Arguments
972
+ - `Component: React.Component` - component which will be enhanced with the prop `addConversion()`.
973
+
974
+ #### Returns
975
+ - A wrapped component with additional props as described above.
976
+
977
+ #### Example
978
+ ```jsx
979
+ import {
980
+ withVisitorCode,
981
+ withAddData,
982
+ withConversion,
983
+ compose,
984
+ } from '@kameleoon/react-sdk';
985
+
986
+ class MyComponent extends React.Component {
987
+ componentDidMount() {
988
+ const { addData, addConversion, getVisitorCode } = this.props;
989
+ const visitorCode = getVisitorCode('example.com');
990
+
991
+ addData(visitorCode, [addConversion(32, 10, false)]);
992
+ }
993
+
994
+ ...
995
+ }
996
+
997
+ export default compose(
998
+ withAddData,
999
+ withConversion,
1000
+ withVisitorCode,
1001
+ )(MyComponent);
1002
+ ```
1003
+
1004
+ ## CustomData
1005
+ A callback function `addCustomData()` adds custom data.
1006
+
1007
+ ##### Please Note:
1008
+ _The index (ID) of the custom data is available on our Back-Office, in the Custom data configuration page. Be careful: this index starts at **0**, so the first custom data you create for a given site will have the ID **0**, **not 1**._
1009
+
1010
+ #### `addCustomData`
1011
+ ##### Arguments
1012
+ - `index: number` - index or unique identifier of the custom data to be stored.
1013
+ - `value: string` - value of the custom data to be stored.
1014
+
1015
+ ##### Returns
1016
+ - An instance of `CustomData: ICustomData`.
1017
+
1018
+ ### `useCustomData`
1019
+ #### Returns
1020
+ - A callback function `addCustomData()`.
1021
+
1022
+
1023
+ #### Example
1024
+ ```jsx
1025
+ import { useEffect } from 'react';
1026
+ import { useAddData, useCustomData, useVisitorCode } from '@kameleoon/react-sdk';
1027
+
1028
+ function MyComponent(): JSX.Element {
1029
+ const { addData } = useAddData();
1030
+ const { getVisitorCode } = useVisitorCode();
1031
+ const { addCustomData } = useCustomData();
1032
+
1033
+ useEffect(() => {
1034
+ const visitorCode = getVisitorCode('example.com');
1035
+
1036
+ addData(visitorCode, [addCustomData(1, 'some custom value')]);
1037
+ }, []);
1038
+
1039
+ ...
1040
+ }
1041
+ ```
1042
+
1043
+ ### `withCustomData`
1044
+ #### Arguments
1045
+ - `Component: React.Component` - component which will be enhanced with the prop `addCustomData()`.
1046
+
1047
+ #### Returns
1048
+ - A wrapped component with additional props as described above.
1049
+
1050
+ #### Example
1051
+ ```jsx
1052
+ import {
1053
+ withVisitorCode,
1054
+ withAddData,
1055
+ withCustomData,
1056
+ compose,
1057
+ } from '@kameleoon/react-sdk';
1058
+
1059
+ class MyComponent extends React.Component {
1060
+ componentDidMount() {
1061
+ const { addData, addCustomData, getVisitorCode } = this.props;
1062
+ const visitorCode = getVisitorCode('example.com');
1063
+
1064
+ addData(visitorCode, [addCustomData(1, 'some custom value')]);
1065
+ }
1066
+
1067
+ ...
1068
+ }
1069
+
1070
+ export default compose(
1071
+ withAddData,
1072
+ withCustomData,
1073
+ withVisitorCode,
1074
+ )(MyComponent);
1075
+ ```
1076
+
1077
+ ## Interest
1078
+ A callback function `addInterest()` adds interest.
1079
+
1080
+ #### `addInterest`
1081
+ ##### Arguments
1082
+ - `index: number` - index of interest.
1083
+
1084
+ ##### Returns
1085
+ - An instance of `Interest: IInterest`.
1086
+
1087
+ ### `useInterest`
1088
+ #### Returns
1089
+ - A callback function `addInterest()`.
1090
+
1091
+ #### Example
1092
+ ```jsx
1093
+ import { useEffect } from 'react';
1094
+ import { useAddData, useInterest, useVisitorCode } from '@kameleoon/react-sdk';
1095
+
1096
+ function MyComponent(): JSX.Element {
1097
+ const { addData } = useAddData();
1098
+ const { getVisitorCode } = useVisitorCode();
1099
+ const { addInterest } = useInterest();
1100
+
1101
+ useEffect(() => {
1102
+ const visitorCode = getVisitorCode('example.com');
1103
+
1104
+ addData(visitorCode, [addInterest(0)]);
1105
+ }, []);
1106
+
1107
+ ...
1108
+ }
1109
+ ```
1110
+
1111
+ ### `withInterest`
1112
+ #### Arguments
1113
+ - `Component: React.Component` - component which will be enhanced with the prop `addInterest()`.
1114
+
1115
+ #### Returns
1116
+ - A wrapped component with additional props as described above.
1117
+
1118
+ #### Example
1119
+ ```jsx
1120
+ import {
1121
+ withVisitorCode,
1122
+ withAddData,
1123
+ withInterest,
1124
+ compose,
1125
+ } from '@kameleoon/react-sdk';
1126
+
1127
+ class MyComponent extends React.Component {
1128
+ componentDidMount() {
1129
+ const { addData, addInterest, getVisitorCode } = this.props;
1130
+ const visitorCode = getVisitorCode('example.com');
1131
+
1132
+ addData(visitorCode, [addInterest(0)]);
1133
+ }
1134
+
1135
+ ...
1136
+ }
1137
+
1138
+ export default compose(
1139
+ withAddData,
1140
+ withInterest,
1141
+ withVisitorCode,
1142
+ )(MyComponent);
1143
+ ```
1144
+
1145
+ ## Helpers
1146
+ ### `compose`
1147
+ Composes single-argument high order components.
1148
+
1149
+ #### Arguments
1150
+ - `HOCs` - high order components to be composed.
1151
+
1152
+ #### Returns
1153
+ - A function obtained by composing the argument HOCs, which is used as single high order component wrapper.
1154
+
1155
+ #### Example
1156
+ ```jsx
1157
+ import { withVisitorCode, withTriggerExperiment, compose } from '@kameleoon/react-sdk';
1158
+
1159
+ class MyComponent extends React.Component {
1160
+ componentDidMount() {
1161
+ const { getVisitorCode, getVariationId } = this.props;
1162
+ const visitorCode = getVisitorCode('example.com');
1163
+ const experimentId = 230243;
1164
+
1165
+ const variationId = getVariationId(visitorCode, experimentId);
1166
+ }
1167
+
1168
+ ...
1169
+ }
1170
+
1171
+ export default compose(withVisitorCode, withTriggerExperiment)(MyComponent);
1172
+ ```