angularjs - Google AdSense ads in Angular 2 components? -


i'm trying load responsive ads in adcomponent in app. component dead simple:

import { component } '@angular/core'; import { form_directives, core_directives } '@angular/common';  @component({   selector: 'ad',   templateurl: 'app/views/ad.html',   directives: [ form_directives, core_directives ] }) export class adcomponent {} 

ad.html:

<div class="demo-card-wide mdl-card mdl-shadow--2dp ad-card">   <div class="mdl-card__supporting-text">     <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>     <ins class="adsbygoogle"         style="display:block"         data-ad-client="ca-pub-0123456789"         data-ad-slot="0123456789"         data-ad-format="rectangle, horizontal"></ins>     <script>     (adsbygoogle = window.adsbygoogle || []).push({});     </script>   </div> </div> 

i'm finding these script tags don't make ui , this seems purposeful decision.

i might able move around of code such js reference head of index.html , window.adsbygoogle part component constructor i'm not sure if these kinds of modifications allowed or if there's better alternative these ads work in angular 2.

does have experience google's adsense ads working in angular 2? there different, proper way this?

here's i've come , have working. admit may stretching "don't modify our code" rule of adsense, doing best keep heart of code doing same thing it's supposed doing:

// ad component import { component, afterviewinit } '@angular/core'; import { form_directives, core_directives } '@angular/common';  @component({   selector: 'ad',   templateurl: 'app/views/ad.html',   directives: [ form_directives, core_directives ] }) export class adcomponent implements afterviewinit {    ngafterviewinit() {     try {       (adsbygoogle = window.adsbygoogle || []).push({});     } catch (e) {}   } }  // ad.html <div class="demo-card-wide mdl-card mdl-shadow--2dp ad-card">   <div class="mdl-card__supporting-text">     <ins class="adsbygoogle" style="display:inline-block;width:330px;height:120px" data-ad-client="my_client_number" data-ad-slot="my_ad_slot_number"></ins>   </div> </div> 

the design of website has ads in material design lite cards that's 2 outer divs in view. <ins> tag cut , paste same tag adsense gave me.

i used afterviewinit because seems adsense watches array adsbygoogle know when scan new ad in dom. don't want modify array until dom has <ins> tag.

i wrapped line in try/catch because testing ad blocker showed component throw error when blocker turned on. in traditional pages error happen without side effects. in angular 2 page stops change detection.

i have done best keep spirit of adsense provided code supposed , how intended behave. goal bring out-of-date requirements functional angular 2 application. works fine in angular 2's rc4 across browsers.

here's hoping don't see tos break...

to typescript agree you'll need add few lines .d.ts file:

declare interface window {   adsbygoogle: any[]; }  declare var adsbygoogle: any[]; 

these make typescript accept (adsbygoogle = window.adsbygoogle || []).push({}); line in ad component.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)