FIX: AdSense ads not loading on initial page load. Only load adsbygoogle.js once because it's the async library.

This commit is contained in:
Neil Lalonde 2016-12-05 13:31:11 -05:00
parent 1cd0bef933
commit 620404ab9e
1 changed files with 28 additions and 37 deletions

View File

@ -1,14 +1,17 @@
import { withPluginApi } from 'discourse/lib/plugin-api'; import { withPluginApi } from 'discourse/lib/plugin-api';
import PageTracker from 'discourse/lib/page-tracker'; import PageTracker from 'discourse/lib/page-tracker';
import loadScript from 'discourse/lib/load-script';
var ad_width = ''; var _loaded = false,
var ad_height = ''; _promise = null,
var ad_mobile_width = 320; ad_width = '',
var ad_mobile_height = 50; ad_height = '',
var currentUser = Discourse.User.current(); ad_mobile_width = 320,
var publisher_id = Discourse.SiteSettings.adsense_publisher_code; ad_mobile_height = 50,
var mobile_width = 320; currentUser = Discourse.User.current(),
var mobile_height = 50; publisher_id = Discourse.SiteSettings.adsense_publisher_code,
mobile_width = 320,
mobile_height = 50;
const mobileView = Discourse.Site.currentProp('mobileView'); const mobileView = Discourse.Site.currentProp('mobileView');
@ -22,36 +25,22 @@ function splitHeightInt(value) {
return str.trim(); return str.trim();
} }
// On each page change, the child is removed and elements part of Adsense's googleads are removed/undefined. function loadAdsense() {
function changePage() { if (_loaded) {
if (!Discourse.SiteSettings.adsense_publisher_code) { return; } return Ember.RSVP.resolve();
const ads = document.getElementById("adsense_loader");
if (ads) {
ads.parentNode.removeChild(ads);
for (var key in window) {
// Undefining all elements starting with google except for googletag so that the reloading doesn't affect dfp. Potential future
// conflicts may occur if other plugins have element starting with google.
if(key.indexOf('google') !== -1 && key.indexOf('googletag') === -1) {
window[key] = undefined;
}
}
} }
// Reinitialize script so that the ad can reload if (_promise) {
const ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.id="adsense_loader"; return _promise;
ga.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; }
const s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}
function oldPluginCode() { var adsenseSrc = (('https:' === document.location.protocol) ? 'https:' : 'http:') + '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
PageTracker.current().on('change', changePage); _promise = loadScript(adsenseSrc, { scriptTag: true }).then(function() {
} _loaded = true;
});
function watchPageChanges(api) { return _promise;
api.onPageChange(changePage);
} }
withPluginApi('0.1', watchPageChanges, { noApi: oldPluginCode });
var data = { var data = {
"topic-list-top" : {}, "topic-list-top" : {},
@ -117,11 +106,13 @@ export default Ember.Component.extend({
}, },
_triggerAds() { _triggerAds() {
const adsbygoogle = window.adsbygoogle || []; loadAdsense().then(function() {
const adsbygoogle = window.adsbygoogle || [];
try { try {
adsbygoogle.push({}); adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) { } } catch (ex) {}
});
}, },
didInsertElement() { didInsertElement() {