11import AppStorage from '../services/localStorage' ;
22import { LS_ANALYTICS_ID_KEY } from '../Constants' ;
3+ import { init , track , Identify , identify } from '@amplitude/analytics-browser'
4+ import { ANALYTICS_SDK_KEY , ANALYTICS_ENDPOINT } from '../Constants'
35
6+ const initAnalytics = ( ) => {
7+ init ( ANALYTICS_SDK_KEY , getRandomUserId ( ) , {
8+ disableCookies : true ,
9+ serverUrl : ANALYTICS_ENDPOINT ,
10+ } )
11+ }
412const trackPageScroll = ( direction ) => {
513 trackEvent ( 'Pages' , 'Scroll' , direction )
614}
@@ -41,6 +49,29 @@ const trackAddCard = (card) => {
4149 trackEvent ( 'Card' , 'Add' , card )
4250}
4351
52+ const identifyUserLanguages = ( languages ) => {
53+ const identity = new Identify ( )
54+ identity . set ( 'Languages' , languages )
55+ identify ( identity )
56+ }
57+
58+ const identifyListingMode = ( listingMode ) => {
59+ const identity = new Identify ( )
60+ identity . set ( 'ListingMode' , listingMode )
61+ identify ( identity )
62+ }
63+ const identifyUserCards = ( cards ) => {
64+ const identity = new Identify ( )
65+ identity . set ( 'Sources' , cards )
66+ identify ( identity )
67+ }
68+
69+ const identifyUserTheme = ( theme ) => {
70+ const identity = new Identify ( )
71+ identity . set ( 'Theme' , theme )
72+ identify ( identity )
73+ }
74+
4475const trackRemoveCard = ( card ) => {
4576 trackEvent ( 'Card' , 'Remove' , card )
4677}
@@ -124,10 +155,9 @@ const trackEvent = (category, action, label) => {
124155 payload . append ( 'el' , label . capitalize ( ) )
125156 }
126157
127- try {
128- var manifestData = chrome . runtime . getManifest ( )
129- payload . append ( 'cd1' , manifestData . version )
130- } catch ( e ) { }
158+ payload . append ( 'cd1' , getAppVersion ( ) )
159+
160+ track ( `${ category . toLowerCase ( ) } _${ action . toLowerCase ( ) } ` )
131161
132162 if ( process . env . NODE_ENV !== 'production' ) {
133163 console . log ( 'Analytics debug payload' , payload . toString ( ) )
@@ -137,28 +167,45 @@ const trackEvent = (category, action, label) => {
137167 navigator . sendBeacon ( 'https://www.google-analytics.com/collect' , payload . toString ( ) )
138168}
139169
170+ const getAppVersion = ( ) => {
171+ try {
172+ var manifestData = chrome . runtime . getManifest ( )
173+ return manifestData . version
174+ } catch ( e ) {
175+ return '0.0'
176+ }
177+ }
178+ /**
179+ * Generates a random user id
180+ */
140181const getRandomUserId = ( ) => {
141182 let userId = AppStorage . getItem ( LS_ANALYTICS_ID_KEY )
142183 if ( ! userId ) {
143- let newUserId = `${ new Date ( ) . getTime ( ) } ${ Math . random ( ) } ` // Random User Id
184+ let newUserId = `${ new Date ( ) . getTime ( ) } ${ Math . random ( ) } `
144185 AppStorage . setItem ( LS_ANALYTICS_ID_KEY , newUserId )
145186 userId = newUserId
146187 }
147188 return userId
148189}
190+
149191Object . assign ( String . prototype , {
150192 capitalize ( ) {
151193 return this . charAt ( 0 ) . toUpperCase ( ) + this . slice ( 1 )
152194 } ,
153195} )
154196
155197export {
198+ initAnalytics ,
156199 trackPageView ,
157200 trackThemeChange ,
158201 trackOpenLinkFrom ,
159202 trackAddLanguage ,
160203 trackRemoveLanguage ,
161204 trackAddCard ,
205+ identifyUserLanguages ,
206+ identifyUserCards ,
207+ identifyListingMode ,
208+ identifyUserTheme ,
162209 trackRemoveCard ,
163210 trackOpenLinksNewTab ,
164211 trackBookmarkFrom ,
0 commit comments