little-camera-7128
08/09/2024, 11:30 AMrhythmic-agent-34208
08/09/2024, 4:20 PMlittle-camera-7128
08/09/2024, 4:25 PMrhythmic-agent-34208
08/12/2024, 3:54 PMlittle-camera-7128
08/13/2024, 8:21 AMgrowthBook.client.ts
which initialises the plugin and creates a growthBook client provider ($gbClient
) that I can use within the components:
export const growthBookPlugin = {
install(
app: App,
{ featuresEndpoint, enableDevMode = false }: GrowthBookVuePluginConfig
) {
let growthBook: GrowthBook | null = null
const init = async (): Promise<GrowthBook | null> => {
if (growthBook) {
return growthBook
}
try {
const json = await getFeaturesJson(featuresEndpoint)
growthBook = new GrowthBook({
enableDevMode
})
growthBook.setPayload(json.features)
return growthBook
} catch (e) {
console.error('GrowthBook Vue plugin error', e)
return null
}
}
app.provide<GrowthBookProvider>(growthBookKey, {
init
})
}
}
export default defineNuxtPlugin(async nuxtApp => {
nuxtApp.vueApp.use(growthBookPlugin, {
featuresEndpoint:
'<https://cdn.growthbook.io/api/features/[SDK-key]>',
enableDevMode: true
})
const growthBookInjectable = inject(growthBookKey)
console.log('GrowthBook Injected', growthBookInjectable)
const growthBook = await growthBookInjectable?.init()
if (!growthBook) {
console.error('GrowthBook failed to initialize')
return {}
}
console.log('GrowthBook', growthBook)
growthBook.setAttributes({
loggedIn: false,
id: Math.random().toString(36).substring(2)
})
return {
provide: {
gbClient: growthBook
}
}
})
Then inside my component onMount
I’m trying to retrieve the feature flag value:
onMounted(async () => {
if (process.client) {
const { $gbClient } = useNuxtApp()
if ($gbClient === undefined) {
console.error('GrowthBook client or context not found', $gbClient)
return
}
console.log('GB Payload', $gbClient.getPayload())
// const value = $gbClient.evalFeature(flagKey).value
const value = $gbClient.getFeatureValue('poc-homepage-test-string', null)
console.log('GB Test Feature Flag value', value)
}
})
However, I’m getting null
even though I can see the poc-homepage-test-string
feature flag value in the payload
which I console log just before I call getFeatureValue
. I’ve tried using evalFeature
as well but same issue (I’m not sure which is the correct one to use because in the code it says getFeatureValue
is depricated, rather use evalFeature
but the Vue documentation says otherwise.little-camera-7128
08/13/2024, 1:31 PMexport const growthBookPlugin
as mentioned in the vue docs here. Instead I just did this to create a growthBook instance:
const growthBook = new GrowthBook({
clientKey: [my-key],
enableDevMode: true
})
await growthBook.init()
Which then leverages the init
function with the growthBook sdk instead of overwriting it according to the Vue docsrhythmic-agent-34208
08/13/2024, 2:54 PMlittle-camera-7128
08/13/2024, 2:59 PMlittle-camera-7128
08/13/2024, 3:00 PM