Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i use in nuxt3 with provide function #10

Open
kpouters opened this issue Aug 5, 2023 · 3 comments
Open

how can i use in nuxt3 with provide function #10

kpouters opened this issue Aug 5, 2023 · 3 comments

Comments

@kpouters
Copy link

kpouters commented Aug 5, 2023

how can i use in nuxt3 with provide function to set visitor

@chrisspiegl
Copy link

chrisspiegl commented Sep 22, 2023

This would be a great addition. But judging by the engagement on this repo, I don't see that happening any time soon?

Also, the injections are wildly crazy in the way they just overwrite words without any namespacing.

@Harm-Nullix
Copy link

Just use window.Tawk_API after load
These guys are not going to fix this for you is my guess

@chrisspiegl
Copy link

That's how I ended up doing it. Sadly, I think you are right @Harm-Nullix, this vue3 component does not appear to be very high on the list for Tawk. We've started thinking about an alternative provider because of the dev api/docs/utilities.

In the mean time, maybe my implementation can help someone to implement their solution with Nuxt3:

I built this plugin / provider:

import TawkMessengerVue from '@tawk.to/tawk-messenger-vue-3'
import { consola } from 'consola'

export default defineNuxtPlugin((nuxtApp) => {
  const logger = consola.withTag('Plugin/TawkWidget.Client')
  logger.debug('🛠️ Setup')

  logger.debug('Loading Tawk')
  nuxtApp.vueApp.use(TawkMessengerVue, {
    propertyId: useRuntimeConfig().public.tawkto.idProperty,
    widgetId: useRuntimeConfig().public.tawkto.idWidget,
  })

  return {
    provide: {
      tawk: window.Tawk_API,
    },
  }
})

Which then lets me do things like this:

try {
  const { $tawk } = useNuxtApp()
  const tawkOnLoad = inject('onLoad')
  tawkOnLoad(() => {
    logger.debug('🦜 Tawk finished loading?', $tawk)
    watchImmediate(route, (route) => {
      const options = {
        route: route.path,
      }
      try {
        $tawk.setAttributes(options, result => logger.debug('🦜 Tawk set attributes', options, result))
      }
      catch (error) {
        logger.warn('Tawk Error while setAttributes', error)
      }
    })
    watchImmediate(userCurrent, (user) => {
      const options = {
        name: user?.displayName,
        email: user?.email,
        userId: user?.uid,
        subscription: userCurrentClaims.value?.stripe?.status || 'none',
        status: user ? 'registered' : 'anonymous',
      }
      try {
        $tawk.setAttributes(options, result => logger.debug('🦜 Tawk set attributes', options, result))
      }
      catch (error) {
        logger.warn('Tawk Error while setAttributes', error)
      }
    })
  })
}
catch (error) {
  logger.warn('Tawk Loading Error', error)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants