From a70491c720e55c53e40179be112b5c822fac25c4 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 8 Nov 2016 16:46:09 -0500 Subject: [PATCH 1/7] FIX: HTMLBars doesn't allow ` {{else}}

ADVERTISEMENT

@@ -20,10 +16,6 @@ data-ad-client="ca-pub-{{publisher_id}}" data-ad-slot={{ad_code}}> - - {{/if}} {{/if}} From 883d5ecd8517797c7d2bc49232bf516895af5d2d Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 9 Nov 2016 16:46:09 -0500 Subject: [PATCH 2/7] FIX: Catch exception if ads can't be rendered --- .../javascripts/discourse/components/google-adsense.js.es6 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index e7047c2..b26a738 100644 --- a/assets/javascripts/discourse/components/google-adsense.js.es6 +++ b/assets/javascripts/discourse/components/google-adsense.js.es6 @@ -116,11 +116,15 @@ export default Ember.Component.extend({ _triggerAds() { const adsbygoogle = window.adsbygoogle || []; - adsbygoogle.push({}); + + try { + adsbygoogle.push({}); + } catch (ex) { } }, didInsertElement() { this._super(); + Ember.run.scheduleOnce('afterRender', this, this._triggerAds); }, From 910b0d0e4cf46be133b1ce5d2a1564eaa4ef5cea Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 17 Nov 2016 11:45:18 -0500 Subject: [PATCH 3/7] FIX: showing ads on mobile when mobile ad units are not defined --- .../discourse-adplugin.hbs | 28 +++++++++---- .../post-bottom/discourse-adplugin.hbs | 42 +++++++++++++++---- .../discourse-adplugin.hbs | 28 +++++++++---- .../discourse-adplugin.hbs | 28 +++++++++---- 4 files changed, 93 insertions(+), 33 deletions(-) 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..4eababf 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,33 @@ -{{#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}} +{{#if site.mobileView}} + {{#if postSpecificCountAdsense}} + {{#if siteSettings.adsense_mobile_post_bottom_code}} + {{google-adsense placement="post-bottom" postNumber=post_number}} + {{/if}} + {{/if}} + {{#if postSpecificCountDFP}} + {{#if siteSettings.dfp_mobile_post_bottom_code}} + {{google-dfp-ad placement="post-bottom" category=topic.category.slug postNumber=post_number}} + {{/if}} + {{/if}} + {{#if postSpecificCountAmazon}} + {{#if siteSettings.amazon_mobile_post_bottom_src_code}} + {{amazon-product-links placement="post-bottom" postNumber=post_number}} + {{/if}} + {{/if}} +{{else}} + {{#if postSpecificCountAdsense}} + {{#if siteSettings.adsense_post_bottom_code}} + {{google-adsense placement="post-bottom" postNumber=post_number}} + {{/if}} + {{/if}} + {{#if postSpecificCountDFP}} + {{#if siteSettings.dfp_post_bottom_code}} + {{google-dfp-ad placement="post-bottom" category=topic.category.slug postNumber=post_number}} + {{/if}} + {{/if}} + {{#if postSpecificCountAmazon}} + {{#if siteSettings.amazon_post_bottom_src_code}} + {{amazon-product-links placement="post-bottom" postNumber=post_number}} + {{/if}} + {{/if}} +{{/if}} \ No newline at end of file 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 From da1d4e006395590e6fa8566196cd7d22552e3f2f Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 21 Nov 2016 15:55:03 -0500 Subject: [PATCH 4/7] FIX: when dfp/adsense publisher id is missing, don't render blank ad units --- .../discourse/components/google-adsense.js.es6 | 6 ++++++ .../discourse/components/google-dfp-ad.js.es6 | 11 +++++++++++ .../discourse/templates/components/google-adsense.hbs | 2 +- .../discourse/templates/components/google-dfp-ad.hbs | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index b26a738..6624e3b 100644 --- a/assets/javascripts/discourse/components/google-adsense.js.es6 +++ b/assets/javascripts/discourse/components/google-adsense.js.es6 @@ -125,6 +125,8 @@ export default Ember.Component.extend({ didInsertElement() { this._super(); + if (!this.get('showAd')) { return; } + Ember.run.scheduleOnce('afterRender', this, this._triggerAds); }, @@ -151,4 +153,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/google-adsense.hbs b/assets/javascripts/discourse/templates/components/google-adsense.hbs index f345c79..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

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

From de4a8366fcc239dff8b0eabfbba6768f89b5e144 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 21 Nov 2016 15:58:29 -0500 Subject: [PATCH 5/7] FIX: don't load adsbygoogle.js when adsense publisher id isn't set --- assets/javascripts/discourse/components/google-adsense.js.es6 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index 6624e3b..34ae824 100644 --- a/assets/javascripts/discourse/components/google-adsense.js.es6 +++ b/assets/javascripts/discourse/components/google-adsense.js.es6 @@ -24,6 +24,8 @@ function splitHeightInt(value) { // On each page change, the child is removed and elements part of Adsense's googleads are removed/undefined. function changePage() { + if (!Discourse.SiteSettings.adsense_publisher_code) { return; } + const ads = document.getElementById("adsense_loader"); if (ads) { ads.parentNode.removeChild(ads); From 1cd0bef9333c24b8aca375e058f7034a58d28a2b Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 23 Nov 2016 14:29:30 -0500 Subject: [PATCH 6/7] FIX: Support for embedding ads via component --- .../components/adplugin-container.hbs | 33 ++++++++++++++++++ .../post-bottom/discourse-adplugin.hbs | 34 +------------------ .../initializers/initialize-ad-plugin.js.es6 | 6 ++++ 3 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 assets/javascripts/discourse/templates/components/adplugin-container.hbs 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/connectors/post-bottom/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/post-bottom/discourse-adplugin.hbs index 4eababf..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,33 +1 @@ -{{#if site.mobileView}} - {{#if postSpecificCountAdsense}} - {{#if siteSettings.adsense_mobile_post_bottom_code}} - {{google-adsense placement="post-bottom" postNumber=post_number}} - {{/if}} - {{/if}} - {{#if postSpecificCountDFP}} - {{#if siteSettings.dfp_mobile_post_bottom_code}} - {{google-dfp-ad placement="post-bottom" category=topic.category.slug postNumber=post_number}} - {{/if}} - {{/if}} - {{#if postSpecificCountAmazon}} - {{#if siteSettings.amazon_mobile_post_bottom_src_code}} - {{amazon-product-links placement="post-bottom" postNumber=post_number}} - {{/if}} - {{/if}} -{{else}} - {{#if postSpecificCountAdsense}} - {{#if siteSettings.adsense_post_bottom_code}} - {{google-adsense placement="post-bottom" postNumber=post_number}} - {{/if}} - {{/if}} - {{#if postSpecificCountDFP}} - {{#if siteSettings.dfp_post_bottom_code}} - {{google-dfp-ad placement="post-bottom" category=topic.category.slug postNumber=post_number}} - {{/if}} - {{/if}} - {{#if postSpecificCountAmazon}} - {{#if siteSettings.amazon_post_bottom_src_code}} - {{amazon-product-links placement="post-bottom" postNumber=post_number}} - {{/if}} - {{/if}} -{{/if}} \ No newline at end of file +{{adplugin-container model=this}} 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' From 620404ab9ef098799d5bd825a963ec42d2892b76 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 5 Dec 2016 13:31:11 -0500 Subject: [PATCH 7/7] FIX: AdSense ads not loading on initial page load. Only load adsbygoogle.js once because it's the async library. --- .../components/google-adsense.js.es6 | 65 ++++++++----------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index 34ae824..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,36 +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() { - if (!Discourse.SiteSettings.adsense_publisher_code) { return; } - - 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" : {}, @@ -117,11 +106,13 @@ export default Ember.Component.extend({ }, _triggerAds() { - const adsbygoogle = window.adsbygoogle || []; + loadAdsense().then(function() { + const adsbygoogle = window.adsbygoogle || []; - try { - adsbygoogle.push({}); - } catch (ex) { } + try { + adsbygoogle.push({}); // ask AdSense to fill one ad unit + } catch (ex) {} + }); }, didInsertElement() {