fix: Corrigindo deduplicação de eventos no Rudderstack

This commit is contained in:
Lucas Santana 2025-01-17 12:51:36 -03:00
parent bcbdd07a41
commit 6a1a471ce5
2 changed files with 93 additions and 70 deletions

View File

@ -1,4 +1,4 @@
import { useEffect } from 'react'; import { useEffect, useRef } from 'react';
import { useRudderstack } from '../../hooks/useRudderstack'; import { useRudderstack } from '../../hooks/useRudderstack';
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import { useAuth } from '../../hooks/useAuth'; import { useAuth } from '../../hooks/useAuth';
@ -7,8 +7,22 @@ export function PageTracker() {
const location = useLocation(); const location = useLocation();
const { page } = useRudderstack(); const { page } = useRudderstack();
const { user } = useAuth(); const { user } = useAuth();
const lastPageTracked = useRef<string | null>(null);
const timeoutRef = useRef<NodeJS.Timeout>();
useEffect(() => { useEffect(() => {
// Se já rastreamos esta página, não rastrear novamente
if (lastPageTracked.current === location.pathname) {
return;
}
// Limpa o timeout anterior se existir
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
// Debounce de 300ms para evitar múltiplos eventos
timeoutRef.current = setTimeout(() => {
// Coleta informações do dispositivo/navegador // Coleta informações do dispositivo/navegador
const deviceInfo = { const deviceInfo = {
screenWidth: window.screen.width, screenWidth: window.screen.width,
@ -73,6 +87,9 @@ export function PageTracker() {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
}); });
// Atualiza a última página rastreada
lastPageTracked.current = location.pathname;
// Atualiza informações da sessão // Atualiza informações da sessão
sessionStorage.setItem('lastVisitedPage', location.pathname); sessionStorage.setItem('lastVisitedPage', location.pathname);
if (!localStorage.getItem('returningVisitor')) { if (!localStorage.getItem('returningVisitor')) {
@ -81,7 +98,15 @@ export function PageTracker() {
if (!sessionStorage.getItem('sessionStartTime')) { if (!sessionStorage.getItem('sessionStartTime')) {
sessionStorage.setItem('sessionStartTime', new Date().toISOString()); sessionStorage.setItem('sessionStartTime', new Date().toISOString());
} }
}, [location, page, user]); }, 300);
// Cleanup
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, [location.pathname, user]); // Reduzido dependências para apenas pathname e user
return null; return null;
} }

View File

@ -33,12 +33,10 @@ import { TestWordHighlighter } from './pages/TestWordHighlighter';
import { ExercisePage } from './pages/student-dashboard/ExercisePage'; import { ExercisePage } from './pages/student-dashboard/ExercisePage';
import { EvidenceBased } from './pages/landing/EvidenceBased'; import { EvidenceBased } from './pages/landing/EvidenceBased';
import { TextSalesLetter } from './pages/landing/TextSalesLetter'; import { TextSalesLetter } from './pages/landing/TextSalesLetter';
import { PageTracker } from './components/analytics/PageTracker';
function RootLayout({ children }: { children: React.ReactNode }) { function RootLayout({ children }: { children: React.ReactNode }) {
return ( return (
<> <>
<PageTracker />
{children} {children}
</> </>
); );