@scaleway/use-growthbook 1.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/.npmignore +5 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE.md +21 -0
- package/README.md +52 -0
- package/dist/AbTestProvider.js +58 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +3 -0
- package/dist/useAbTestAttributes.js +14 -0
- package/package.json +36 -0
package/.npmignore
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @scaleway/use-growthbook
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- [#1434](https://github.com/scaleway/scaleway-lib/pull/1434) [`4695cbc`](https://github.com/scaleway/scaleway-lib/commit/4695cbce29e4ac688607b97d67401e33923c92f3) Thanks [@Slashgear](https://github.com/Slashgear)! - Initialize the AB test common package for integration of GrowthBook inside Scaleway frontend apps
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Scaleway
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# `@scaleway/use-growthbook`
|
|
2
|
+
|
|
3
|
+
Tiny adapter to easily add [GrowthBook](https://www.growthbook.io/) to your React Application.
|
|
4
|
+
The idea of this package is to propose a _facade pattern_ above GrowthBook React SDK.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ pnpm add @scaleway/use-growthbook
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## How to use
|
|
13
|
+
|
|
14
|
+
### First add the provider to your application roots
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { AbTestProvider } from '@scaleway/use-growthbook'
|
|
18
|
+
import React from 'react'
|
|
19
|
+
import ReactDOM from 'react-dom'
|
|
20
|
+
import App from './App'
|
|
21
|
+
|
|
22
|
+
ReactDOM.render(
|
|
23
|
+
<React.StrictMode>
|
|
24
|
+
<AbTestProvider
|
|
25
|
+
config={{ apiHost: 'string', clientKey: 'string', enableDevMode: true }}
|
|
26
|
+
anonymousId="123456789"
|
|
27
|
+
trackingCallback={(experiment, result) => console.log(experiment, result)}
|
|
28
|
+
errorCallback={console.error}
|
|
29
|
+
>
|
|
30
|
+
<App />
|
|
31
|
+
</AbTestProvider>
|
|
32
|
+
</React.StrictMode>,
|
|
33
|
+
document.getElementById('root'),
|
|
34
|
+
)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Attributes
|
|
38
|
+
|
|
39
|
+
A hook `useAbTestAttributes` is available to get currentAttributes and to set new ones dynamically.
|
|
40
|
+
|
|
41
|
+
### API
|
|
42
|
+
|
|
43
|
+
Exported utils from GrowthBook React are listed here:
|
|
44
|
+
|
|
45
|
+
- FeatureString
|
|
46
|
+
- FeaturesReady
|
|
47
|
+
- IfFeatureEnabled
|
|
48
|
+
- useExperiment
|
|
49
|
+
- useFeature
|
|
50
|
+
- withRunExperiment
|
|
51
|
+
- useFeatureIsOn
|
|
52
|
+
- useFeatureValue
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { GrowthBookProvider, GrowthBook } from '@growthbook/growthbook-react';
|
|
2
|
+
import { useMemo, useCallback, useEffect } from 'react';
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
const getGrowthBookInstance = _ref => {
|
|
6
|
+
let {
|
|
7
|
+
config: {
|
|
8
|
+
apiHost,
|
|
9
|
+
clientKey,
|
|
10
|
+
enableDevMode
|
|
11
|
+
},
|
|
12
|
+
anonymousId,
|
|
13
|
+
trackingCallback
|
|
14
|
+
} = _ref;
|
|
15
|
+
return new GrowthBook({
|
|
16
|
+
apiHost,
|
|
17
|
+
clientKey,
|
|
18
|
+
enableDevMode,
|
|
19
|
+
attributes: {
|
|
20
|
+
anonymousId,
|
|
21
|
+
userId: undefined,
|
|
22
|
+
organizationId: undefined,
|
|
23
|
+
organizationType: undefined
|
|
24
|
+
},
|
|
25
|
+
trackingCallback
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const AbTestProvider = _ref2 => {
|
|
29
|
+
let {
|
|
30
|
+
children,
|
|
31
|
+
config,
|
|
32
|
+
anonymousId,
|
|
33
|
+
trackingCallback,
|
|
34
|
+
errorCallback
|
|
35
|
+
} = _ref2;
|
|
36
|
+
const growthbook = useMemo(() => getGrowthBookInstance({
|
|
37
|
+
config,
|
|
38
|
+
anonymousId,
|
|
39
|
+
trackingCallback
|
|
40
|
+
}), [trackingCallback, config, anonymousId]);
|
|
41
|
+
const loadFeature = useCallback(async () => {
|
|
42
|
+
if (config.clientKey) {
|
|
43
|
+
await growthbook.loadFeatures({
|
|
44
|
+
autoRefresh: false,
|
|
45
|
+
timeout: 500
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}, [growthbook, config]);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
loadFeature().catch(errorCallback);
|
|
51
|
+
}, [loadFeature, errorCallback]);
|
|
52
|
+
return jsx(GrowthBookProvider, {
|
|
53
|
+
growthbook: growthbook,
|
|
54
|
+
children: children
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { AbTestProvider };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, withRunExperiment } from '@growthbook/growthbook-react';
|
|
2
|
+
import * as react from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
type Attributes = Record<string, unknown>;
|
|
6
|
+
|
|
7
|
+
declare const useAbTestAttributes: () => [Attributes, (attributes: Attributes) => null | undefined];
|
|
8
|
+
|
|
9
|
+
type ToolConfig = {
|
|
10
|
+
apiHost: string;
|
|
11
|
+
clientKey: string;
|
|
12
|
+
enableDevMode: boolean;
|
|
13
|
+
};
|
|
14
|
+
type TrackingCallback = (experiment: {
|
|
15
|
+
key: string;
|
|
16
|
+
}, result: {
|
|
17
|
+
key: string;
|
|
18
|
+
}) => null;
|
|
19
|
+
type AbTestProviderProps = {
|
|
20
|
+
children: ReactNode;
|
|
21
|
+
anonymousId: string;
|
|
22
|
+
config: ToolConfig;
|
|
23
|
+
trackingCallback: TrackingCallback;
|
|
24
|
+
errorCallback: (error: string) => null;
|
|
25
|
+
};
|
|
26
|
+
declare const AbTestProvider: ({ children, config, anonymousId, trackingCallback, errorCallback, }: AbTestProviderProps) => react.JSX.Element;
|
|
27
|
+
|
|
28
|
+
export { AbTestProvider, useAbTestAttributes };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, withRunExperiment } from '@growthbook/growthbook-react';
|
|
2
|
+
export { useAbTestAttributes } from './useAbTestAttributes.js';
|
|
3
|
+
export { AbTestProvider } from './AbTestProvider.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useGrowthBook } from '@growthbook/growthbook-react';
|
|
2
|
+
import { useMemo, useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
const useAbTestAttributes = () => {
|
|
5
|
+
const growthBook = useGrowthBook();
|
|
6
|
+
const attributes = useMemo(() => growthBook?.getAttributes() ?? {}, [growthBook]);
|
|
7
|
+
const setAttributes = useCallback(newAttributes => growthBook?.setAttributes({
|
|
8
|
+
...attributes,
|
|
9
|
+
...newAttributes
|
|
10
|
+
}), [growthBook, attributes]);
|
|
11
|
+
return [attributes, setAttributes];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { useAbTestAttributes };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scaleway/use-growthbook",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Utility package to expose AB test tool",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=14.x"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/scaleway/scaleway-lib",
|
|
20
|
+
"directory": "packages/growthbook"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"ab",
|
|
25
|
+
"feature flags"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@growthbook/growthbook-react": "0.17.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"react": "18.2.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": "18.x"
|
|
35
|
+
}
|
|
36
|
+
}
|