@brillout/docpress 0.16.24 → 0.16.25

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.
@@ -28,6 +28,7 @@ type Config = {
28
28
  indexName: string;
29
29
  };
30
30
  googleAnalytics?: string;
31
+ umamiId?: string;
31
32
  i18n?: true;
32
33
  pressKit?: true;
33
34
  docsDir?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.16.24",
3
+ "version": "0.16.25",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@brillout/picocolors": "^1.0.10",
@@ -52,6 +52,7 @@ async function onRenderClient(pageContext: PageContextClient) {
52
52
  installSectionUrlHashs()
53
53
  setHydrationIsFinished()
54
54
  initGoogleAnalytics(pageContext)
55
+ initUmami(pageContext)
55
56
 
56
57
  globalObject.isNotFirstRender = true
57
58
  }
@@ -104,3 +105,46 @@ declare global {
104
105
  gtag: (...args: any[]) => void
105
106
  }
106
107
  }
108
+
109
+ async function initUmami(pageContext: PageContextClient) {
110
+ // Only add script tag on first render
111
+ if (globalObject.isNotFirstRender) {
112
+ return
113
+ }
114
+
115
+ // Simple way to plug in umami
116
+ const umamiId = pageContext.config.docpress.umamiId;
117
+ if (!umamiId) {
118
+ return
119
+ }
120
+
121
+ try {
122
+ await addScript('https://cloud.umami.is/script.js', {
123
+ 'data-website-id': umamiId,
124
+ })
125
+ } catch {
126
+ // Umami analytics unavailable
127
+ }
128
+ }
129
+
130
+ /** https://umami.is/docs/tracker-functions */
131
+ type Umami = {
132
+ track: {
133
+ (): void
134
+ (eventName: string): void
135
+ (data: object): void
136
+ (eventName: string, data: object): void
137
+ }
138
+ identify: {
139
+ (uniqueId: string): void
140
+ (data: object): string
141
+ (uniqueId: string, data: object): void
142
+ }
143
+ }
144
+
145
+ declare global {
146
+ interface Window {
147
+ /** Script load is not guaranteed */
148
+ umami?: Umami;
149
+ }
150
+ }
package/types/Config.ts CHANGED
@@ -35,6 +35,7 @@ type Config = {
35
35
  indexName: string
36
36
  }
37
37
  googleAnalytics?: string
38
+ umamiId?: string
38
39
 
39
40
  i18n?: true
40
41
  pressKit?: true
@@ -2,12 +2,17 @@ export { addScript }
2
2
 
3
3
  import { genPromise } from './genPromise.js'
4
4
 
5
- async function addScript(src: string) {
5
+ async function addScript(src: string, attributes?: Record<string, string>) {
6
6
  const { promise, resolve, reject } = genPromise()
7
7
  const script = document.createElement('script')
8
8
  script.onload = () => resolve()
9
9
  script.onerror = () => reject(new Error(`Failed to load script: ${src}`))
10
10
  script.src = src
11
+ if (attributes) {
12
+ for (const [name, value] of Object.entries(attributes)) {
13
+ script.setAttribute(name, value)
14
+ }
15
+ }
11
16
  document.head.appendChild(script)
12
17
  return promise
13
18
  }