ChatALL icon indicating copy to clipboard operation
ChatALL copied to clipboard

Add a composable used to get the Matomo instance in SFC

Open jerray opened this issue 1 year ago • 2 comments

After exploring the code of the "vue-matoto", I found that it initializes the Matomo instance asynchronously. This prevents the Matomo instance from being accessible to components immediately after the app is mounted.

To make it work in SFC, a possible solution without touching the code of "vue-matomo" would be to create a composable called useMatomo. Within this function, create a ref of null, then start a timer to check if window.Piwik exists. Once window.Piwik becomes available, assign the value of window.Piwik.getAsyncTracker() to the ref. Finally, return the ref as the output of the function.

Then we can use Matomo in SFC as follows:

const $matomo = useMatomo();

const onSomeCallback = () => {
  $matomo.value?.trackEvent();
}

This can be used to fix the problems in #72

jerray avatar May 19 '23 08:05 jerray

Looks great! Why not patch other files to use this?

sunner avatar May 19 '23 09:05 sunner

At first, I planned to refactor the ChatMessage component, but I found that another PR had already accomplished it. I wondered why the inject did not work and then found a solution. Certainly, I can provide my version of refactoring it.

Looks great! Why not patch other files to use this?

jerray avatar May 19 '23 13:05 jerray

@jerray if your PR is accepted I could integrate your composable on my PR 😁

k4lu-0p avatar May 19 '23 19:05 k4lu-0p

@jerray if your PR is accepted I could integrate your composable on my PR 😁

@k4lu-0p Launch it! @jerray Thank you again.

sunner avatar May 19 '23 22:05 sunner

@jerray if your PR is accepted I could integrate your composable on my PR 😁

@k4lu-0p Launch it! @jerray Thank you again.

done !

k4lu-0p avatar May 19 '23 22:05 k4lu-0p