From 04525d0cf1463f6760fc83ebabdd390e7b14b272 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 5 Jun 2019 17:00:47 -0400 Subject: [PATCH] FIX: dfp category targeting params DFP category targeting param wasn't always being updated on topic list pages. Also remove dependency on a category param being passed in to the component from a template so that we can now place dfp ads in places that don't have access to the current category and targeting will still work. --- .../discourse/components/ad-component.js.es6 | 5 ++++ .../discourse/components/google-dfp-ad.js.es6 | 25 ++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/assets/javascripts/discourse/components/ad-component.js.es6 b/assets/javascripts/discourse/components/ad-component.js.es6 index 29be5f4..f527b44 100644 --- a/assets/javascripts/discourse/components/ad-component.js.es6 +++ b/assets/javascripts/discourse/components/ad-component.js.es6 @@ -8,6 +8,11 @@ export default Ember.Component.extend({ "router.currentRoute.parent.attributes.category_id" ), + currentCategorySlug: Ember.computed.or( + "router.currentRoute.attributes.category.slug", + "router.currentRoute.parent.attributes.category.slug" + ), + @computed("currentUser.groups") showToGroups(groups) { const currentUser = Discourse.User.current(); diff --git a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 b/assets/javascripts/discourse/components/google-dfp-ad.js.es6 index da99b75..6453f7a 100755 --- a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 +++ b/assets/javascripts/discourse/components/google-dfp-ad.js.es6 @@ -266,22 +266,23 @@ export default AdComponent.extend({ return this.isNthPost(parseInt(this.siteSettings.dfp_nth_post_code)); }, - @observes("refreshOnChange") - refreshAd() { - var slot = ads[this.get("divId")]; + @on("didUpdate") + updated() { + if (this.get("listLoading")) { + return; + } + + let slot = ads[this.get("divId")]; if (!(slot && slot.ad)) { return; } - var self = this, - ad = slot.ad; + let ad = slot.ad, + categorySlug = this.get("currentCategorySlug"); - if (this.get("loadedGoogletag") && this.get("refreshOnChange")) { + if (this.get("loadedGoogletag")) { window.googletag.cmd.push(function() { - ad.setTargeting( - "discourse-category", - self.get("category") ? self.get("category") : "0" - ); + ad.setTargeting("discourse-category", categorySlug || "0"); window.googletag.pubads().refresh([ad]); }); } @@ -293,7 +294,7 @@ export default AdComponent.extend({ return; } - var self = this; + let self = this; loadGoogle(this.siteSettings).then(function() { self.set("loadedGoogletag", true); window.googletag.cmd.push(function() { @@ -306,7 +307,7 @@ export default AdComponent.extend({ if (slot && slot.ad) { slot.ad.setTargeting( "discourse-category", - self.get("category") ? self.get("category") : "0" + self.get("currentCategorySlug") || "0" ); window.googletag.display(self.get("divId")); window.googletag.pubads().refresh([slot.ad]);