Skip to content

What is the correct way of implementing Nuxt Meta Pixel on Production only? (Without losing typescript support) #16

Description

@phillipmohr

I would like to implement the Nuxt Meta Pixel on production only and track events.

Problem: My current setup is working as intended but when I use $fbq I'm getting '$fbq' is of type 'unknown'.ts(18046)

I'm aware of the $production option in the nuxt config which I have used like so:

$production: {
      modules: [
          'nuxt-meta-pixel'
      ],
}

My runtime config in nuxt config looks like so:

runtimeConfig: {
  public: {
    metapixel: {
           // ID is in .env
            default: { id: '', pageView: '**', autoconfig: true }
    },
  }
}

This is how I track an event on a certain page:

const { $fbq } = useNuxtApp()
const { NODE_ENV } = useRuntimeConfig().public

onMounted(() => {
    if (NODE_ENV === 'production') {
       // `'$fbq' is of type 'unknown'.ts(18046)`
        $fbq('track', 'Lead')
    }
})

Is this approach correct for only implementing the tracking pixel on production? How can I keep Typescript support when importing $fbq?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions