-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
154 lines (141 loc) · 3.75 KB
/
index.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as MatomoTracker from 'matomo-tracker'
import * as Cookies from 'js-cookie'
import nanoid = require('nanoid')
import Analytics from 'analytics'
const matomo = new MatomoTracker(1, 'https://matomo.wepublish.dev/matomo.php')
let analytics
let pageInterval: NodeJS.Timer | number
let pageTimeout: NodeJS.Timeout | number
const cookieName: string = 'wepublish-matomo'
/**
* Define analytics plugin
*/
const matomoPlugin = () => {
return {
name: "wp-matomo-analytics",
page: () => {
if (pageInterval) {
clearInterval(pageInterval)
}
if (pageTimeout) {
clearTimeout(pageTimeout)
}
pageInterval = setInterval(findElementAndTrack, 250)
pageTimeout = setTimeout(() => {
clearInterval(pageInterval)
}, 2500)
}
}
}
/**
* Searches for a html element with id 'peer-element'. If found, the method inits the tracking.
*/
function findElementAndTrack () {
const peerElement =
document.querySelector<HTMLElement>("#peer-element")
if (!peerElement) {
return
}
clearInterval(pageInterval)
track(peerElement)
}
/**
* Sends the tracking request to the matomo instance. Responsible to build the payload, which is sent along.
* @param peerElement
*/
function track (peerElement: HTMLElement) {
const peerName = peerElement.dataset.peerName
const peerArticleId = peerElement.dataset.peerArticleId
const publisherName = peerElement.dataset.publisherName
if (!peerElement || !peerArticleId || !publisherName) {
return
}
const apiv = 1
const h = new Date().getHours()
const m = new Date().getMinutes()
const s = new Date().getSeconds()
let action_name
let url
let urlref
let _id
let cookie
let res
try {
action_name = document.title
url = window.location.href
urlref = document.referrer
_id = getUniqueVisitorId()
cookie = canSetCookie()
res = `${screen.width}x${screen.height}`
} catch (e) {
console.log(e)
}
matomo.track({
url,
urlref,
_id,
apiv,
action_name,
h,
m,
s,
send_image: 0,
cookie,
res,
'dimension1': peerArticleId,
'dimension2': peerName,
'dimension3': publisherName
})
}
/**
* Tries to retrieve the unique client id from the cookies.
* If no such id is available as cookie, the function tries to generate a new id.
* If that new id cannot be stored as cookie, undefined is returned. In that case Matomo fallback gets a turn.
*/
function getUniqueVisitorId (): string | undefined {
let trackerCookie = Cookies.get(cookieName)
// if cookie doesn't exist yet, create one
if (!trackerCookie) {
createUniqueVisitorCookie()
trackerCookie = Cookies.get(cookieName)
if (!trackerCookie) {
return undefined
}
}
const trackerCookieObject = JSON.parse(trackerCookie)
return trackerCookieObject.uniqueVisitorId
}
/**
* Generates a unique client id with nanoid and stores it as cookie.
*/
function createUniqueVisitorCookie (): void {
const uniqueVisitorId = nanoid(30)
Cookies.set(cookieName, JSON.stringify({uniqueVisitorId}))
}
/**
* Test function to check, whether cookies can be set or not.
*/
function canSetCookie (): 1 | 0 {
const testCookieName = 'wep-temp-test-cookie'
const testCookieValue = 'wep-temp-test-cookie-content'
Cookies.set(testCookieName, testCookieValue)
const testCookie = Cookies.get(testCookieName)
Cookies.remove(testCookieName)
return testCookie === testCookieValue ? 1 : 0
}
/**
* Public interface. Exported members
*/
export function initWepublishAnalytics({appName}: {appName: string}) {
if (!appName) {
throw new Error('No app name given!')
}
analytics = Analytics({
app: appName,
plugins: [matomoPlugin()]
})
}
export function trackPage() {
// call peer view
analytics.page(matomoPlugin())
}