Skip to content

Commit

Permalink
feat: 유저 이탈시 백엔드에 로그 전송 로직 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
JjungminLee committed Feb 14, 2024
1 parent c9eea88 commit d391aa9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SetLocalStorage } from './SetLocalStorage';
import { SetLocalStorage, SetLocalStorageClear } from './SetLocalStorage';
import { postLog } from './apis/postLog';
import { LogPayloadParams, ServiceNameType } from './types/LogType';
import CryptoJS from 'crypto-js';

Expand Down Expand Up @@ -89,3 +90,13 @@ export const Logger = ({ userId, serviceName, name, message, path, tags }: LogPa
},
};
};

window.addEventListener('beforeunload', async (event) => {
// 명세에 따라 preventDefault는 호출해야하며, 기본 동작을 방지합니다.
event.preventDefault();
console.log('이탈감지');
const logList: any[] = JSON.parse(localStorage.getItem('yls-web') as string) || [];
console.log(logList);
// const res = await postLog();
SetLocalStorageClear();
});
4 changes: 2 additions & 2 deletions src/SetLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { postLog } from './apis/postLog';
import { LogType } from './types/LogType';

const SetLocalStorageClear = () => {
export const SetLocalStorageClear = () => {
const list: any[] = [];
localStorage.setItem('yls-web', JSON.stringify(list));
};
Expand All @@ -17,8 +17,8 @@ export const SetLocalStorage = async (logger: LogType) => {
const updateList = [...remainList, logger];
localStorage.setItem('yls-web', JSON.stringify(updateList));
} else {
//const res = await postLog();
SetLocalStorageClear();
const res = await postLog();
}
}
};

0 comments on commit d391aa9

Please sign in to comment.