https://www.growthbook.io/ logo
b

breezy-king-73167

05/05/2023, 5:22 AM
Hi. Has anyone able to successfully use GB in Nuxt 2? I'm having an issue where I can see the features in localStorage but no matter what I do, getFeatureValue and isOn is not working with the feature key. Even with the chrome extension, it is saying that there is no feature or experiment.
f

fresh-football-47124

05/05/2023, 6:06 AM
I know it works with Nuxt
seems like its making the network call?
could there be two instances somehow?
b

breezy-king-73167

05/05/2023, 6:08 AM
Hmm.. two instances of gb or the feature flag?
Found it
r

ripe-answer-39685

05/17/2023, 12:46 AM
@breezy-king-73167 I am having some issues with a feature not working in Chrome. When you say “found it”, what was the cause of your issue / how did you fix it? Thanks
b

breezy-king-73167

05/17/2023, 12:55 AM
Apparently, you have to load the features first even though it was stored already in local storage. Add this to your plugin.
Copy code
await gb.loadFeatures({ autoRefresh: true });
r

ripe-answer-39685

05/17/2023, 12:55 AM
Thanks for the tip
b

breezy-king-73167

05/17/2023, 1:05 AM
In case this can help, I created this simple boilerplate for Nuxt 2 and GA4/GTAG for our POC.
Copy code
import { GrowthBook } from '@growthbook/growthbook'

export default ({ env }, inject) => {
  let growthBook = null
  /**
   * For initializing Growthbook
   * @returns (null/Growthbook) Returns GrowthBook or null
   */
  const initGrowthBook = async function () {
    /* Prevent reinitializing Growthbook */
    if (growthBook) {
      return growthBook
    }

    /* Initialize Growthbook */
    growthBook = new GrowthBook({
      apiHost: env.GROWTHBOOK_API_HOST,
      clientKey: env.GROWTHBOOK_API_KEY,
      enableDevMode: env.NODE_ENV === 'development',
      trackingCallback: (experiment, result) => {
        // track using GA4
        if ("gtag" in window) {
          this.$gtag.query("event", "experiment_viewed", {
            event_category: "experiment",
            experiment_id: experiment.key,
            variation_id: result.variationId,
          })
        } else {
          console.warn("no gtag")
        }
      }
    })

    //Set user attributes
    await this.$gtag.query('get', env.GA4_TRACKING_ID, 'client_id', (client_id)=>{
      growthBook.setAttributes({
        country: 'france',
        id: client_id,
      })
    })

    /* Load Growthbook features */
    try {
      await growthBook.loadFeatures({
        autoRefresh: true,
        timeout: 2000,
      })

      return growthBook
    } catch (error) {
      console.error('Failed to load features from GrowthBook', error)
      return null
    }
  }

  inject('initGrowthBook', initGrowthBook)
f

fresh-football-47124

05/17/2023, 1:19 AM
Cool - we can add that to our docs - would you like to contribute it?
b

breezy-king-73167

05/22/2023, 2:30 AM
SLR. Sure. Haha. If it is alright? 🙂
f

fresh-football-47124

05/22/2023, 5:53 AM
Of course, we’re open source
b

breezy-king-73167

05/22/2023, 10:16 PM
Alright will do. Reading the CONTRIBUTING.md 🙂
a

aloof-easter-14325

07/04/2023, 9:19 AM
@breezy-king-73167 I try to use this but it didn't work, it said
loadFeatures
is not a function, did you ever experience it?
5 Views