Giter Site home page Giter Site logo

moritzsternemann / vue-plausible Goto Github PK

View Code? Open in Web Editor NEW
120.0 5.0 8.0 947 KB

Plausible Analytics Vue.js Plugin and NuxtJS Module

License: MIT License

TypeScript 100.00%
plausible plausible-analytics vue vuejs nuxt nuxtjs nuxt-module vue-plugin plugin analytics

vue-plausible's Introduction

Plausible Analytics for Vue.js and NuxtJS

NPM

Vue.js plugin and NuxtJS module for Plausible Analytics

Installation

Using npm:

npm install vue-plausible

Using yarn:

yarn add vue-plausible

Vue.js Plugin

import Vue from 'vue'
import { VuePlausible } from 'vue-plausible'

Vue.use(VuePlausible, {
  // see configuration section
})

To enable automatic page view tracking for SPAs, call the enableAutoPageviews() method. To enable automatic outbound link tracking, call the enableAutoOutboundTracking() method.

NuxtJS Module

// nuxt.config.js

// optional when using nuxt.config.ts
/// <reference types="vue-plausible" />

export default {
  modules: [
    'vue-plausible'
  ]
}

Module Options

Add a plausible section to nuxt.config.js to set the module options:

// nuxt.config.js

export default {
  plausible: {
    // see configuration section
  }
}

Runtime Config

To use dynamic environment variables in production, use publicRuntimeConfig. Otherwise, the configuration options passed in nuxt.config.js will be read once and bundled during the build only. See the configuration section for all available options.

// nuxt.config.js

export default {
  buildModules: [
    'vue-plausible'
  ],
  plausible: { // Use as fallback if no runtime config is available at runtime
    domain: process.env.PLAUSIBLE_DOMAIN
  },
  publicRuntimeConfig: {
    plausible: {
      domain: process.env.PLAUSIBLE_DOMAIN,
      apiHost: process.env.PLAUSIBLE_API_HOST
    }
  }
}

Usage

vue-plausible is based on the official plausible-tracker package.

Configuration

Configuration options are inherited from plausible-tracker:

Option Type Description Default
domain string Your site's domain name, as declared by you in Plausible's settings. location.hostname
hashMode boolean Enables tracking based on URL hash changes. false
trackLocalhost boolean Enables tracking on localhost. false
apiHost string Plausible's API host to use. Change this if you are self-hosting. https://plausible.io
enableAutoPageviews boolean Enables automatic pageview tracking in SPAs. Learn more true
enableAutoOutboundTracking boolean Enables automatic outbound link click tracking. Learn more false

Integration

The plausible-tracker package provides various methods to track specific events, for example trackPageview() and trackEvent(). You can find all the available methods and options in the plausible-tracker documentation. The Plausible instance is exposed to your Vue.js or NuxtJS app in the following ways:

  • Vue.$plausible (Vue.js only)
  • this.$plausible inside Vue components
  • context.app.$plausible inside asyncData, fetch, plugins, middleware, nuxtServerInit (NuxtJS only)
  • this.$plausible inside Vuex stores (NuxtJS only)

Proxying

To use proxying as described in the Plausible 'Using a proxy' documentation, you need to adjust the apiHost configuration option accordingly. Using vue-plausible only requires to proxy the /api/event endpoint since the frontend code is already bundled from plausible-tracker.

Please note that event endpoint path always ends with /api/event. See the following example:

plausible: {
  apiHost: 'https://<yourdomain.com>/stats' // Reports events to https://<yourdomain.com>/stats/api/event
}

Opt out

To exclude yourself from the analytics, plausible-tracker offers an opt-out mechanism that can be activated by setting the localStorage.plausible_ignore value to true.

More information can be found in the Plausible documentation.

Author

I'm Moritz Sternemann, a computer-science student at Technical University of Munich.

License

This project is available under the MIT license. See the LICENSE file for more information.

vue-plausible's People

Contributors

ambroisemaupate avatar bewe11 avatar btkostner avatar danielroe avatar moritzsternemann avatar tillsanders avatar tobbentm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vue-plausible's Issues

Make nuxt module options overridable with runtimeConfig

This would allow changing Plausible options over different environments (dev, testing, production) using Nuxt runtimeConfig.


For example in nuxt/axios

https://github.com/nuxt-community/axios-module/blob/main/lib/module.js#L11


const PlausibleModule: Module<PlausibleOptions> = function (moduleOptions) {
  const { nuxt } = this

  const options = {
    ...defaultOptions,
    ...this.options.plausible,
    ...moduleOptions,
    ...(nuxt.options.runtimeConfig && nuxt.options.runtimeConfig.plausible)
  }

  this.addPlugin({
    src: path.resolve(__dirname, '../esm/nuxt-plugin.js'),
    mode: 'client',
    ssr: false,
    fileName: 'vue-plausible.client.js',
    options
  })
}

Plausible response is always a 202

I'm not sure if this is more relevant here or over on plausible-tracker, but I'm having a hell of a time getting this module working properly.

I'd like to just set a proxy to go straight to plausible.io/js/script.js, and it seemed like this package was part of the road to get there, but the instructions provided by this module don't apply to my use case. I'm not self-hosting Plausible, so setting apiHost to my domain will never work. Instead, I'm trying what seems like the right direction (though I have no idea), which is to set Plausible as normal, and set a proxy to go from my site to the Plausible API via nuxt-proxy. But no matter what I do, if I get anywhere close to right with either of these libraries, I'm only getting as far as 202 Accepted, and the traffic isn't registering on the dashboard.

Here are my current settings:

nuxt.config.js

...
plausible: {
    domain: "https://my-site-name.com",
},
proxy {
  "/api/event": {
      target: "https://plausible.io/api/event",
   },
},

Is this unnecessary? Is there some way to use the apiHost property to proxy without self-hosting, or is there something else I should be doing?

localStorage crash in anonymous mode in Chrome

Hi, I have a problem with this amazing plugin because it uses plausible-tracker in version 0.3.4. Do you think you can update this dependency? Because I think since version 0.3.6 plausible-tracker has localStorage access detection. The thing is, when you use incognito mode in Chrome, you can't access localStorage (unless you explicitly allow it in the settings as a user) and it throws an error in plausible-tracker -> which causes Nuxt to crash during Nuxt initialization, causing a fatal error => app crash

enableAutoOutboundTracking option

I'm trying to see if we can get the enableAutoOutboundTracking functionality working.

https://github.com/plausible/plausible-tracker/blob/master/README.md#outbound-link-click-tracking

I tried passing it in my nuxt.config as plausible.enableAutoOutboundTracking: true but it doesn't seem to be an option (it's done a bit differently in the plausible-tracker code.

Is there another way to do this or am I messing something else up? I added the custom event in my dashboard as well but no luck.

Thanks for the plugin!

Cannot read properties of undefined (reading 'options')

Upon upgrading Nuxt 3 to the current stable build, getting an error.

 ERROR  Cannot read properties of undefined (reading 'options')                                                15:06:27

  at PlausibleModule (node_modules/.pnpm/[email protected]/node_modules/vue-plausible/lib/cjs/nuxt-module.js:18:89)
  at installModule (node_modules/.pnpm/@[email protected][email protected]/node_modules/@nuxt/kit/dist/index.mjs:416:9)
  at async initNuxt (node_modules/.pnpm/[email protected]_jbw4qtqtinc3kuqpq2aa3cjwz4/node_modules/nuxt/dist/index.mjs:1825:7)
  at async loadNuxt (node_modules/.pnpm/[email protected]_jbw4qtqtinc3kuqpq2aa3cjwz4/node_modules/nuxt/dist/index.mjs:1857:5)
  at async loadNuxt (node_modules/.pnpm/@[email protected][email protected]/node_modules/@nuxt/kit/dist/index.mjs:710:19)
  at async Object.invoke (node_modules/.pnpm/[email protected]/node_modules/nuxi/dist/chunks/prepare.mjs:30:18)
  at async _main (node_modules/.pnpm/[email protected]/node_modules/nuxi/dist/cli.mjs:50:20)
Nuxi 3.0.0                                                                                                     15:09:29
                                                                                                               15:09:29
RootDir: /Users/josh/Repositories/fnf-entries-app                                                              15:09:29
Nuxt project info: (copied to clipboard)                                                                       15:09:29

------------------------------
- Operating System: `Darwin`
- Node Version:     `v17.5.0`
- Nuxt Version:     `3.0.0`
- Nitro Version:    `1.0.0`
- Package Manager:  `[email protected]`
- Builder:          `vite`
- User Config:      `modules`, `runtimeConfig`, `apollo`, `plausible`
- Runtime Modules:  `@nuxtjs/[email protected]`, `[email protected]`, `@nuxtjs/[email protected]`, `[email protected]`
- Build Modules:    `-`
------------------------------

enableAutoOutboundTracking breaks some <router-link> and <a @click> behavior.

When using vue-plausible with enableAutoOutboundTracking, clicking router-links and tags sometimes triggers a page refresh. I'm not sure what causes the error, but disabling enableAutoOutboundTracking fixes tags that otherwise causes a hard-refresh. This seems to happen with and tags that have #click event attached to them.

Request executed twice

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

Request called twice

Hello!
I'm having almost the same issue : "Request executed twice #3".

I'm using Nuxt.js too (with default SSR), except that I only use the nuxt.config.js configuration, I don't call it somewhere else 🤔, and it's the same endpoint for both

modules: [
  'vue-plausible'
],

plausible: {
  apiHost: 'https://plausible.example.com'
},

image

Any idea ?
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.