2
2
3
3
import { useRef } from 'react'
4
4
import { useServerInsertedHTML } from 'next/navigation'
5
+ import type { TrackerAction } from '~/constants/tracker'
5
6
6
- import { apiClient } from '~/lib/request'
7
+ declare global {
8
+ interface Window {
9
+ umami ?: {
10
+ track : ( event : string , data ?: any ) => void
11
+ }
12
+ }
13
+ }
7
14
8
15
export const Analyze = ( ) => {
9
16
const onceRef = useRef ( false )
@@ -13,33 +20,39 @@ export const Analyze = () => {
13
20
}
14
21
15
22
onceRef . current = true
16
- const apiBase = apiClient . proxy . fn . utils . analyze . toString ( true )
23
+ // const apiBase = apiClient.proxy.fn.utils.analyze.toString(true)
17
24
18
25
return (
19
26
< script
20
27
dangerouslySetInnerHTML = { {
21
28
__html :
22
- `var apiBase = "${ apiBase } ";
23
- ${ function run ( ) {
24
- document . addEventListener ( 'click' , async ( e ) => {
25
- const $ = e . target as HTMLElement
26
- const event = $ . dataset . event
27
-
28
- if ( event ) {
29
- await fetch ( apiBase , {
30
- method : 'POST' ,
31
- headers : {
32
- 'Content-Type' : 'application/json' ,
29
+ `${ function run ( ) {
30
+ document . addEventListener (
31
+ 'click' ,
32
+ async ( e ) => {
33
+ const $ = e . target as HTMLElement
34
+ const event = $ . dataset . event
35
+
36
+ if ( event ) {
37
+ window . umami ?. track ( event , {
38
+ type : 'click' ,
39
+ } )
40
+ }
33
41
} ,
34
- body : JSON . stringify ( {
35
- key : 'mx' ,
36
- event,
37
- type : 'inc' ,
38
- } ) ,
42
+ true ,
43
+ )
44
+
45
+ document . addEventListener ( 'impression' , async ( e : any ) => {
46
+ const detail = e . detail as {
47
+ action : TrackerAction
48
+ label : string
49
+ }
50
+
51
+ window . umami ?. track ( detail . label , {
52
+ type : 'impression' ,
53
+ } )
39
54
} )
40
- }
41
- } )
42
- } . toString ( ) } \n` + `run();` ,
55
+ } . toString ( ) } \n` + `run();` ,
43
56
} }
44
57
/>
45
58
)
0 commit comments