34 lines
806 B
TypeScript
34 lines
806 B
TypeScript
'use client';
|
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
interface MetricObserverProps {
|
|
postId: string;
|
|
}
|
|
|
|
export default function MetricObserver({ postId }: MetricObserverProps) {
|
|
const isRegistered = useRef(false);
|
|
|
|
useEffect(() => {
|
|
if (isRegistered.current) return;
|
|
|
|
// Register Impression
|
|
const trackImpression = async () => {
|
|
try {
|
|
await fetch('/api/metrics', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ postId, tipo: 'impresion' })
|
|
});
|
|
isRegistered.current = true;
|
|
} catch (error) {
|
|
console.error("Metric Error", error);
|
|
}
|
|
};
|
|
|
|
trackImpression();
|
|
}, [postId]);
|
|
|
|
return null; // Silent component
|
|
}
|