Skip to content

Commit 5877fe6

Browse files
committed
Send separate new session event
1 parent 492ab2f commit 5877fe6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

packages/common/src/utils/analytics.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
149166
export 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
}

0 commit comments

Comments
 (0)