Skip to content

Commit

Permalink
feat: add composable and remove vue-matomo
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdmlln committed Jan 10, 2024
1 parent 0131ac6 commit ad5a125
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 62 deletions.
29 changes: 11 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
},
"dependencies": {
"@nuxt/kit": "^3.8.2",
"defu": "^6.1.3",
"vue-matomo": "^4.2.0"
"defu": "^6.1.3"
},
"peerDependencies": {
"@remindgmbh/nuxt-typo3": "^4.0.0 || develop",
"@remindgmbh/nuxt-typo3-solr": "^2.0.0 || develop"
"@remindgmbh/nuxt-typo3": "^4.0.0 || ^4.0.0-develop",
"@remindgmbh/nuxt-typo3-solr": "^2.0.0 || ^2.0.0-develop"
},
"peerDependenciesMeta": {
"@remindgmbh/nuxt-typo3-solr": {
Expand All @@ -50,8 +49,8 @@
"@nuxt/module-builder": "^0.5.4",
"@nuxt/schema": "^3.8.2",
"@remindgmbh/eslint-config-nuxt": "^1.0.5",
"@remindgmbh/nuxt-typo3": "develop",
"@remindgmbh/nuxt-typo3-solr": "develop",
"@remindgmbh/nuxt-typo3": "^4.0.0-develop",
"@remindgmbh/nuxt-typo3-solr": "^2.0.0-develop",
"@types/node": "^20.9.3",
"eslint": "^8.54.0",
"nuxt": "^3.8.2",
Expand Down
15 changes: 4 additions & 11 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
import { defu } from 'defu'
import { name, version } from '../package.json'
import type { ViteConfig } from '@nuxt/schema'

export const CONFIG_KEY = 'typo3Matomo'

export interface ModuleOptions {
domains: string
domains: string[]
matomoUrl: string
siteId: number
}
Expand All @@ -18,26 +17,20 @@ export default defineNuxtModule<ModuleOptions>({
configKey: CONFIG_KEY,
},
defaults: {
domains: '',
domains: [],
matomoUrl: '',
siteId: 0,
},
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)

nuxt.options.alias['#nuxt-typo3-matomo'] = resolver.resolve('runtime')

nuxt.options.runtimeConfig.public[CONFIG_KEY] = defu(
nuxt.options.runtimeConfig.public[CONFIG_KEY],
options,
)

const viteConfig: ViteConfig = {
optimizeDeps: {
include: ['vue-matomo'],
},
}

nuxt.options.vite = defu(viteConfig, nuxt.options.vite)

addPlugin({
src: resolver.resolve('./runtime/plugins/matomo'),
mode: 'client',
Expand Down
53 changes: 53 additions & 0 deletions src/runtime/composables/useMatomo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export function useMatomo() {
function enableLinkTracking() {
window._paq.push(['enableLinkTracking'])
}

function setCustomUrl(url: string) {
window._paq.push(['setCustomUrl', url])
}

function setDomains(domains: string[]) {
window._paq.push(['setDomains', domains])
}

function setReferrerUrl(url: string) {
window._paq.push(['setReferrerUrl', url])
}

function setSiteId(id: number) {
window._paq.push(['setSiteId', id])
}

function setTrackerUrl(url: string) {
window._paq.push(['setTrackerUrl', url])
}

function trackPageView(title?: string) {
window._paq.push(['trackPageView', title])
}

function trackSiteSearch(
keyword: string,
category?: string,
numberOfResults?: number,
) {
window._paq.push([
'trackSiteSearch',
keyword,
category ?? false,
numberOfResults ?? false,
])
}

return {
enableLinkTracking,
setCustomUrl,
setDomains,
setReferrerUrl,
setSiteId,
setTrackerUrl,
trackPageView,
trackSiteSearch,
}
}
74 changes: 47 additions & 27 deletions src/runtime/plugins/matomo.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
import {
defineNuxtPlugin,
useHead,
useLogger,
useRuntimeConfig,
useRouter,
useT3Data,
useT3DataUtil,
useRuntimeConfig,
type T3SolrModel,
} from '#imports'
// @ts-ignore
import VueMatomo from 'vue-matomo'
import { useMatomo } from '../composables/useMatomo'

export default defineNuxtPlugin((nuxtApp) => {
export default defineNuxtPlugin(() => {
const config = useRuntimeConfig().public.typo3Matomo

const logger = useLogger()

if (!config.matomoUrl || config.siteId === 0) {
logger.warn('Matomo disabled: matomoUrl or siteId not configured')
return
}

nuxtApp.vueApp.use(VueMatomo, {
host: config.matomoUrl,
siteId: config.siteId,
// No type for $router, see https://github.com/nuxt/nuxt/issues/12737#issuecomment-1397241218
router: nuxtApp.$router,
domains: config.domains,
trackSiteSearch: () => {
const { currentPageData } = useT3Data()
if (currentPageData.value) {
const { findContentElementByType } = useT3DataUtil()
const searchResults =
findContentElementByType<T3SolrModel.Typo3.SolrPiResults>(
'solr_pi_results',
currentPageData.value,
)
if (searchResults) {
return {
keyword: searchResults.content.data.query ?? '',
resultsCount: searchResults.content.data.count,
}
}
window._paq = window._paq || []

const matomo = useMatomo()

const router = useRouter()

matomo.setTrackerUrl(new URL('matomo.php', config.matomoUrl).href)
matomo.setSiteId(config.siteId)
matomo.setDomains(config.domains)

useHead({
script: [
{
src: new URL('matomo.js', config.matomoUrl).href,
async: true,
type: 'text/javascript',
},
],
})

router.afterEach((to, from) => {
matomo.setReferrerUrl(window.location.origin + from.fullPath)
matomo.setCustomUrl(window.location.origin + to.fullPath)

const { currentPageData } = useT3Data()
if (currentPageData.value) {
const { findContentElementByType } = useT3DataUtil()
const searchResults =
findContentElementByType<T3SolrModel.Typo3.SolrPiResults>(
'solr_pi_results',
currentPageData.value,
)?.content.data
if (searchResults) {
matomo.trackSiteSearch(
searchResults.query ?? '',
undefined,
searchResults.count,
)
return
}
}

return false
},
matomo.trackPageView(to.fullPath)
})
})
7 changes: 7 additions & 0 deletions src/runtime/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare global {
interface Window {
_paq: unknown[][]
}
}

export {}

0 comments on commit ad5a125

Please sign in to comment.