mirror of
https://github.com/lucasrcsantana/story-generator.git
synced 2025-12-17 05:47:52 +00:00
fix: Corrigindo deduplicação de eventos no Rudderstack
This commit is contained in:
parent
bcbdd07a41
commit
6a1a471ce5
@ -1,4 +1,4 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useRudderstack } from '../../hooks/useRudderstack';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { useAuth } from '../../hooks/useAuth';
|
||||
@ -7,8 +7,22 @@ export function PageTracker() {
|
||||
const location = useLocation();
|
||||
const { page } = useRudderstack();
|
||||
const { user } = useAuth();
|
||||
const lastPageTracked = useRef<string | null>(null);
|
||||
const timeoutRef = useRef<NodeJS.Timeout>();
|
||||
|
||||
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
|
||||
const deviceInfo = {
|
||||
screenWidth: window.screen.width,
|
||||
@ -73,6 +87,9 @@ export function PageTracker() {
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
});
|
||||
|
||||
// Atualiza a última página rastreada
|
||||
lastPageTracked.current = location.pathname;
|
||||
|
||||
// Atualiza informações da sessão
|
||||
sessionStorage.setItem('lastVisitedPage', location.pathname);
|
||||
if (!localStorage.getItem('returningVisitor')) {
|
||||
@ -81,7 +98,15 @@ export function PageTracker() {
|
||||
if (!sessionStorage.getItem('sessionStartTime')) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -33,12 +33,10 @@ import { TestWordHighlighter } from './pages/TestWordHighlighter';
|
||||
import { ExercisePage } from './pages/student-dashboard/ExercisePage';
|
||||
import { EvidenceBased } from './pages/landing/EvidenceBased';
|
||||
import { TextSalesLetter } from './pages/landing/TextSalesLetter';
|
||||
import { PageTracker } from './components/analytics/PageTracker';
|
||||
|
||||
function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<>
|
||||
<PageTracker />
|
||||
{children}
|
||||
</>
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user