File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed
packages/common/src/utils Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -146,6 +146,23 @@ const isAllowedEvent = (eventName, secondArg) => {
146146 }
147147} ;
148148
149+ // After 30min no event we mark a session
150+ const NEW_SESSION_TIME = 1000 * 60 * 60 * 30 ;
151+
152+ const getLastTimeEventSent = ( ) => {
153+ const lastTime = localStorage . getItem ( 'csb-last-event-sent' ) ;
154+
155+ if ( lastTime === null ) {
156+ return 0 ;
157+ }
158+
159+ return + lastTime ;
160+ } ;
161+
162+ const markLastTimeEventSent = ( ) => {
163+ localStorage . setItem ( 'csb-last-event-sent' , Date . now ( ) . toString ( ) ) ;
164+ } ;
165+
149166export default function track ( eventName , secondArg : Object = { } ) {
150167 try {
151168 if ( ! DNT && isAllowedEvent ( eventName , secondArg ) ) {
@@ -163,6 +180,13 @@ export default function track(eventName, secondArg: Object = {}) {
163180 }
164181 try {
165182 if ( typeof global . amplitude !== 'undefined' ) {
183+ const currentTime = Date . now ( ) ;
184+ if ( currentTime - getLastTimeEventSent ( ) > NEW_SESSION_TIME ) {
185+ // We send a separate New Session event if people have been inactive for a while
186+ global . amplitude . logEvent ( 'New Session' ) ;
187+ }
188+ markLastTimeEventSent ( ) ;
189+
166190 debug ( '[Amplitude] Tracking' , eventName , data ) ;
167191 global . amplitude . logEvent ( eventName , data ) ;
168192 }
You can’t perform that action at this time.
0 commit comments