FIX: show non-restricted ads instead of not showing ads at all (#213)

In some cases where there were category restricted house ads we were not
showing ads on reload. This commit filter out all the ads that should
not be shown on current page, leaving only allowed ads. So now we'll
show ads on every reload in all the cases.

Internal ticket: t130920
This commit is contained in:
Arpit Jalan 2024-06-27 22:13:54 +05:30 committed by GitHub
parent a38fbd0935
commit 9b72130fdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 18 deletions

View File

@ -72,23 +72,22 @@ export default AdComponent.extend({
placement = this.get("placement").replace(/-/g, "_"), placement = this.get("placement").replace(/-/g, "_"),
adNames = this.adsNamesForSlot(placement); adNames = this.adsNamesForSlot(placement);
if (adNames.length > 0) { // filter out ads that should not be shown on the current page
const filteredAds = adNames.filter((adName) => {
const ad = houseAds.creatives[adName];
return (
!ad.category_ids?.length ||
ad.category_ids.includes(this.currentCategoryId)
);
});
if (filteredAds.length > 0) {
if (!adIndex[placement]) { if (!adIndex[placement]) {
adIndex[placement] = 0; adIndex[placement] = 0;
} }
let ad = houseAds.creatives[adNames[adIndex[placement]]] || ""; let ad = houseAds.creatives[filteredAds[adIndex[placement]]] || "";
adIndex[placement] = (adIndex[placement] + 1) % adNames.length; adIndex[placement] = (adIndex[placement] + 1) % filteredAds.length;
// check if the ad includes the current category, or if no category restrictions are set for the ad
// otherwise don't show it
if (
!ad.category_ids?.length ||
ad.category_ids.includes(this.currentCategoryId)
) {
return ad.html; return ad.html;
} }
} else {
return "";
}
}, },
adsNamesForSlot(placement) { adsNamesForSlot(placement) {
@ -120,9 +119,21 @@ export default AdComponent.extend({
if (adIndex.topic_list_top === null) { if (adIndex.topic_list_top === null) {
// start at a random spot in the ad inventory // start at a random spot in the ad inventory
const houseAds = this.site.get("house_creatives");
Object.keys(adIndex).forEach((placement) => { Object.keys(adIndex).forEach((placement) => {
const adNames = this.adsNamesForSlot(placement); const adNames = this.adsNamesForSlot(placement);
adIndex[placement] = Math.floor(Math.random() * adNames.length); if (adNames.length === 0) {
return;
}
// filter out ads that should not be shown on the current page
const filteredAds = adNames.filter((adName) => {
const ad = houseAds.creatives[adName];
return (
!ad.category_ids?.length ||
ad.category_ids.includes(this.currentCategoryId)
);
});
adIndex[placement] = Math.floor(Math.random() * filteredAds.length);
}); });
} }

View File

@ -197,7 +197,7 @@ acceptance(
assert assert
.dom(".h-topic-list") .dom(".h-topic-list")
.doesNotExist( .doesNotExist(
"ad is not displayed because the current category id is included in the ad category_ids" "ad is not displayed because the current category id is not included in the ad category_ids"
); );
}); });
} }
@ -229,7 +229,7 @@ acceptance(
assert assert
.dom(".h-topic-list") .dom(".h-topic-list")
.doesNotExist( .doesNotExist(
"ad is not displayed because the current category id is included in the ad category_ids" "ad is not displayed because the current category id is not included in the ad category_ids"
); );
}); });
} }
@ -261,7 +261,7 @@ acceptance(
assert assert
.dom(".h-topic-list") .dom(".h-topic-list")
.doesNotExist( .doesNotExist(
"ad is not displayed because the current category id is included in the ad category_ids" "ad is not displayed because the current category id is not included in the ad category_ids"
); );
}); });
} }
@ -288,7 +288,7 @@ acceptance(
}, },
}); });
test("hides ad to anon users when current category id is not included in ad category_ids", async (assert) => { test("shows ad to anon users when current category id is included in ad category_ids", async (assert) => {
await visit("/c/bug/1"); await visit("/c/bug/1");
assert assert
.dom(".h-topic-list") .dom(".h-topic-list")
@ -298,3 +298,34 @@ acceptance(
}); });
} }
); );
acceptance(
"House Ads | Category and Group Permissions | Anonymous | Show non-restricted ads",
function (needs) {
needs.settings({
no_ads_for_categories: "",
});
needs.site({
house_creatives: {
settings: {
topic_list_top: "Topic List Top One|Topic List Top Two",
},
creatives: {
"Topic List Top One": {
html: "<div class='h-topic-list-one'>TOPIC LIST TOP ONE</div>",
category_ids: [2],
},
"Topic List Top Two": {
html: "<div class='h-topic-list-two'>TOPIC LIST TOP TWO</div>",
category_ids: [],
},
},
},
});
test("shows non-restricted ad to anon users", async (assert) => {
await visit("/c/bug/1");
assert.dom(".h-topic-list-two").exists("non-restricted ad is displayed");
});
}
);