@castlabs/ui 7.12.0 → 7.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@castlabs/ui",
3
- "version": "7.12.0",
3
+ "version": "7.13.0",
4
4
  "repository": "https://github.com/castlabs/ui-styleguide",
5
5
  "private": false,
6
6
  "description": "A vanilla HTML/CS/JS & Vue.js component library for Castlabs.",
@@ -0,0 +1,5 @@
1
+ # Matomo Integration
2
+
3
+ This standalone plugin is based on [vue-matomo](https://github.com/AmazingDreams/vue-matomo) (MIT license).
4
+
5
+ It is not used by @castlabs/ui itself, but provided for Vue child projects to use.
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Provide matomo integration without dependency to external package.
3
+ */
4
+
5
+ import VueMatomo from 'vue-matomo'
6
+ import type { App } from 'vue'
7
+ import type { Router, RouteLocationNormalized } from 'vue-router'
8
+
9
+ export function setCookie (name: string, value: string, days = 365, path = '/') {
10
+ document.cookie = `${name}=${value}; max-age=${60 * 60 * 24 * days}; path=${path}; SameSite=Lax;`
11
+ }
12
+
13
+ export function getCookie (cname: string): string {
14
+ for (const cookie of document?.cookie?.split(/; */) ?? []) {
15
+ const [name, value] = cookie.split('=')
16
+ if (name === cname) return value ?? ''
17
+ }
18
+ return ''
19
+ }
20
+
21
+ function getPaq () {
22
+ // cast potentialy non-exsisting _paq for typescript
23
+ return (window as unknown as { _paq: (string | number)[][] })?._paq ?? []
24
+ }
25
+
26
+ export function hasCookieConsent (what: string): boolean {
27
+ const value = getCookie('cl_banner_nok')
28
+ for (const token of value.split('|')) {
29
+ if (token === `+${what}`) return true
30
+ }
31
+ return false
32
+ }
33
+
34
+ export function enableMatomo (record = false, env = 'PROD') {
35
+ // Enable detailed matomo tracking if user gave consent.
36
+ const _paq = getPaq()
37
+ if (hasCookieConsent('mtm') && getCookie('mtm_cookie_consent') === '') {
38
+ // make Matomo create cookie 'mtm_cookie_consent' to remember consent. ~13 months=9480 hours.
39
+ _paq.push(['rememberCookieConsentGiven', 9480])
40
+ }
41
+ if (record) trackPage(document?.title, window?.location?.pathname, env)
42
+ }
43
+
44
+ export function trackPage (title: string, path: string, env: string) {
45
+ // Instruct Matomo to do the actual page tracking.
46
+
47
+ let prettyPath = path
48
+ const urnRegexp = /urn:[a-zA-Z]+:[a-zA-Z]+:[a-fA-F0-9]{32}/g
49
+ const foundUrns = [...prettyPath.matchAll(urnRegexp)]
50
+ foundUrns.forEach(elem => {
51
+ prettyPath = prettyPath.replace(elem.toString(), (elem[0].split(':')[2] ?? '').toUpperCase())
52
+ })
53
+
54
+ const prettyTitle = `${window?.location?.hostname} / ${title.replace(/ *\|[^|]*$/, '')}${env === 'PROD' ? '' : env}`
55
+
56
+ const _paq = getPaq()
57
+ _paq.push(['setDocumentTitle', prettyTitle])
58
+ _paq.push(['setCustomUrl', prettyPath])
59
+ _paq.push(['trackPageView'])
60
+ _paq.push(['enableLinkTracking'])
61
+ }
62
+
63
+ function trackViaMatomo (to: RouteLocationNormalized, env: string) {
64
+ if (!to.meta.analyticsIgnore) {
65
+ // matomo settings
66
+ enableMatomo()
67
+ trackPage(to.meta.title as string, to.fullPath, env)
68
+ }
69
+ }
70
+
71
+ export default {
72
+ install: (app: App, router: Router, env = 'PROD', siteId = 1) => {
73
+ console.log('setup matomo', env, siteId)
74
+ app.use(VueMatomo, {
75
+ host: 'https://castlabs.matomo.cloud',
76
+ siteId: siteId,
77
+ enableLinkTracking: false,
78
+ requireConsent: false,
79
+ trackInitialView: false,
80
+ disableCookies: false,
81
+ requireCookieConsent: true,
82
+ enableHeartBeatTimer: true,
83
+ heartBeatTimerInterval: 15,
84
+ trackerFileName: 'matomo',
85
+ cookieDomain: '*.castlabs.com',
86
+ domains: ['*.castlabs.com']
87
+ })
88
+
89
+ router.afterEach((to: RouteLocationNormalized) => {
90
+ trackViaMatomo(to, env)
91
+ })
92
+ }
93
+ }
@@ -0,0 +1 @@
1
+ declare module 'vue-matomo'
@@ -1,3 +1,3 @@
1
1
  :root {
2
- #{'--cl-version'}: 'v7.12.0';
2
+ #{'--cl-version'}: 'v7.13.0';
3
3
  }