gtm-support / vue-gtm

Simple implementation of Google Tag Manager for Vue
https://www.npmjs.com/package/@gtm-support/vue-gtm
MIT License
192 stars 23 forks source link

Return gtmPlugin from app context #409

Closed filiphazardous closed 1 month ago

filiphazardous commented 5 months ago

I have a problem with Vite inline, which in production mode just copies the useGtm function wholesale to the calling file, without copying the scope. This fix solves that problem, since it always fetches the gtm instance from the vue app context

filiphazardous commented 5 months ago

Here's the bug in nuxt-gtm that this would solve (at least in my use-case): https://github.com/zadigetvoltaire/nuxt-gtm/issues/14

Shinigami92 commented 5 months ago

Looks like getCurrentInstance is there since Vue v3.0.0. So I guess this is safe.

https://github.com/vuejs/core/commit/9c0f820a8e4a934227175b2911090658064d6017

Shinigami92 commented 5 months ago

Is it possible to add a test for this? So we can prevent regression?

filiphazardous commented 5 months ago

Is it possible to add a test for this? So we can prevent regression?

I'll see if I can come up with a way to test it. What triggered it in our case was two levels of indirection (it was initialized through nuxt-gtm and useGtm was called directly in our project, so I guess Vite couldn't connect the dots). I'll see if I can reproduce a similar sandbox.

Shinigami92 commented 5 months ago

Is it possible to add a test for this? So we can prevent regression?

I'll see if I can come up with a way to test it. What triggered it in our case was two levels of indirection (it was initialized through nuxt-gtm and useGtm was called directly in our project, so I guess Vite couldn't connect the dots). I'll see if I can reproduce a similar sandbox.

Maybe spy or mocks can help to simplify it