-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics-overrides.ts
More file actions
40 lines (33 loc) · 1.16 KB
/
analytics-overrides.ts
File metadata and controls
40 lines (33 loc) · 1.16 KB
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
function queryLast<T extends Element>(selector: string): T | undefined {
const list = document.querySelectorAll<T>(selector)
if (list.length > 0) {
return list[list.length - 1]
}
}
function pagePathname() {
const locationOverride = queryLast<HTMLMetaElement>('meta[name=analytics-location]')
if (locationOverride) {
return locationOverride.content
} else {
return window.location.pathname
}
}
function pageQuery() {
const stripParams = queryLast<HTMLMetaElement>('meta[name=analytics-location-query-strip]')
let search = ''
if (!stripParams) {
search = window.location.search
}
const extraParams = queryLast<HTMLMetaElement>('meta[name=analytics-location-params]')
if (extraParams) {
search += (search ? '&' : '?') + extraParams.content
}
for (const meta of document.querySelectorAll<HTMLMetaElement>('meta[name=analytics-param-rename]')) {
const names = meta.content.split(':', 2)
search = search.replace(new RegExp(`(^|[?&])${names[0]}($|=)`, 'g'), `$1${names[1]}$2`)
}
return search
}
export function requestUri() {
return `${window.location.protocol}//${window.location.host}${pagePathname() + pageQuery()}`
}