function trackAdEvent(eventType) {
const trackingUrl = ‘https://your-analytics-server.com/track’;
const payload = {
event: eventType,
timestamp: Date.now(),
videoId: ‘video_123’,
adId: ‘ad_campaign_xyz’
};
// METHOD A: The Tracking Pixel (Old School & Reliable)
// Creates an invisible image to ping a server URL. Best for simple tracking.
const pixel = new Image();
pixel.src = `${trackingUrl}?event=${eventType}&t=${Date.now()}`;
// METHOD B: navigator.sendBeacon (Modern Best Practice)
// Specifically designed for analytics. It guarantees the data is sent
// even if the user closes the tab right as they click “Skip”.
if (navigator.sendBeacon) {
navigator.sendBeacon(‘/api/analytics’, JSON.stringify(payload));
}
// METHOD C: Google Analytics (GA4) / Tag Manager Integration
// If you use GTM, you can push the event directly to the data layer.
if (typeof dataLayer !== ‘undefined’) {
window.dataLayer.push({
‘event’: ‘video_ad_interaction’,
‘ad_action’: eventType
});
}
console.log(`Analytics Fired: Ad ${eventType}`); // For your testing
}