-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add composable and remove vue-matomo
- Loading branch information
Showing
6 changed files
with
127 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare global { | ||
interface Window { | ||
_paq: unknown[][] | ||
} | ||
} | ||
|
||
export {} |