-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
45 lines (38 loc) · 1.57 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
*
* 🔌 [Plugin | NuxtModuleMatomo] Methods
*
*/
console.log(`🔌 [Plugin | Matomo] Matomo`)
// ///////////////////////////////////////////////////////////////////// Imports
// -----------------------------------------------------------------------------
import Config from '@/nuxt.config'
// ////////////////////////////////////////////////////////////////////// Export
// -----------------------------------------------------------------------------
export default ({ app }) => {
const MATOMO_URL = Config.matomo.src
const routing = Config.matomo.routing
const targetType = Config.matomo.target
app.router.afterEach((to, from) => {
if (typeof document !== 'undefined' && targetType === 'tag-manager') {
let script = document.getElementById('matomo_injected_script')
// if matomo script does not already exist,
// create it and intialize tracking
if (!script) {
script = document.createElement('script')
script.setAttribute('id', 'matomo_injected_script')
script.innerHTML = `
var _mtm = window._mtm = window._mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='${MATOMO_URL}'; s.parentNode.insertBefore(g,s);
`
document.head.appendChild(script)
} else if (script && routing) {
// if script does exist,
// push page view event on router change
_mtm.push({'event': 'mtm.PageView'});
}
}
})
}