1

I have adsense account trying to display ad units in certain places in my site.

the problem is I need to test locally , so I had to follow the steps here: https://stackoverflow.com/a/64694852/11325588

Unfortunately, it only shows an empty ins element with no ads. There's no console errors and no failed network requests. Image shows that empty ins element, some details are hidden for anonymity

import React, { useEffect } from 'react';

type Props = {
  className?: string;
  style?: React.CSSProperties;
  slot: string;
  layout?: string;
  layoutKey?: string;
  format?: string;
  responsive?: string;
  // eslint-disable-next-line react/boolean-prop-naming
  pageLevelAds?: boolean;
  adTest?: string;
};

 const GoogleAd = ({
  className = '',
  style = { display: 'block' },
  slot,
  layout = '',
  layoutKey = '',
  format = 'auto',
  responsive = 'false',
  pageLevelAds = true,
  adTest,
  ...rest
}: Props) => {

    const client = process.env.GOOGLE_ADSENSE_KEY

    if(!client) return null;

  useEffect(() => {
    const p: any = {};
    if (pageLevelAds) {
      p.google_ad_client = client;
      p.enable_page_level_ads = true;
    }

    try {
      if (typeof window === 'object') {
        ((window).adsbygoogle = (window).adsbygoogle || []).push(p);
      }
    } catch {
      // Pass
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  

  return (
    <ins
      className={className}
      style={style}
      data-ad-client={client}
      data-ad-slot={slot}
      // data-ad-layout={layout}
      // data-ad-layout-key={layoutKey}
      data-ad-format={format}
      data-full-width-responsive={responsive}
      data-adtest={adTest}
      {...rest}
    />
  );
};

export default GoogleAd;

then in some page I use

<GoogleAd slot="XXXXXX" format="auto" responsive="true" adTest="on"/>

Hanaffi
  • 11
  • 4
  • Welcome to SO @hanaffi! In order for us to help you we need more information. Please include the code of what's not working properly along with what you have already attempted. – JNYRanger May 20 '22 at 15:04
  • @JNYRanger Hey, Thanks for your reply. Just edited the post and include the code. Can you please check it? Thanks :) – Hanaffi May 23 '22 at 20:05

0 Answers0