Skip to content

Commit

Permalink
fix: nuxt-module-plausible no longer throws an error on navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
timelytree committed Feb 29, 2024
1 parent a65554f commit 28b8d9c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
8 changes: 4 additions & 4 deletions modules/nuxt-module-plausible/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const plugins = [

// ///////////////////////////////////////////////////////////// registerPlugins
const registerPlugins = () => {
plugins.forEach((plugin) => {
addPlugin(plugin)
console.log('🔌 [nuxt-module-plausible:plugin]')
})
plugins.forEach((plugin) => {
addPlugin(plugin)
console.log('🔌 [nuxt-module-plausible:plugin]')
})
}

// /////////////////////////////////////////////////////////////////////// Setup
Expand Down
43 changes: 24 additions & 19 deletions modules/nuxt-module-plausible/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,42 @@ import { defineNuxtPlugin } from '#imports'

// ////////////////////////////////////////////////////////////////////// Export
// -----------------------------------------------------------------------------
export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(() => {
if (process.server) { return } // only run client-side

// Do not fire Plausible if not in production mode
// unless tracking localhost is explicitly enabled in the config
if (process.env.NODE_ENV !== 'production') {
if (!nuxtApp.$config.public.plausible.trackLocalhost) {
return
}
const router = useRouter()
const config = useRuntimeConfig().public

// Only run in production OR if trackLocalhost is explcitly set to true
if (process.env.NODE_ENV !== 'production' || !config.trackLocalhost === true) {
return
}

const router = nuxtApp.$router
const config = nuxtApp.$config.public.siteUrl
console.log('PASS')

let isInitialPageLoad = true

router.afterEach((to) => {
router.afterEach(to => {

console.log(to)

// Ignore initial page because it's fired in the head
if (isInitialPageLoad) {
isInitialPageLoad = false
return
}

// Check if we're on client-side
if (process.client) {
// Track virtual navigation changes
window.plausible = window.plausible || function() {
(window.plausible.q = window.plausible.q || []).push(arguments)
}
window.plausible('pageview', {
url: `${config.public.siteUrl}${to.fullPath}`
})
// Track virtual navigation changes
window.plausible = window.plausible || function () {
(window.plausible.q = window.plausible.q || []).push(arguments)
}

console.log({
url: `${config.siteUrl}${to.fullPath}`
})

window.plausible('pageview', {
url: `${config.siteUrl}${to.fullPath}`
})
})
})
2 changes: 1 addition & 1 deletion nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default defineNuxtConfig({
plausible: {
include: true,
domain: 'singularity.storage',
trackLocalhost: true,
trackLocalhost: false,
autoOutboundTracking: true
},
// //////////////////////////////////////////////////// [Module] @nuxt/content
Expand Down

0 comments on commit 28b8d9c

Please sign in to comment.