1
1
'use client' ;
2
2
3
+ import { useAppDispatch , useTypedSelector } from '@/lib/hooks/store' ;
4
+ import { setPwaDismissed } from '@/lib/store/userSlice' ;
5
+ import logEvent from '@/lib/utils/logEvent' ;
3
6
import Cookies from 'js-cookie' ;
4
7
import { useEffect , useMemo , useState } from 'react' ;
5
- import { setPwaDismissed } from '@/lib/store/userSlice' ;
6
- import { useAppDispatch , useTypedSelector } from '@/lib/hooks/store' ;
8
+ import { PWA_DISMISSED , PWA_INSTALLED } from '../constants/events' ;
7
9
8
10
type UserChoice = Promise < {
9
11
outcome : 'accepted' | 'dismissed' ;
@@ -17,7 +19,7 @@ interface BeforeInstallPromptEvent extends Event {
17
19
prompt ( ) : Promise < void > ;
18
20
}
19
21
20
- const PWA_DISMISSED = 'pwaBannerDismissed' ;
22
+ const PWA_BANNER_DISMISSED = 'pwaBannerDismissed' ;
21
23
22
24
export default function usePWA ( ) {
23
25
const [ bannerState , setBannerState ] = useState < PwaBannerState > ( 'Generic' ) ;
@@ -31,9 +33,35 @@ export default function usePWA() {
31
33
[ ] ,
32
34
) ;
33
35
36
+ const getPwaMetaData = useMemo ( ( ) => {
37
+ const userAgent = window . navigator . userAgent ;
38
+ const platform = userAgent . includes ( 'Win' )
39
+ ? 'Windows'
40
+ : userAgent . includes ( 'Mac' )
41
+ ? 'MacOS'
42
+ : userAgent . includes ( 'Linux' )
43
+ ? 'Linux'
44
+ : 'Unknown OS' ;
45
+
46
+ const browser = userAgent . includes ( 'Chrome' )
47
+ ? 'Chrome'
48
+ : userAgent . includes ( 'Firefox' )
49
+ ? 'Firefox'
50
+ : userAgent . includes ( 'Safari' )
51
+ ? 'Safari'
52
+ : userAgent . includes ( 'Edge' )
53
+ ? 'Edge'
54
+ : userAgent . includes ( 'Edge' )
55
+ ? 'Opera'
56
+ : 'Unknown Browser' ;
57
+
58
+ return { browser, platform } ;
59
+ } , [ ] ) ;
60
+
34
61
const declineInstallation = async ( ) => {
35
62
if ( userCookiesAccepted ) {
36
- Cookies . set ( PWA_DISMISSED , 'true' ) ;
63
+ Cookies . set ( PWA_BANNER_DISMISSED , 'true' ) ;
64
+ logEvent ( PWA_DISMISSED , getPwaMetaData ) ;
37
65
}
38
66
setBannerState ( 'Hidden' ) ;
39
67
await dispatch ( setPwaDismissed ( true ) ) ;
@@ -50,12 +78,15 @@ export default function usePWA() {
50
78
* immediately after installation. In some cases, isStandalone might
51
79
* still return false momentarily, causing the banner to reappear.
52
80
*/
81
+ if ( userCookiesAccepted ) {
82
+ logEvent ( PWA_INSTALLED , getPwaMetaData ) ;
83
+ }
53
84
window . beforeinstallpromptEvent = undefined ;
54
85
setBannerState ( 'Hidden' ) ;
55
86
} ;
56
87
57
88
useEffect ( ( ) => {
58
- const pwaBannerDismissedCookie = Boolean ( Cookies . get ( PWA_DISMISSED ) ) ;
89
+ const pwaBannerDismissedCookie = Boolean ( Cookies . get ( PWA_BANNER_DISMISSED ) ) ;
59
90
const isStandalone =
60
91
typeof window !== 'undefined' && window . matchMedia ( '(display-mode: standalone)' ) . matches ;
61
92
const isHidden =
0 commit comments