Skip to content

adsense 上报广告的展示和点击行为

1.检测原生广告展示

js
  const observedSet = new WeakSet();
  const cbSet = new Set();
  let timer = null;

  async function handleAdStatus (adContainer) {
    const adStatus = adContainer.getAttribute('data-ad-status');
    let id = adContainer.querySelector('iframe')?.getAttribute('id');
    if (adStatus === 'filled') {
      if (cbSet.has(id)) return;
      cbSet.add(id);
      await trackEvent('ad_filled', { id });//打点
    } else if (adStatus === 'unfilled') {
      await trackEvent('ad_unfilled', { id });//打点
    }
  }

  const io = new IntersectionObserver((entries) => {
    entries.forEach(async entry => {
      if (entry.isIntersecting) {
        const adContainer = entry.target;
        if (observedSet.has(adContainer)) return;
        observedSet.add(adContainer);

        const mo = new MutationObserver(async mutations => {
          mutations.forEach(async mutation => {
            if (mutation.type === 'attributes' && mutation.attributeName === 'data-ad-status') {
              await handleAdStatus(adContainer);
              if (adContainer.getAttribute('data-ad-status') === 'filled') {
                mo.disconnect();
                io.unobserve(adContainer);
              }
            }
          });
        });
        mo.observe(adContainer, { attributes: true });

        await handleAdStatus(adContainer);
        if (adContainer.getAttribute('data-ad-status') === 'filled') {
          mo.disconnect();
          io.unobserve(adContainer);
        }
      }
    });
  }, {
    threshold: 0.5 // 至少有 50% 可见才打点
  });

  timer = setInterval(() => {
    const adContainers = document.querySelectorAll('.adsbygoogle');
    adContainers.forEach(adContainer => {
      io.observe(adContainer);
    });
    // 可选:如果所有广告都已处理,可以关闭定时器
    if (cbSet.size === adContainers.length && timer) {
      clearInterval(timer);
      timer = null;
    }
  }, 1000);

2.检测插屏广告的展示

js
  let t = setInterval(async () => {
    const hash = window.location.hash;
    if (hash.indexOf('google') > -1) {
      await trackEvent('ad_cp_filled'); //打点
      clearInterval(t);
    }
  }, 1000);

3.检测广告是否被点击了

js
// 在广告被填充的地方添加监听
 adContainer.addEventListener('click', async function () {
  console.log('ad click');
  await trackEvent('ad_click', { id });
 });

以上代码是否对adsense 账号有风险未知,请谨慎使用

备案号:豫ICP备17017964号