1

I am using Google Adsense for my website, the ads are shown successfully, but when I want to show ad in one component and ad in another component, then in only one component it works and every time it works on the one component(which is on the left side)

Component One

HTML

<div class="text-center">
  <ins class="adsbygoogle ads_resp" style="display:inline-block" data-ad-client="publisherid" data-ad-slot="ad unit id"
       data-full-width-responsive="true"></ins>
</div>

TS

declare interface Window { adsbygoogle: any[]; }

declare var adsbygoogle: any[];

export class OneComponent implements OnInit {
 ngAfterViewInit() {
    setTimeout(() => {
     try {
        (adsbygoogle = window["adsbygoogle"] || []).push({});
         /*           or

    (window["adsbygoogle"] = window["adsbygoogle"] || []).push({}); Both are wrking finr*/
      } catch (e) {
        console.error('adsid',e);
      }
    }, 300);
  }
}

The same code is written in the other component

and the both components are wrapped in another component like

I want to put the ad in both one and other component, when I put the ads in both, then on the left side ad shows and in the right only blank space, if I use ads only in one or other component then it works fine.

One more interesting thing, on the main page of the website(left side), when I put two ad codes then two ads are showing but when the same put on the right side it won't works

In the other pages, only 1 ad is showing unable to show two ads, the two ads are shown only in the left side of the main page.

VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
  • Have you found solution for this? I have the same problem – Keyul Jul 25 '20 at 21:33
  • no, not yet at a time only one manual ad placement and the google auto ads, don't know why that happening. – VIKAS KOHLI Jul 27 '20 at 19:03
  • Actually, I found the solution after posting here. It require to call (window['adsbygoogle'] = window['adsbygoogle'] || []).push({}); two times, if you want to show two ads. Complete answer : https://stackoverflow.com/a/63045073/4067905 – Keyul Jul 28 '20 at 05:28
  • great your worked ? – VIKAS KOHLI Jul 29 '20 at 12:03
  • I checked it's not perfect, sometime load sometimes not, as you know in Angular our page is divided into multiple components, header module and so on and I need google ad in each module but with that way, it's not working – VIKAS KOHLI Aug 07 '20 at 08:39

0 Answers0