Heyzap / heyzap-cordova

Heyzap SDK for Apache Cordova
https://www.heyzap.com/
10 stars 18 forks source link

Cannot add addEventListener #16

Open rafaellop opened 7 years ago

rafaellop commented 7 years ago

I'm unable to use the addEventListener or I am doing it wrong.

I have successfuly installed the SDK + AdMob and Facebook Audience Network. The test suite shows interstitials and banners correctly. I can also show and display banners and interstitials in the apps, so everything seems to be configured and working properly. However, I'm suffering troubles with the addEventListener events. I'm simply not able to define them. My code is:

document.addEventListener('deviceready', function() {

  HeyzapAds.start("<PUBLISHER_KEY>").then(function() {
    // Start fetching ads

#55       HeyzapAds.BannerAd.addEventListener(HeyzapAds.BannerAd.Events.LOADED,
                function() {
                  // The ad is loaded.
                });

  }, function(error) {
    // Handle Error

  });
}, false);

Unfortunately console reports errors:

26799 TypeError: Cannot read property 'addEventListener' of undefined at app.js:55

What am I doing wrong?

And by the way, how can I use the native banners feature of Admob and FAN?

vintage commented 7 years ago

It seems correct. I don't use banner ads in my app, so can't help here, but this one definitely works for me for reward videos:

HeyzapAds.IncentivizedAd.addEventListener(HeyzapAds.IncentivizedAd.Events.COMPLETE, () => {
    console.log('completed');
});

Can you check it in your codebase to see if it also returns undefined error?

rafaellop commented 7 years ago

Konrad, thanks for the tip. I've checked and this IncentivizedAd listener doesn't return undefined error. All other (BannerAd and VideoAd) shows undefined errors. I've checked the plugin's /www/ads/InterstitialAds.js and /www/ads/IncentivizedAd.js and they looked almost the same. So I replaced VideoAd in all the examples for interstitials callbacs as found in the docs (https://github.com/Heyzap/heyzap-cordova/blob/master/docs/advanced.md) as:

HeyzapAds.VideoAd.addEventListener(HeyzapAds.VideoAd.Events.AVAILABLE,
  function(tag) {
    // An ad is available for display.
  });

to

HeyzapAds.InterstitialAd.addEventListener(HeyzapAds.InterstitialAd.Events.AVAILABLE,
  function(tag) {
    // An ad is available for display.
  });

and the callbacks for Interstitial ads started to bubble up.

I supposed the same issue is related to the BannerAd, but the code seemed OK. I've checked it one more time, typed instead of copy&paste and it ... stopped to complain undefined errors.

However, the LOADED type was never called back even if the banner is visible on the screen. So I changed the original plugin sources to use AVAILABLE like in the InterstitialAd/IncentivizedAd instead of LOADED and... it started to work.

I'll introduce the changes to the repository and I'll post a pull, but such errors are such a headache...