Skip to content

Commit

Permalink
chore: adding more samples for precomp
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-statsig committed Mar 7, 2024
1 parent 9e25aa1 commit 75aa78d
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 2 deletions.
10 changes: 9 additions & 1 deletion samples/react/src/SamplesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { ReactNode, useEffect } from 'react';

const SAMPLES = [
import('./samples/PrecomputedClientBasic'),
// Precomputed Evaluations Client
import('./samples/precomputed-client/PrecomputedInitialize'),
import('./samples/precomputed-client/PrecomputedClientBasic'),
import('./samples/precomputed-client/PrecomputedClientCheckGate'),
import('./samples/precomputed-client/PrecomputedClientGetDynamicConfig'),
import('./samples/precomputed-client/PrecomputedClientGetLayer'),
import('./samples/precomputed-client/PrecomputedClientGetLogEvent'),
import('./samples/precomputed-client/PrecomputedClientShutdown'),

import('./samples/OnDeviceClientBasic'),
import('./samples/BootstrapInit'),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { PrecomputedEvaluationsClient } from '@statsig/precomputed-evaluations';

// </snippet>
import { STATSIG_CLIENT_KEY as YOUR_CLIENT_KEY } from '../Contants';
import { STATSIG_CLIENT_KEY as YOUR_CLIENT_KEY } from '../../Contants';

// prettier-ignore
export default async function Sample(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { myStatsigClient } from './PrecomputedClientInstance';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
if (myStatsigClient.checkGate("new_homepage_design")) {
// Gate is on, show new home page
} else {
// Gate is off, show old home page
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { myStatsigClient } from './PrecomputedClientInstance';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
const dynamicConfig = myStatsigClient.getDynamicConfig("awesome_product_details");
const itemName = dynamicConfig.value["product_name"] ?? "Some Fallback";
const price = dynamicConfig.value["price"] ?? 10.0;

if (dynamicConfig.value["is_discount_enabled"] === true) {
// apply some discount logic
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { myStatsigClient } from './PrecomputedClientInstance';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
// Values via getLayer

const layer = myStatsigClient.getLayer("user_promo_experiments");

const promoTitle = layer.getValue("title") ?? "Welcome to Statsig!";
const discount = layer.getValue("discount") ?? 0.1;
// </snippet>

viaGetExperiment()
}

// prettier-ignore
function viaGetExperiment() {
// <snippet>

// or, via getExperiment

const titleExperiment = myStatsigClient.getExperiment("new_user_promo_title");
const priceExperiment = myStatsigClient.getExperiment("new_user_promo_price");

const promoTitle = titleExperiment.value["title"] ?? "Welcome to Statsig!";
const discount = priceExperiment.value["discount"] ?? 0.1;
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { myStatsigClient } from './PrecomputedClientInstance';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
myStatsigClient.logEvent({
eventName: 'add_to_cart',
value: 'SKU_12345',
metadata: {
price: '9.99',
item_name: 'diet_coke_48_pack',
},
});
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PrecomputedEvaluationsClient } from '@statsig/precomputed-evaluations';

import { STATSIG_CLIENT_KEY } from '../../Contants';

export const myStatsigClient = new PrecomputedEvaluationsClient(
STATSIG_CLIENT_KEY,
{ userID: '' },
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { myStatsigClient } from './PrecomputedClientInstance';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
await myStatsigClient.shutdown();
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <snippet>
import { PrecomputedEvaluationsClient } from '@statsig/precomputed-evaluations';

// </snippet>
import { STATSIG_CLIENT_KEY as YOUR_CLIENT_KEY } from '../../Contants';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
const myStatsigClient = new PrecomputedEvaluationsClient(
YOUR_CLIENT_KEY,
{ userID: 'a-user' },
{ environment: { tier: 'development' } } // (optional) Configure SDK via StatsigOptions here
);

await myStatsigClient.initialize();
// </snippet>
}

0 comments on commit 75aa78d

Please sign in to comment.