-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add more snippets around stable id (#321)
- Loading branch information
1 parent
d8ec2cb
commit d9e7a7b
Showing
5 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
samples/react/src/samples/precomputed-client/sample-precomp-access-stable-id.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* eslint-disable no-console */ | ||
import { myStatsigClient } from './sample-precomp-instance'; | ||
|
||
// <snippet> | ||
const context = myStatsigClient.getContext(); | ||
console.log('Statsig StableID:', context.stableID); | ||
// </snippet> |
86 changes: 86 additions & 0 deletions
86
samples/react/src/samples/precomputed-client/sample-precomp-bootstrap-full.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
|
||
/* eslint-disable no-console */ | ||
import Statsig, { StatsigUser } from 'statsig-node'; | ||
|
||
import { StatsigClient } from '@statsig/js-client'; | ||
|
||
import { STATSIG_CLIENT_KEY as YOUR_CLIENT_KEY } from '../../Contants'; | ||
|
||
type Request = { | ||
body: { user: StatsigUser }; | ||
}; | ||
type Response = { | ||
json: (input: ResponseType) => void; | ||
}; | ||
type RequestHandler = (req: Request, res: Response) => Promise<void>; | ||
type ResponseType = { | ||
values: string; | ||
user: StatsigUser; | ||
}; | ||
|
||
const app = { | ||
post: (url: string, handler: RequestHandler) => { | ||
console.log('req', url, handler); | ||
}, | ||
}; | ||
|
||
function generateStableID(): string { | ||
return ''; | ||
} | ||
|
||
// <snippet> | ||
// -- Server Side -- | ||
const isStatsigServerReady = Statsig.initialize( | ||
process.env['STATSIG_SERVER_KEY']!, | ||
); | ||
|
||
app.post('/init-statsig-client', async (req, res) => { | ||
await isStatsigServerReady; | ||
|
||
const user = req.body.user; | ||
if (!user.customIDs || !user.customIDs['stableID']) { | ||
user.customIDs = { | ||
...user.customIDs, | ||
stableID: generateStableID(), | ||
}; | ||
} | ||
|
||
const values = Statsig.getClientInitializeResponse( | ||
user, | ||
YOUR_CLIENT_KEY, // <- Client Key | ||
{ | ||
hash: 'djb2', | ||
}, | ||
); | ||
|
||
res.json({ values: JSON.stringify(values), user }); | ||
}); | ||
// </snippet> | ||
|
||
// prettier-ignore | ||
//<snippet> | ||
|
||
// -- Client Side -- | ||
function loadUserData(): string { | ||
// Returns the request body containing you user with optional stableID | ||
// </snippet> | ||
return ''; | ||
//<snippet> | ||
} | ||
|
||
// </snippet> | ||
|
||
// prettier-ignore | ||
export async function Sample(): Promise<void> { | ||
// <snippet> | ||
const { values, user: serverVerifiedUser } = await fetch('/init-statsig-client', { | ||
method: 'POST', | ||
body: loadUserData(), | ||
}).then((res) => res.json() as Promise<ResponseType>); | ||
|
||
const myStatsigClient = new StatsigClient(YOUR_CLIENT_KEY, serverVerifiedUser); | ||
myStatsigClient.dataAdapter.setData(values); | ||
myStatsigClient.initializeSync(); | ||
// </snippet> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
samples/react/src/samples/react-precomp/sample-react-precomp-access-stable-id.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// <snippet> | ||
import { useStatsigClient } from '@statsig/react-bindings'; | ||
|
||
function MyComponent() { | ||
const { client } = useStatsigClient(); | ||
const context = client.getContext(); | ||
|
||
return <div>{context.stableID}</div>; | ||
} | ||
// </snippet> | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(MyComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters