add analytics
This commit is contained in:
68
app/src/services/analytics/useLetramentoTracking.js
Normal file
68
app/src/services/analytics/useLetramentoTracking.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { getAnalytics } from './AnalyticsManager';
|
||||
|
||||
/**
|
||||
* Hook para rastrear uso de atividades de letramento
|
||||
* @param {string} activityId - ID único da atividade de letramento (ex: 'mouse-basico')
|
||||
* @param {string} categoryName - Nome da categoria (ex: 'Mouse', 'Teclado')
|
||||
*/
|
||||
export function useLetramentoTracking(activityId, categoryName) {
|
||||
const analyticsRef = useRef(getAnalytics());
|
||||
const sessionStartRef = useRef(Date.now());
|
||||
const completionTrackedRef = useRef(false);
|
||||
|
||||
// Track activity start
|
||||
useEffect(() => {
|
||||
if (!activityId) return;
|
||||
|
||||
sessionStartRef.current = Date.now();
|
||||
analyticsRef.current.trackEvent('letramento_atividade_iniciada', {
|
||||
atividade_id: activityId,
|
||||
categoria: categoryName || 'letramento',
|
||||
});
|
||||
|
||||
return () => {
|
||||
// Track abandonment on unmount if not completed
|
||||
if (!completionTrackedRef.current) {
|
||||
analyticsRef.current.trackEvent('letramento_atividade_abandonada', {
|
||||
atividade_id: activityId,
|
||||
categoria: categoryName || 'letramento',
|
||||
tempo_sessao_segundos: Math.round((Date.now() - sessionStartRef.current) / 1000),
|
||||
});
|
||||
}
|
||||
};
|
||||
}, [activityId, categoryName]);
|
||||
|
||||
// Track activity completion
|
||||
const trackCompletion = (success = true) => {
|
||||
if (!activityId || completionTrackedRef.current) return;
|
||||
completionTrackedRef.current = true;
|
||||
|
||||
const sessionDuration = Math.round((Date.now() - sessionStartRef.current) / 1000);
|
||||
|
||||
analyticsRef.current.trackEvent(
|
||||
success ? 'letramento_atividade_completada' : 'letramento_atividade_falhou',
|
||||
{
|
||||
atividade_id: activityId,
|
||||
categoria: categoryName || 'letramento',
|
||||
tempo_sessao_segundos: sessionDuration,
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// Track custom letramento event
|
||||
const trackEvent = (eventName, eventData = {}) => {
|
||||
if (!activityId) return;
|
||||
|
||||
analyticsRef.current.trackEvent(eventName, {
|
||||
atividade_id: activityId,
|
||||
categoria: categoryName || 'letramento',
|
||||
...eventData,
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
trackCompletion,
|
||||
trackEvent,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user