Skip to content

Commit 1680d22

Browse files
authored
docs(react-sdk,browser-sdk): updated various out-of-date docs (#338)
- Simplified the getting started instructions - Cleaned up outdates "features update" instructions. There's "Event listeners" instead. - Update docs to reflect that `featureOptions` are passed at the root level now
1 parent 62b55a8 commit 1680d22

3 files changed

Lines changed: 90 additions & 127 deletions

File tree

packages/browser-sdk/README.md

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ type Configuration = {
100100
sseBaseUrl?: "https://livemessaging.bucket.co";
101101
feedback?: undefined; // See FEEDBACK.md
102102
enableTracking?: true; // set to `false` to stop sending track events and user/company updates to Bucket servers. Useful when you're impersonating a user
103-
featureOptions?: {
104-
fallbackFeatures?:
105-
| string[]
106-
| Record<string, { key: string; payload: any } | true>; // Enable these features if unable to contact bucket.co. Can be a list of feature keys or a record with configuration values
107-
timeoutMs?: number; // Timeout for fetching features (default: 5000ms)
108-
staleWhileRevalidate?: boolean; // Revalidate in the background when cached features turn stale to avoid latency in the UI (default: false)
109-
staleTimeMs?: number; // at initialization time features are loaded from the cache unless they have gone stale. Defaults to 0 which means the cache is disabled. Increase in the case of a non-SPA
110-
expireTimeMs?: number; // In case we're unable to fetch features from Bucket, cached/stale features will be used instead until they expire after `expireTimeMs`. Default is 30 days
111-
};
103+
fallbackFeatures?:
104+
| string[]
105+
| Record<string, { key: string; payload: any } | true>; // Enable these features if unable to contact bucket.co. Can be a list of feature keys or a record with configuration values
106+
timeoutMs?: number; // Timeout for fetching features (default: 5000ms)
107+
staleWhileRevalidate?: boolean; // Revalidate in the background when cached features turn stale to avoid latency in the UI (default: false)
108+
staleTimeMs?: number; // at initialization time features are loaded from the cache unless they have gone stale. Defaults to 0 which means the cache is disabled. Increase this in the case of a non-SPA
109+
expireTimeMs?: number; // In case we're unable to fetch features from Bucket, cached/stale features will be used instead until they expire after `expireTimeMs`. Default is 30 days
112110
};
113111
```
114112

@@ -194,23 +192,6 @@ const override = bucketClient.getFeatureOverride("huddle"); // returns boolean |
194192

195193
Feature overrides are persisted in `localStorage` and will be restored when the page is reloaded.
196194

197-
### Feature Updates
198-
199-
You can listen for feature updates using `onFeaturesUpdated`:
200-
201-
```ts
202-
// Register a callback for feature updates
203-
const unsubscribe = bucketClient.onFeaturesUpdated(() => {
204-
console.log("Features were updated");
205-
});
206-
207-
// Later, stop listening for updates
208-
unsubscribe();
209-
```
210-
211-
> [!NOTE]
212-
> Note that the callback may be called even if features haven't actually changed.
213-
214195
### Remote config
215196

216197
Similar to `isEnabled`, each feature has a `config` property. This configuration is managed from within Bucket.
@@ -312,11 +293,12 @@ See details in [Feedback HTTP API](https://docs.bucket.co/reference/http-trackin
312293

313294
Event listeners allow for capturing various events occurring in the `BucketClient`. This is useful to build integrations with other system or for various debugging purposes. There are 5 kinds of events:
314295

315-
- FeaturesUpdated
316-
- User
317-
- Company
318-
- Check
319-
- Track
296+
- `configCheck`: Your code used a feature config
297+
- `enabledCheck`: Your code checked whether a specific feature should be enabled
298+
- `featuresUpdated`: Features were updated. Either because they were loaded as part of initialization or because the user/company updated
299+
- `user`: User information updated (similar to the `identify` call used in tracking terminology)
300+
- `company`: Company information updated (sometimes to the `group` call used in tracking terminology)
301+
- `track`: Track event occurred.
320302

321303
Use the `on()` method to add an event listener to respond to certain events. See the API reference for details on each hook.
322304

packages/react-sdk/README.md

Lines changed: 77 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Install via npm:
1010
npm i @bucketco/react-sdk
1111
```
1212

13-
## Setup
13+
## Get started
1414

1515
### 1. Define Features (optional)
1616

@@ -39,8 +39,7 @@ declare module "@bucketco/react-sdk" {
3939

4040
### 2. Add the `BucketProvider` context provider
4141

42-
Add the `BucketProvider` context provider to your application.
43-
This will initialize the Bucket SDK, fetch features and start listening for automated feedback survey events.
42+
Add the `BucketProvider` context provider to your application:
4443

4544
**Example:**
4645

@@ -52,107 +51,33 @@ import { BucketProvider } from "@bucketco/react-sdk";
5251
company={{ id: "acme_inc", plan: "pro" }}
5352
user={{ id: "john doe" }}
5453
loadingComponent={<Loading />}
55-
featureOptions={{ fallbackFeatures: ["huddle"] }}
5654
>
5755
{/* children here are shown when loading finishes or immediately if no `loadingComponent` is given */}
5856
</BucketProvider>;
5957
```
6058

61-
- `publishableKey` is used to connect the provider to an _environment_ on Bucket. Find your `publishableKey` under [environment settings](https://app.bucket.co/envs/current/settings/app-environments) in Bucket,
62-
- `company`, `user` and `otherContext` make up the _context_ that is used to determine if a feature is enabled or not. `company` and `user` contexts are automatically transmitted to Bucket servers so the Bucket app can show you which companies have access to which features etc.
63-
> [!Note]
64-
> If you specify `company` and/or `user` they must have at least the `id` property, otherwise they will be ignored in their entirety. You should also supply anything additional you want to be able to evaluate feature targeting against,
65-
- `featureOptions` contains configuration for features:
66-
67-
- `fallbackFeatures`: A list of strings which specify which features to consider enabled if the SDK is unable to fetch features. Can be provided in two formats:
68-
69-
```ts
70-
// Simple array of feature keys
71-
featureOptions={{
72-
fallbackFeatures: ["feature1", "feature2"]
73-
}}
74-
75-
// Or with configuration overrides
76-
featureOptions={{
77-
fallbackFeatures: {
78-
"feature1": true, // just enable the feature
79-
"feature2": { // enable with configuration
80-
key: "variant-a",
81-
payload: {
82-
limit: 100,
83-
mode: "test"
84-
}
85-
}
86-
}
87-
}}
88-
```
59+
### 3. Use `useFeature(<featureKey>)` to get feature status
8960

90-
- `timeoutMs`: Timeout in milliseconds when fetching features from the server,
91-
- `staleWhileRevalidate`: If set to `true`, stale features will be returned while refetching features in the background,
92-
- `expireTimeMs`: If set, features will be cached between page loads for this duration (in milliseconds),
93-
- `staleTimeMs`: Maximum time (in milliseconds) that stale features will be returned if `staleWhileRevalidate` is true and new features cannot be fetched.
61+
Using the `useFeature` hook from your components lets you toggle features on/off and configure features through Remote Config:
9462

95-
Example with all options:
63+
**Example:**
9664

9765
```tsx
98-
<BucketProvider
99-
publishableKey={YOUR_PUBLISHABLE_KEY}
100-
featureOptions={{
101-
// Fallback features if server is unreachable
102-
fallbackFeatures: {
103-
"premium-feature": {
104-
key: "basic",
105-
payload: { maxItems: 10 },
106-
},
107-
},
108-
// Timeout after 5 seconds
109-
timeoutMs: 5000,
110-
// Return stale data while fetching
111-
staleWhileRevalidate: true,
112-
// Cache features for 1 hour
113-
expireTimeMs: 60 * 60 * 1000,
114-
// Allow stale data up to 5 minutes
115-
staleTimeMs: 5 * 60 * 1000,
116-
}}
117-
// ... other props
118-
>
119-
{children}
120-
</BucketProvider>
121-
```
122-
123-
- `loadingComponent` lets you specify an React component to be rendered instead of the children while the Bucket provider is initializing. If you want more control over loading screens, `useFeature()` returns `isLoading` which you can use to customize the loading experience:
124-
125-
```tsx
126-
function LoadingBucket({ children }) {
127-
const { isLoading } = useFeature("myFeature")
128-
if (isLoading) {
129-
return <Spinner />
130-
}
66+
function StartHuddleButton() {
67+
const {
68+
isEnabled, // boolean indicating if the feature is enabled
69+
track, // track usage of the feature
70+
} = useFeature("huddle");
13171

132-
return children
72+
if (!isEnabled) {
73+
return null;
13374
}
13475

135-
//-- Initialize the Bucket provider
136-
<BucketProvider publishableKey={YOUR_PUBLISHABLE_KEY} /*...*/>
137-
<LoadingBucket>
138-
{/* children here are shown when loading finishes */}
139-
</LoadingBucket>
140-
<BucketProvider>
141-
```
76+
return <button onClick={track}>Start huddle!</button>;
77+
}
78+
```
14279

143-
- `enableTracking` (default: `true`): Set to `false` to stop sending tracking events and user/company updates to Bucket. Useful when you're impersonating a user,
144-
- `apiBaseUrl`: Optional base URL for the Bucket API. Use this to override the default API endpoint,
145-
- `appBaseUrl`: Optional base URL for the Bucket application. Use this to override the default app URL,
146-
- `sseBaseUrl`: Optional base URL for Server-Sent Events. Use this to override the default SSE endpoint,
147-
- `debug`: Set to `true` to enable debug logging to the console,
148-
- `toolbar`: Optional configuration for the Bucket toolbar,
149-
- `feedback`: Optional configuration for feedback collection:
150-
151-
```ts
152-
{
153-
enableLiveSatisfaction: boolean; // Enable/disable live satisfaction surveys
154-
}
155-
```
80+
`useFeature` can help you do much more. See a full example for `useFeature` [see below](#usefeature).
15681

15782
## Feature toggles
15883

@@ -162,7 +87,7 @@ If you supply `user` or `company` objects, they must include at least the `id` p
16287
In addition to the `id`, you must also supply anything additional that you want to be able to evaluate feature targeting rules against.
16388
The additional attributes are supplied using the `otherContext` prop.
16489

165-
Attributes cannot be nested (multiple levels) and must be either strings, integers or booleans.
90+
Attributes cannot be nested (multiple levels) and must be either strings, numbers or booleans.
16691
A number of special attributes exist:
16792

16893
- `name` -- display name for `user`/`company`,
@@ -212,6 +137,65 @@ configuration in your application.
212137
Note that, similar to `isEnabled`, accessing `config` on the object returned by `useFeature()` automatically
213138
generates a `check` event.
214139

140+
## `<BucketProvider>` component
141+
142+
The `<BucketProvider>` initializes the Bucket SDK, fetches features and starts listening for automated feedback survey events. The component can be configured using a number of props:
143+
144+
- `publishableKey` is used to connect the provider to an _environment_ on Bucket. Find your `publishableKey` under [environment settings](https://app.bucket.co/envs/current/settings/app-environments) in Bucket,
145+
- `company`, `user` and `otherContext` make up the _context_ that is used to determine if a feature is enabled or not. `company` and `user` contexts are automatically transmitted to Bucket servers so the Bucket app can show you which companies have access to which features etc.
146+
> [!Note]
147+
> If you specify `company` and/or `user` they must have at least the `id` property, otherwise they will be ignored in their entirety. You should also supply anything additional you want to be able to evaluate feature targeting against,
148+
- `fallbackFeatures`: A list of strings which specify which features to consider enabled if the SDK is unable to fetch features. Can be provided in two formats:
149+
150+
```ts
151+
// Simple array of feature keys
152+
fallbackFeatures={["feature1", "feature2"]}
153+
154+
// Or with configuration overrides
155+
fallbackFeatures: {
156+
"feature1": true, // just enable the feature
157+
"feature2": { // enable with configuration
158+
key: "variant-a",
159+
payload: {
160+
limit: 100,
161+
mode: "test"
162+
}
163+
}
164+
}
165+
```
166+
167+
- `timeoutMs`: Timeout in milliseconds when fetching features from the server,
168+
- `staleWhileRevalidate`: If set to `true`, stale features will be returned while refetching features in the background,
169+
- `expireTimeMs`: If set, features will be cached between page loads for this duration (in milliseconds),
170+
- `staleTimeMs`: Maximum time (in milliseconds) that stale features will be returned if `staleWhileRevalidate` is true and new features cannot be fetched.
171+
- `loadingComponent` lets you specify an React component to be rendered instead of the children while the Bucket provider is initializing. If you want more control over loading screens, `useFeature()` returns `isLoading` which you can use to customize the loading experience:
172+
173+
```tsx
174+
function LoadingBucket({ children }) {
175+
const { isLoading } = useFeature("myFeature")
176+
if (isLoading) {
177+
return <Spinner />
178+
}
179+
180+
return children
181+
}
182+
183+
//-- Initialize the Bucket provider
184+
<BucketProvider publishableKey={YOUR_PUBLISHABLE_KEY} /*...*/>
185+
<LoadingBucket>
186+
{/* children here are shown when loading finishes */}
187+
</LoadingBucket>
188+
<BucketProvider>
189+
```
190+
191+
- `enableTracking`: Set to `false` to stop sending tracking events and user/company updates to Bucket. Useful when you're impersonating a user (defaults to `true`),
192+
- `apiBaseUrl`: Optional base URL for the Bucket API. Use this to override the default API endpoint,
193+
- `appBaseUrl`: Optional base URL for the Bucket application. Use this to override the default app URL,
194+
- `sseBaseUrl`: Optional base URL for Server-Sent Events. Use this to override the default SSE endpoint,
195+
- `debug`: Set to `true` to enable debug logging to the console,
196+
- `toolbar`: Optional configuration for the Bucket toolbar,
197+
- `feedback`: Optional configuration for feedback collection
198+
215199
## Hooks
216200

217201
### `useFeature()`
@@ -248,7 +232,7 @@ function StartHuddleButton() {
248232
<button
249233
onClick={(e) =>
250234
requestFeedback({
251-
title: payload?.question ?? "How do you like Huddles?",
235+
title: payload?.question ?? "How do you like the Huddles feature?",
252236
position: {
253237
type: "POPOVER",
254238
anchor: e.currentTarget as HTMLElement,

packages/react-sdk/dev/plain/app.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,6 @@ export function App() {
221221
return (
222222
<BucketProvider
223223
publishableKey={publishableKey}
224-
feedback={{
225-
enableLiveSatisfaction: true,
226-
}}
227224
company={initialCompany}
228225
user={initialUser}
229226
otherContext={initialOtherContext}

0 commit comments

Comments
 (0)