moritzsternemann / vue-plausible

Plausible Analytics Vue.js Plugin and NuxtJS Module
MIT License
120 stars 8 forks source link

Request executed twice #3

Closed gambolputty closed 3 years ago

gambolputty commented 3 years ago

Hello!

In my Nuxt app the request to /api/event is emitted twice. The first request is targeted at my custom host defined in the apiHost property in nuxt.config.js (self-hosted Plausible instance). The second one is sent to https://plausible.io/api/event. Is this intentional?

This code is executed in my layout file when mounted:

    const { enableAutoPageviews } = Plausible()

    // This tracks the current page view and all future ones as well
    enableAutoPageviews()

image

moritzsternemann commented 3 years ago

Hey @gambolputty!

When you call Plausible() like in you snippet, a new instance of the plausible tracker is created with the default settings.

The event duplication is then caused by having two instances of the tracker, one with your apiHost, created by the Nuxt module and configured in nuxt.config.js. The second instance is initialized by the call in your layout file.

If you want to access the plausible tracker instance in any of you components, including layouts, I suggest to use the properties explained in the Usage section of the README.

gambolputty commented 3 years ago

My mistake. Thanks for clarifying! Will close this issue now.