diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6
index a349ff4..0aa2959 100644
--- a/assets/javascripts/discourse/components/google-adsense.js.es6
+++ b/assets/javascripts/discourse/components/google-adsense.js.es6
@@ -1,14 +1,17 @@
import { withPluginApi } from 'discourse/lib/plugin-api';
import PageTracker from 'discourse/lib/page-tracker';
+import loadScript from 'discourse/lib/load-script';
-var ad_width = '';
-var ad_height = '';
-var ad_mobile_width = 320;
-var ad_mobile_height = 50;
-var currentUser = Discourse.User.current();
-var publisher_id = Discourse.SiteSettings.adsense_publisher_code;
-var mobile_width = 320;
-var mobile_height = 50;
+var _loaded = false,
+ _promise = null,
+ ad_width = '',
+ ad_height = '',
+ ad_mobile_width = 320,
+ ad_mobile_height = 50,
+ currentUser = Discourse.User.current(),
+ publisher_id = Discourse.SiteSettings.adsense_publisher_code,
+ mobile_width = 320,
+ mobile_height = 50;
const mobileView = Discourse.Site.currentProp('mobileView');
@@ -22,34 +25,22 @@ function splitHeightInt(value) {
return str.trim();
}
-// On each page change, the child is removed and elements part of Adsense's googleads are removed/undefined.
-function changePage() {
- 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;
- }
- }
+function loadAdsense() {
+ if (_loaded) {
+ return Ember.RSVP.resolve();
}
- // Reinitialize script so that the ad can reload
- const ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.id="adsense_loader";
- ga.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
- const s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
-}
+ if (_promise) {
+ return _promise;
+ }
-function oldPluginCode() {
- PageTracker.current().on('change', changePage);
-}
+ var adsenseSrc = (('https:' === document.location.protocol) ? 'https:' : 'http:') + '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
+ _promise = loadScript(adsenseSrc, { scriptTag: true }).then(function() {
+ _loaded = true;
+ });
-function watchPageChanges(api) {
- api.onPageChange(changePage);
+ return _promise;
}
-withPluginApi('0.1', watchPageChanges, { noApi: oldPluginCode });
var data = {
"topic-list-top" : {},
@@ -106,7 +97,7 @@ export default Ember.Component.extend({
mobile_width: mobile_width,
mobile_height: mobile_height,
- init: function() {
+ init() {
this.set('ad_width', data[this.placement]["ad_width"] );
this.set('ad_height', data[this.placement]["ad_height"] );
this.set('ad_code', data[this.placement]["ad_code"] );
@@ -114,6 +105,24 @@ export default Ember.Component.extend({
this._super();
},
+ _triggerAds() {
+ loadAdsense().then(function() {
+ const adsbygoogle = window.adsbygoogle || [];
+
+ try {
+ adsbygoogle.push({}); // ask AdSense to fill one ad unit
+ } catch (ex) {}
+ });
+ },
+
+ didInsertElement() {
+ this._super();
+
+ if (!this.get('showAd')) { return; }
+
+ Ember.run.scheduleOnce('afterRender', this, this._triggerAds);
+ },
+
adWrapperStyle: function() {
return `width: ${this.get('ad_width')}px; height: ${this.get('ad_height')}px;`.htmlSafe();
}.property('ad_width', 'ad_height'),
@@ -137,4 +146,8 @@ export default Ember.Component.extend({
checkTrustLevels: function() {
return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level));
}.property('trust_level'),
+
+ showAd: function() {
+ return Discourse.SiteSettings.adsense_publisher_code && this.get('checkTrustLevels');
+ }.property('checkTrustLevels')
});
diff --git a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 b/assets/javascripts/discourse/components/google-dfp-ad.js.es6
index b05c5d1..dc05230 100755
--- a/assets/javascripts/discourse/components/google-dfp-ad.js.es6
+++ b/assets/javascripts/discourse/components/google-dfp-ad.js.es6
@@ -56,6 +56,11 @@ function custom_targeting(key_array, value_array, location) {
}
function defineSlot(divId, placement, settings, isMobile) {
+
+ if (!settings.dfp_publisher_id) {
+ return;
+ }
+
var ad, width, height;
if (ads[divId]) {
@@ -185,6 +190,10 @@ export default Ember.Component.extend({
return `width: ${this.get('width')}px;`.htmlSafe();
}.property('width'),
+ showAd: function() {
+ return Discourse.SiteSettings.dfp_publisher_id && this.get('checkTrustLevels');
+ }.property('checkTrustLevels'),
+
checkTrustLevels: function() {
return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level));
}.property('trust_level'),
@@ -205,6 +214,8 @@ export default Ember.Component.extend({
}.observes('refreshOnChange'),
_initGoogleDFP: function() {
+ if (!this.get('showAd')) { return; }
+
var self = this;
loadGoogle(this.siteSettings).then(function() {
self.set('loadedGoogletag', true);
diff --git a/assets/javascripts/discourse/templates/components/adplugin-container.hbs b/assets/javascripts/discourse/templates/components/adplugin-container.hbs
new file mode 100644
index 0000000..65e1b28
--- /dev/null
+++ b/assets/javascripts/discourse/templates/components/adplugin-container.hbs
@@ -0,0 +1,33 @@
+{{#if site.mobileView}}
+ {{#if model.postSpecificCountAdsense}}
+ {{#if siteSettings.adsense_mobile_post_bottom_code}}
+ {{google-adsense placement="post-bottom" postNumber=model.post_number}}
+ {{/if}}
+ {{/if}}
+ {{#if model.postSpecificCountDFP}}
+ {{#if siteSettings.dfp_mobile_post_bottom_code}}
+ {{google-dfp-ad placement="post-bottom" category=model.topic.category.slug postNumber=model.post_number}}
+ {{/if}}
+ {{/if}}
+ {{#if model.postSpecificCountAmazon}}
+ {{#if siteSettings.amazon_mobile_post_bottom_src_code}}
+ {{amazon-product-links placement="post-bottom" postNumber=model.post_number}}
+ {{/if}}
+ {{/if}}
+{{else}}
+ {{#if model.postSpecificCountAdsense}}
+ {{#if siteSettings.adsense_post_bottom_code}}
+ {{google-adsense placement="post-bottom" postNumber=model.post_number}}
+ {{/if}}
+ {{/if}}
+ {{#if model.postSpecificCountDFP}}
+ {{#if siteSettings.dfp_post_bottom_code}}
+ {{google-dfp-ad placement="post-bottom" category=model.topic.category.slug postNumber=model.post_number}}
+ {{/if}}
+ {{/if}}
+ {{#if model.postSpecificCountAmazon}}
+ {{#if siteSettings.amazon_post_bottom_src_code}}
+ {{amazon-product-links placement="post-bottom" postNumber=model.post_number}}
+ {{/if}}
+ {{/if}}
+{{/if}}
diff --git a/assets/javascripts/discourse/templates/components/google-adsense.hbs b/assets/javascripts/discourse/templates/components/google-adsense.hbs
index 7d81e18..ee5e7d5 100644
--- a/assets/javascripts/discourse/templates/components/google-adsense.hbs
+++ b/assets/javascripts/discourse/templates/components/google-adsense.hbs
@@ -1,4 +1,4 @@
-{{#if checkTrustLevels}}
+{{#if showAd}}
{{#if site.mobileView}}
ADVERTISEMENT
@@ -7,10 +7,6 @@
data-ad-client="ca-pub-{{publisher_id}}"
data-ad-slot={{ad_mobile_code}}>
-
-
{{else}}
ADVERTISEMENT
@@ -20,10 +16,6 @@
data-ad-client="ca-pub-{{publisher_id}}"
data-ad-slot={{ad_code}}>
-
-
{{/if}}
{{/if}}
diff --git a/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs b/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs
index cb3fb17..c484d6a 100755
--- a/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs
+++ b/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs
@@ -1,4 +1,4 @@
-{{#if checkTrustLevels}}
+{{#if showAd}}
{{#if site.mobileView}}
ADVERTISEMENT
diff --git a/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs
index 0400350..c89417d 100644
--- a/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs
+++ b/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs
@@ -1,9 +1,21 @@
-{{#if siteSettings.adsense_topic_list_top_code}}
- {{google-adsense placement="topic-list-top"}}
-{{/if}}
-{{#if siteSettings.dfp_topic_list_top_code}}
- {{google-dfp-ad placement="topic-list-top" refreshOnChange=loading category=category.slug}}
-{{/if}}
-{{#if siteSettings.amazon_topic_list_top_src_code}}
- {{amazon-product-links placement="topic-list-top"}}
+{{#if site.mobileView}}
+ {{#if siteSettings.adsense_mobile_topic_list_top_code}}
+ {{google-adsense placement="topic-list-top"}}
+ {{/if}}
+ {{#if siteSettings.dfp_mobile_topic_list_top_code}}
+ {{google-dfp-ad placement="topic-list-top" refreshOnChange=loading category=category.slug}}
+ {{/if}}
+ {{#if siteSettings.amazon_mobile_topic_list_top_src_code}}
+ {{amazon-product-links placement="topic-list-top"}}
+ {{/if}}
+{{else}}
+ {{#if siteSettings.adsense_topic_list_top_code}}
+ {{google-adsense placement="topic-list-top"}}
+ {{/if}}
+ {{#if siteSettings.dfp_topic_list_top_code}}
+ {{google-dfp-ad placement="topic-list-top" refreshOnChange=loading category=category.slug}}
+ {{/if}}
+ {{#if siteSettings.amazon_topic_list_top_src_code}}
+ {{amazon-product-links placement="topic-list-top"}}
+ {{/if}}
{{/if}}
\ No newline at end of file
diff --git a/assets/javascripts/discourse/templates/connectors/post-bottom/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/post-bottom/discourse-adplugin.hbs
index b922b5a..ab5aa7d 100644
--- a/assets/javascripts/discourse/templates/connectors/post-bottom/discourse-adplugin.hbs
+++ b/assets/javascripts/discourse/templates/connectors/post-bottom/discourse-adplugin.hbs
@@ -1,9 +1 @@
-{{#if postSpecificCountAdsense}}
- {{google-adsense placement="post-bottom" postNumber=post_number}}
-{{/if}}
-{{#if postSpecificCountDFP}}
- {{google-dfp-ad placement="post-bottom" category=topic.category.slug postNumber=post_number}}
-{{/if}}
-{{#if postSpecificCountAmazon}}
- {{amazon-product-links placement="post-bottom" postNumber=post_number}}
-{{/if}}
+{{adplugin-container model=this}}
diff --git a/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs
index 2928caf..ed36964 100644
--- a/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs
+++ b/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs
@@ -1,9 +1,21 @@
-{{#if siteSettings.adsense_topic_above_post_stream_code}}
- {{google-adsense placement="topic-above-post-stream"}}
-{{/if}}
-{{#if siteSettings.dfp_topic_above_post_stream_code}}
- {{google-dfp-ad placement="topic-above-post-stream" refreshOnChange=model.id category=model.category.slug}}
-{{/if}}
-{{#if siteSettings.amazon_topic_above_post_stream_src_code}}
- {{amazon-product-links placement="topic-above-post-stream"}}
+{{#if site.mobileView}}
+ {{#if siteSettings.adsense_mobile_topic_above_post_stream_code}}
+ {{google-adsense placement="topic-above-post-stream"}}
+ {{/if}}
+ {{#if siteSettings.dfp_mobile_topic_above_post_stream_code}}
+ {{google-dfp-ad placement="topic-above-post-stream" refreshOnChange=model.id category=model.category.slug}}
+ {{/if}}
+ {{#if siteSettings.amazon_mobile_topic_above_post_stream_src_code}}
+ {{amazon-product-links placement="topic-above-post-stream"}}
+ {{/if}}
+{{else}}
+ {{#if siteSettings.adsense_topic_above_post_stream_code}}
+ {{google-adsense placement="topic-above-post-stream"}}
+ {{/if}}
+ {{#if siteSettings.dfp_topic_above_post_stream_code}}
+ {{google-dfp-ad placement="topic-above-post-stream" refreshOnChange=model.id category=model.category.slug}}
+ {{/if}}
+ {{#if siteSettings.amazon_topic_above_post_stream_src_code}}
+ {{amazon-product-links placement="topic-above-post-stream"}}
+ {{/if}}
{{/if}}
\ No newline at end of file
diff --git a/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs
index 8f17b29..8e3f62c 100644
--- a/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs
+++ b/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs
@@ -1,9 +1,21 @@
-{{#if siteSettings.adsense_topic_above_suggested_code}}
- {{google-adsense placement="topic-above-suggested"}}
-{{/if}}
-{{#if siteSettings.dfp_topic_above_suggested_code}}
- {{google-dfp-ad placement="topic-above-suggested" refreshOnChange=model.id category=model.category.slug}}
-{{/if}}
-{{#if siteSettings.amazon_topic_above_suggested_src_code}}
- {{amazon-product-links placement="topic-above-suggested"}}
+{{#if site.mobileView}}
+ {{#if siteSettings.adsense_mobile_topic_above_suggested_code}}
+ {{google-adsense placement="topic-above-suggested"}}
+ {{/if}}
+ {{#if siteSettings.dfp_mobile_topic_above_suggested_code}}
+ {{google-dfp-ad placement="topic-above-suggested" refreshOnChange=model.id category=model.category.slug}}
+ {{/if}}
+ {{#if siteSettings.amazon_mobile_topic_above_suggested_src_code}}
+ {{amazon-product-links placement="topic-above-suggested"}}
+ {{/if}}
+{{else}}
+ {{#if siteSettings.adsense_topic_above_suggested_code}}
+ {{google-adsense placement="topic-above-suggested"}}
+ {{/if}}
+ {{#if siteSettings.dfp_topic_above_suggested_code}}
+ {{google-dfp-ad placement="topic-above-suggested" refreshOnChange=model.id category=model.category.slug}}
+ {{/if}}
+ {{#if siteSettings.amazon_topic_above_suggested_src_code}}
+ {{amazon-product-links placement="topic-above-suggested"}}
+ {{/if}}
{{/if}}
\ No newline at end of file
diff --git a/assets/javascripts/initializers/initialize-ad-plugin.js.es6 b/assets/javascripts/initializers/initialize-ad-plugin.js.es6
index 1b6d473..8971918 100644
--- a/assets/javascripts/initializers/initialize-ad-plugin.js.es6
+++ b/assets/javascripts/initializers/initialize-ad-plugin.js.es6
@@ -30,6 +30,12 @@ export default {
withPluginApi('0.1', api => {
api.decorateWidget('post:after', dec => {
+
+ if (dec.canConnectComponent) {
+ return dec.connect({ component: 'adplugin-container', context: 'model' });
+ }
+
+ // Old way for backwards compatibility
return dec.connect({
templateName: 'connectors/post-bottom/discourse-adplugin',
context: 'model'