react-activity-tracker
Version:
A library to track user interactions and API activity in React apps
31 lines (26 loc) • 658 B
JavaScript
let events = [];
// Log events
const logEvent = (type, data) => {
const timestamp = new Date().toISOString();
events.push({ type, timestamp, ...data });
};
// Get all stored events
const getEvents = () => {
return events;
};
// Send logs to a server
const sendToServer = (url) => {
if (url && events.length > 0) {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(events),
}).then(() => {
events = []; // Clear logs after sending
});
}
};
// Expose the public methods
export { logEvent, getEvents, sendToServer };