angularcordovaionic-frameworkvideogular

Videogular2 Ima implementation in Ionic 3


I use Videogular 2 on Ionic 3. I follow this tutorial (https://videogular.github.io/videogular2/modules/ima-ads/) for the implementation. But when Videogular tries to load ads on VgImaAds.loadAds method, it gives this error 'googletag is not defined'. I have imported every module needed in App.module.ts.Here is the imports in App.module.ts.

imports: [
IonicModule.forRoot(MyApp,{
  backButtonText:'',
  backButtonIcon:'arrow-back',
  iconMode:'md',
  modalEnter: 'modal-slide-in',
  modalLeave: 'modal-slide-out',
}),
IonicStorageModule.forRoot(),
BrowserModule,
VgCoreModule,
VgControlsModule,
VgOverlayPlayModule,
VgBufferingModule,
VgImaAdsModule]

Here is my page's component html.

 <ion-content>
    <vg-player id="demo-player">
        <vg-ima-ads
            [vgCompanion]="'companion-ad'"
            [vgCompanionSize]="[728, 90]"
            [vgNetwork]="'6062'"
            [vgUnitPath]="'iab_vast_samples'"
            [vgAdTagUrl]="'http://pubads.g.doubleclick.net/gampad/ads?sz=640x360&iu=/20277799247/Tv8-App-Preroll&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&url=[referrer_url]&description_url=[description_url]&correlator='"
            [vgSkipButton]="'.skip-button'">
        </vg-ima-ads>

        <video [vgMedia]="media" #media id="singleVideo" preload="auto" crossorigin>
            <source [src]="liveStreamLink" type="application/x-mpegURL">
        </video>
    </vg-player>
    <div class='skip-button'>custom skip ad button</div>
    <div id="companion-ad"></div>
</ion-content>

Also, i have added the tag in the head section of index.html. But apparently, it can't find the googletag property of this script on VgImaAds.loadAds . What should i do?


Solution

  • You need to initialize googletag property before declaration for the ima script in index.html

    <script>
        var googletag = googletag || {};
        googletag.cmd = googletag.cmd || [];
        (function() {
        var gads = document.createElement('script');
        gads.async = true; gads.type = 'text/javascript';
        gads.src = 'https://www.googletagservices.com/tag/js/gpt.js';
        var node = document.getElementsByTagName('script')[0];
        node.parentNode.insertBefore(gads, node);
        })();
    </script>