added advertising

This commit is contained in:
SlimusMinus
2026-09-23 23:38:54 +03:00
parent 3661985a6a
commit 4e20315998
33 changed files with 3351 additions and 18 deletions

View 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]);
}