FIX: handle no ad size given for AdSense and DFP ad units

FIX: handle no ad size given for AdSense and DFP ad units

For AdSense, assume responsive size. For DFP, don’t attempt to render the ad unit.

diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6
index 168ee17..4ef2b3a 100644
--- a/assets/javascripts/discourse/components/google-adsense.js.es6
+++ b/assets/javascripts/discourse/components/google-adsense.js.es6
@@ -14,14 +14,24 @@ function parseAdWidth(value) {
   if (value === "responsive") {
     return "auto";
   }
-  return `${parseInt(value.substring(0, 3).trim())}px`;
+  const w = parseInt(value.substring(0, 3).trim());
+  if (isNaN(w)) {
+    return "auto";
+  } else {
+    return `${w}px`;
+  }
 }
 
 function parseAdHeight(value) {
   if (value === "responsive") {
     return "auto";
   }
-  return `${parseInt(value.substring(4, 7).trim())}px`;
+  const h = parseInt(value.substring(4, 7).trim());
+  if (isNaN(h)) {
+    return "auto";
+  } else {
+    return `${h}px`;
+  }
 }
 
 function loadAdsense() {
diff --git a/assets/javascripts/discourse/components/google-dfp-ad.js.es6 b/assets/javascripts/discourse/components/google-dfp-ad.js.es6
index 74e75b4..4f5df35 100755
--- a/assets/javascripts/discourse/components/google-dfp-ad.js.es6
+++ b/assets/javascripts/discourse/components/google-dfp-ad.js.es6
@@ -126,10 +126,14 @@ function getWidthAndHeight(placement, settings, isMobile) {
     renderCounts[placement] += 1;
   }
 
-  return {
+  const sizeObj = {
     width: parseInt(splitWidthInt(size)),
     height: parseInt(splitHeightInt(size))
   };
+
+  if (!isNaN(sizeObj.width) && !isNaN(sizeObj.height)) {
+    return sizeObj;
+  }
 }
 
 function defineSlot(
@@ -290,21 +294,24 @@ export default AdComponent.extend({
     "showToTrustLevel",
     "showToGroups",
     "showAfterPost",
-    "showOnCurrentPage"
+    "showOnCurrentPage",
+    "size"
   )
   showAd(
     publisherId,
     showToTrustLevel,
     showToGroups,
     showAfterPost,
-    showOnCurrentPage
+    showOnCurrentPage,
+    size
   ) {
     return (
       publisherId &&
       showToTrustLevel &&
       showToGroups &&
       showAfterPost &&
-      showOnCurrentPage
+      showOnCurrentPage &&
+      size
     );
   },

GitHub sha: 62888e73

1 Like