From 2f1ac7738053e9917bc1811f484c23406c09a9e1 Mon Sep 17 00:00:00 2001 From: vi Date: Tue, 1 Sep 2015 11:16:07 +1000 Subject: [PATCH 1/4] Added trust levels to site setting and bug found in adsense reload comment out in line 30 to 57 --- .../components/google-adsense.js.es6 | 99 ++++++++++--------- .../templates/components/google-adsense.hbs | 1 - config/locales/server.en.yml | 1 + config/settings.yml | 4 + 4 files changed, 55 insertions(+), 50 deletions(-) diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index 74e5a1d..dd53e38 100644 --- a/assets/javascripts/discourse/components/google-adsense.js.es6 +++ b/assets/javascripts/discourse/components/google-adsense.js.es6 @@ -3,6 +3,7 @@ import PageTracker from 'discourse/lib/page-tracker'; var ad_width = ''; var ad_height = ''; var ad_code = ''; +var currentUser = Discourse.User.current(); var publisher_id = Discourse.SiteSettings.adsense_publisher_code; var preGoogleVars = null; var postGoogleVars = null; @@ -18,43 +19,42 @@ function splitHeightInt(value) { return str.trim(); } - PageTracker.current().on('change', function(url) { - - var ads = document.getElementById("adsense_loader"); if (ads) { - // clear the old element and its state - //ads.remove(); ads.parentNode.removeChild(ads); - for (var i = 0; i < postGoogleVars.length; i++) { - var key = postGoogleVars[i]; - window[key] = undefined; - } - } - - if(preGoogleVars === null) { - preGoogleVars = []; - for(var key in window) { - if(key.indexOf("google") !== -1) { - preGoogleVars.push(key); + for (var key in window) { + if (key.indexOf("google") !== -1){ + window[key] = undefined; } - } - } + // for (var i = 0; i < postGoogleVars.length; i++) { + // var key = postGoogleVars[i]; + // window[key] = undefined; + } + } + + // if(preGoogleVars === null) { + // preGoogleVars = []; + // for(var key in window) { + // if(key.indexOf("google") !== -1) { + // preGoogleVars.push(key); + // } + // } + // } var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.id="adsense_loader"; ga.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'; - ga.addEventListener('load', function(e) { - if(postGoogleVars === null) { - postGoogleVars = []; + // ga.addEventListener('load', function(e) { + // if(postGoogleVars === null) { + // postGoogleVars = []; - for(var key in window) { - if(key.indexOf("google") !== -1 && preGoogleVars.indexOf(key) == -1) { - postGoogleVars.push(key); - } - } - } - }); + // for(var key in window) { + // if(key.indexOf("google") !== -1 && preGoogleVars.indexOf(key) == -1) { + // postGoogleVars.push(key); + // } + // } + // } + // }); var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); }); @@ -66,28 +66,29 @@ var data = { "post-bottom" : {} } - if (Discourse.SiteSettings.adsense_publisher_code) { - if (Discourse.SiteSettings.adsense_topic_list_top_code && !Discourse.SiteSettings.adsense_show_topic_list_top) { - data["topic-list-top"]["ad_code"] = Discourse.SiteSettings.adsense_topic_list_top_code; - data["topic-list-top"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); - data["topic-list-top"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); - } - if (Discourse.SiteSettings.adsense_topic_above_post_stream_code && !Discourse.SiteSettings.adsense_show_topic_above_post_stream) { - data["topic-above-post-stream"]["ad_code"] = Discourse.SiteSettings.adsense_topic_above_post_stream_code; - data["topic-above-post-stream"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes)); - data["topic-above-post-stream"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes)); - } - if (Discourse.SiteSettings.adsense_topic_above_suggested_code && !Discourse.SiteSettings.adsense_show_topic_above_suggested) { - data["topic-above-suggested"]["ad_code"] = Discourse.SiteSettings.adsense_topic_above_suggested_code; - data["topic-above-suggested"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes)); - data["topic-above-suggested"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes)); - } - if (Discourse.SiteSettings.adsense_post_bottom_code && !Discourse.SiteSettings.adsense_show_post_bottom) { - data["post-bottom"]["ad_code"] = Discourse.SiteSettings.adsense_post_bottom_code; - data["post-bottom"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_post_bottom_ad_sizes)); - data["post-bottom"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_post_bottom_ad_sizes)); - } + // if ((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level)) { + if (Discourse.SiteSettings.adsense_topic_list_top_code && !Discourse.SiteSettings.adsense_show_topic_list_top && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { + data["topic-list-top"]["ad_code"] = Discourse.SiteSettings.adsense_topic_list_top_code; + data["topic-list-top"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); + data["topic-list-top"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); + } + if (Discourse.SiteSettings.adsense_topic_above_post_stream_code && !Discourse.SiteSettings.adsense_show_topic_above_post_stream && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { + data["topic-above-post-stream"]["ad_code"] = Discourse.SiteSettings.adsense_topic_above_post_stream_code; + data["topic-above-post-stream"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes)); + data["topic-above-post-stream"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes)); + } + if (Discourse.SiteSettings.adsense_topic_above_suggested_code && !Discourse.SiteSettings.adsense_show_topic_above_suggested && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { + data["topic-above-suggested"]["ad_code"] = Discourse.SiteSettings.adsense_topic_above_suggested_code; + data["topic-above-suggested"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes)); + data["topic-above-suggested"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes)); + } + if (Discourse.SiteSettings.adsense_post_bottom_code && !Discourse.SiteSettings.adsense_show_post_bottom && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { + data["post-bottom"]["ad_code"] = Discourse.SiteSettings.adsense_post_bottom_code; + data["post-bottom"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_post_bottom_ad_sizes)); + data["post-bottom"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_post_bottom_ad_sizes)); + } + // } } diff --git a/assets/javascripts/discourse/templates/components/google-adsense.hbs b/assets/javascripts/discourse/templates/components/google-adsense.hbs index ca671d9..f98d0d7 100644 --- a/assets/javascripts/discourse/templates/components/google-adsense.hbs +++ b/assets/javascripts/discourse/templates/components/google-adsense.hbs @@ -12,7 +12,6 @@ {{else}} -

Google Adsense

Date: Tue, 1 Sep 2015 11:27:31 +1000 Subject: [PATCH 2/4] Added trust levels to site settings for dfp --- .../discourse/components/google-dfp-ad.js.es6 | 14 ++++++++------ .../templates/components/google-dfp-ad.hbs | 2 -- config/locales/server.en.yml | 1 + config/settings.yml | 4 ++++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 b/assets/javascripts/discourse/components/google-dfp-ad.js.es6 index acb92f5..a4f5f5f 100755 --- a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 +++ b/assets/javascripts/discourse/components/google-dfp-ad.js.es6 @@ -3,6 +3,7 @@ import loadScript from 'discourse/lib/load-script'; var const_width = ''; var const_height = ''; +var currentUser = Discourse.User.current(); var _loaded = false, _promise = null; @@ -91,7 +92,7 @@ function loadGoogle(settings) { } googletag.cmd.push(function() { - if (settings.dfp_topic_list_top_code && !settings.dfp_show_topic_list_top && settings.topic_list_top_ad_sizes) { + if (settings.dfp_topic_list_top_code && !settings.dfp_show_topic_list_top && settings.topic_list_top_ad_sizes && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level))) { const_width = parseInt(splitWidthInt(settings.topic_list_top_ad_sizes)); const_height = parseInt(splitHeightInt(settings.topic_list_top_ad_sizes)); if (Discourse.Mobile.mobileView) { @@ -99,11 +100,12 @@ function loadGoogle(settings) { } else { googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_list_top_code, [parseInt(splitWidthInt(settings.topic_list_top_ad_sizes)), parseInt(splitHeightInt(settings.topic_list_top_ad_sizes))], 'div-gpt-ad-topic-list-top') - custom_targeting((keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_key_code)), (keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_value_code))) - googletag.addService(googletag.pubads()); + .setTargeting('gender', ['female']) + //custom_targeting((keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_key_code)), (keyParse(Discourse.SiteSettings.dfp_target_topic_list_top_value_code))) + .addService(googletag.pubads()); } } - if (settings.dfp_topic_above_post_stream_code && !settings.dfp_show_topic_above_post_stream && settings.topic_above_post_stream_ad_sizes) { + if (settings.dfp_topic_above_post_stream_code && !settings.dfp_show_topic_above_post_stream && settings.topic_above_post_stream_ad_sizes && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level))) { const_width = parseInt(splitWidthInt(settings.topic_above_post_stream_ad_sizes)); const_height = parseInt(splitHeightInt(settings.topic_above_post_stream_ad_sizes)); if (Discourse.Mobile.mobileView) { @@ -116,7 +118,7 @@ function loadGoogle(settings) { .addService(googletag.pubads()); } } - if (settings.dfp_topic_above_suggested_code && !settings.dfp_show_topic_above_suggested && settings.topic_above_suggested_ad_sizes) { + if (settings.dfp_topic_above_suggested_code && !settings.dfp_show_topic_above_suggested && settings.topic_above_suggested_ad_sizes && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level))) { const_width = parseInt(splitWidthInt(settings.topic_above_suggested_ad_sizes)); const_height = parseInt(splitHeightInt(settings.topic_above_suggested_ad_sizes)); if (Discourse.Mobile.mobileView) { @@ -130,7 +132,7 @@ function loadGoogle(settings) { googletag.addService(googletag.pubads()); } } - if (settings.dfp_post_bottom_code && !settings.dfp_show_post_bottom && settings.post_bottom_ad_sizes) { + if (settings.dfp_post_bottom_code && !settings.dfp_show_post_bottom && settings.post_bottom_ad_sizes && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level))) { const_width = parseInt(splitWidthInt(settings.post_bottom_ad_sizes)); const_height = parseInt(splitHeightInt(settings.post_bottom_ad_sizes)); if (Discourse.Mobile.mobileView) { diff --git a/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs b/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs index 486c4b0..ca96b4c 100755 --- a/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs +++ b/assets/javascripts/discourse/templates/components/google-dfp-ad.hbs @@ -1,5 +1,3 @@ -

Google Ad Plugin Display

- {{#if site.mobileView}} {{#if loadedGoogletag}}
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 145ec12..38bd1a7 100755 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1,6 +1,7 @@ en: site_settings: dfp_publisher_id: "Put in your publisher id here" + dfp_through_trust_level: "Show your ads to users based on trust." dfp_show_topic_list_top: "Disable topic_list_top ad" dfp_topic_list_top_code: "Show leaderboard ad above topic lists." topic_list_top_ad_sizes: "Choose your ad size" diff --git a/config/settings.yml b/config/settings.yml index c19051d..99994e9 100755 --- a/config/settings.yml +++ b/config/settings.yml @@ -98,6 +98,10 @@ ad_plugin: dfp_publisher_id: client: true default: '' + dfp_through_trust_level: + client: true + default: 2 + enum: 'TrustLevelSetting' dfp_topic_list_top_code: client: true default: '' From 67141dd88477c652d4b0781c89e477f5c1205086 Mon Sep 17 00:00:00 2001 From: vi Date: Tue, 1 Sep 2015 13:20:52 +1000 Subject: [PATCH 3/4] Added mobile code to topic list top position to test --- .../components/google-adsense.js.es6 | 17 +++++++++---- .../discourse/components/google-dfp-ad.js.es6 | 2 +- .../templates/components/google-adsense.hbs | 2 +- config/locales/server.en.yml | 8 +++++++ config/settings.yml | 24 +++++++++++++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index dd53e38..16a0c91 100644 --- a/assets/javascripts/discourse/components/google-adsense.js.es6 +++ b/assets/javascripts/discourse/components/google-adsense.js.es6 @@ -3,6 +3,7 @@ import PageTracker from 'discourse/lib/page-tracker'; var ad_width = ''; var ad_height = ''; var ad_code = ''; +var ad_mobile_code = ''; var currentUser = Discourse.User.current(); var publisher_id = Discourse.SiteSettings.adsense_publisher_code; var preGoogleVars = null; @@ -66,12 +67,17 @@ var data = { "post-bottom" : {} } + if (Discourse.SiteSettings.adsense_publisher_code) { - // if ((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level)) { - if (Discourse.SiteSettings.adsense_topic_list_top_code && !Discourse.SiteSettings.adsense_show_topic_list_top && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { - data["topic-list-top"]["ad_code"] = Discourse.SiteSettings.adsense_topic_list_top_code; - data["topic-list-top"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); - data["topic-list-top"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); + if (!Discourse.SiteSettings.adsense_show_topic_list_top && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { + if (!Discourse.Mobile.mobileView && Discourse.SiteSettings.adsense_topic_list_top_code) { + data["topic-list-top"]["ad_code"] = Discourse.SiteSettings.adsense_topic_list_top_code; + data["topic-list-top"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); + data["topic-list-top"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes)); + } + if (Discourse.Mobile.mobileView && Discourse.SiteSettings.adsense_mobile_topic_list_top_code) { + data["topic-list-top"]["ad_mobile_code"] = Discourse.SiteSettings.adsense_mobile_topic_list_top_code; + } } if (Discourse.SiteSettings.adsense_topic_above_post_stream_code && !Discourse.SiteSettings.adsense_show_topic_above_post_stream && !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level))) { data["topic-above-post-stream"]["ad_code"] = Discourse.SiteSettings.adsense_topic_above_post_stream_code; @@ -104,6 +110,7 @@ export default Ember.Component.extend({ 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"] ); + this.set('ad_mobile_code', data[this.placement]["ad_mobile_code"] ); this._super(); }, diff --git a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 b/assets/javascripts/discourse/components/google-dfp-ad.js.es6 index a4f5f5f..f6ede75 100755 --- a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 +++ b/assets/javascripts/discourse/components/google-dfp-ad.js.es6 @@ -96,7 +96,7 @@ function loadGoogle(settings) { const_width = parseInt(splitWidthInt(settings.topic_list_top_ad_sizes)); const_height = parseInt(splitHeightInt(settings.topic_list_top_ad_sizes)); if (Discourse.Mobile.mobileView) { - googletag.defineSlot(settings.dfp_topic_list_top_code, [320,50], 'div-gpt-ad-topic-list-top').addService(googletag.pubads()); + googletag.defineSlot(settings.dfp_mobile_topic_list_top_code, [320,50], 'div-gpt-ad-topic-list-top').addService(googletag.pubads()); } else { googletag.defineSlot('/' + settings.dfp_publisher_id + '/' + settings.dfp_topic_list_top_code, [parseInt(splitWidthInt(settings.topic_list_top_ad_sizes)), parseInt(splitHeightInt(settings.topic_list_top_ad_sizes))], 'div-gpt-ad-topic-list-top') diff --git a/assets/javascripts/discourse/templates/components/google-adsense.hbs b/assets/javascripts/discourse/templates/components/google-adsense.hbs index f98d0d7..280fdcd 100644 --- a/assets/javascripts/discourse/templates/components/google-adsense.hbs +++ b/assets/javascripts/discourse/templates/components/google-adsense.hbs @@ -3,7 +3,7 @@ + data-ad-slot={{ad_mobile_code}}>