https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
f

fast-psychiatrist-23708

02/11/2024, 12:40 AM
Hey! I was wondering if someone could offer me some support with linking growthbooks to my webserver .js program. I have written the following function which is simply supposed to grab a feature flag which confirms works in the iOS app. I have created a new Client Key for JS and everything and have no idea why I keep getting undefined back from the feature flags. My Client Key also has not indicated that it has established a connection like it has with my iOS app.
const { GrowthBook } = require("@growthbook/growthbook");
async function getABTestResult(growthbookId) {
const growthbook = new GrowthBook({
apiHost: "<https://cdn.growthbook.io>",
clientKey: "JS_CLIENT_KEY",
enableDevMode: true,
subscribeToChanges: true,
trackingCallback: (experiment, result) => {
// TODO: Use your real analytics tracking system
console.log("Viewed Experiment", {
experimentId: experiment.key,
variationId: result.key
});
}
});
growthbook.setAttributes({
"id": growthbookId,
"loggedIn": true,
"country": "US"
});
// Wait for features to be available
try {
await growthbook.loadFeatures();
console.log("Features loaded successfully.");
} catch (error) {
console.error("Failed to load features:", error);
}  // console: Features loaded successfully.
const value = growthbook.getFeatureValue(
"live_version",
"fallback"
);
console.log(value); // console: fallback
if (growthbook.isOn("my-feature")) {
console.log("Feature is enabled!")
} else {
console.log("ooff")
}  // console: ooff
}
4 Views