export const trackView = (adId) => {
    hubspotTrack('View', adId);
    googleAnalyticsTrack('View', adId);
};
export const trackClick = (adId, event) => {
    const timeForTrackingRequests = 150; // ms
    if (event) {
        event.preventDefault();
    }
    hubspotTrack('Click', adId);
    googleAnalyticsTrack('Click', adId);
    // give tracking request time to complete
    setTimeout(() => {
        const link = hrefClimber(event === null || event === void 0 ? void 0 : event.target);
        if (link.target && link.target.toLowerCase() === '_blank') {
            window.open(link.href);
        }
        else if (link.href) {
            document.location = link.href;
        }
    }, timeForTrackingRequests);
};
const hubspotTrack = (type, adId) => {
    if (!window['_hsq']) {
        console.warn('Unable to track Hubspot event, _hsq not found', type, adId);
        return;
    }
    window['_hsq'].push(['trackEvent', {
            id: `Docs ad - ${type} - ${adId}`
        }]);
};
const googleAnalyticsTrack = (type, adId) => {
    if (!window['gtag']) {
        console.warn('Unable to track Google Analytics event, gtag not found', type, adId);
        return;
    }
    window['gtag']('event', `Docs ad - ${type} - ${adId}`, {
        'event_category': `Docs ad - ${type}`,
        'event_label': adId
    });
};
// recursive function to climb the DOM looking for href tags
const hrefClimber = (el) => {
    if (el['href']) {
        return el;
    }
    else if (el.parentNode) {
        return hrefClimber(el.parentNode);
    }
};
