Home

Awesome

ng2-adsense NPM versioncoverage status

Easy AdSense for Angular Applications

Demo: https://ng2-adsense.xmplaylist.com/

Install

npm install ng2-adsense

Dependencies

Latest version available for each version of Angular

ng2-adsenseAngular
5.4.35.x 6.x 7.x
6.0.38.x
8.0.19.x
9.1.010.x 11.x
10.1.012.x 13.x
11.0.014.x
12.0.015.x
13.0.016.x
current>=17

Use

Add adsense code

Use the standard AdSense code somewhere in your <head></head> as you normally would

<script async src=//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js></script>

Import NgModule

Add AdsenseModule to the imports of your NgModule

import { AdsenseModule } from 'ng2-adsense';

@NgModule({
  imports: [
    // shown passing global defaults (optional)
    AdsenseModule.forRoot({
      adClient: 'ca-pub-7640562161899788',
      adSlot: 7259870550,
    }),
    ...

Show Ad

Uses global defaults which can be overriden via inputs

<ng-adsense></ng-adsense>

Inputs

inputtypedescription
adClientstringaccount ca-pub-XXXXXXXXXXXXXXXX
adSlotstring/numberad slot/number
adFormatstringadsense ad format
adRegionstringolder adsense code to make all ads on page the same
displaystringelement display style
fullWidthResponsivebooleanenable full width responsive ad
heightnumberelement height in px
widthnumberelement width in px
layoutstringused for in-feed ads
layoutKeystringused for in-feed ads
pageLevelAdsbooleanenable page-level ads
adteststringsets up some sort of google test ad
classNamestringadd custom class names to the "ins" element
<ng-adsense
  [adClient]="'ca-pub-7640562161899788'"
  [adSlot]="7259870550"
  [display]="'inline-block'"
  [width]="320"
  [height]="108"
></ng-adsense>