added advertising
This commit is contained in:
36
src/hooks/useAdImpressionTracking.js
Normal file
36
src/hooks/useAdImpressionTracking.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { useEffect } from 'react';
|
||||
import { API_URL } from '../config/authConstants';
|
||||
import { getAdSessionId } from '../utils/adSession';
|
||||
import { apiFetch } from '../api/apiClient';
|
||||
|
||||
export function useAdImpressionTracking(ref, advertisingId) {
|
||||
useEffect(() => {
|
||||
const node = ref.current;
|
||||
if (!node || !advertisingId) return;
|
||||
|
||||
let sent = false;
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
if (!entry.isIntersecting || sent) return;
|
||||
sent = true;
|
||||
observer.disconnect();
|
||||
|
||||
const payload = JSON.stringify({
|
||||
advertisingId,
|
||||
sessionId: getAdSessionId(),
|
||||
});
|
||||
|
||||
apiFetch(`${API_URL}/api/ads/impression`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: payload,
|
||||
keepalive: true, // отправка при уходе со страницы, как sendBeacon
|
||||
}).catch(() => {});
|
||||
},
|
||||
{ threshold: 0.25 }
|
||||
);
|
||||
|
||||
observer.observe(node);
|
||||
return () => observer.disconnect();
|
||||
}, [ref, advertisingId]);
|
||||
}
|
||||
Reference in New Issue
Block a user