Skip to content

Commit 3a6e6a6

Browse files
authored
Use footer text content ad for unknown themes (#562)
This is much less intrusive than the stickybox, and allows us to still have an ad injected, but not cover the content for users when this happens. <img width="1436" alt="Screenshot 2025-03-06 at 7 39 00 PM" src="https://github.com/user-attachments/assets/6850c4f5-b136-4dbb-af0f-8d8f2b47ed6f" /> Replaces #447 Closes #311
1 parent 57d34df commit 3a6e6a6

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/ethicalads.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,23 @@ export class EthicalAdsAddon extends AddonBase {
196196
if (elementToAppend) {
197197
elementToAppend.append(placement);
198198
}
199-
} else if (window.innerWidth > 1300) {
200-
// https://ethical-ad-client.readthedocs.io/en/latest/#stickybox
201-
placement.setAttribute("data-ea-type", "image");
202-
placement.setAttribute("data-ea-style", "stickybox");
203-
this.addEaPlacementToElement(placement);
204-
// `document.body` here is not too much relevant, since we are going to
205-
// use this selector only for a floating stickybox ad
206-
const elementInsertBefore = document.body;
207-
elementInsertBefore.insertBefore(
208-
placement,
209-
elementInsertBefore.lastChild,
210-
);
199+
} else {
200+
// Default to a text ad appended to the root selector when no known placement found
201+
placement.setAttribute("data-ea-type", "text");
202+
// TODO: Check this placement on the dashboard,
203+
// and see how this is performing.
204+
const docToolName = docTool.getDocumentationTool();
205+
const idSuffix = docToolName ? `-${docToolName}` : "";
206+
placement.setAttribute("id", `readthedocs-ea-text-footer${idSuffix}`);
207+
208+
const rootSelector = docTool.getRootSelector();
209+
const rootElement = document.querySelector(rootSelector);
210+
211+
if (rootElement) {
212+
rootElement.append(placement);
213+
} else {
214+
console.debug("Could not find root element to append ad");
215+
}
211216
}
212217
}
213218

@@ -239,6 +244,11 @@ export class EthicalAdsAddon extends AddonBase {
239244
}
240245

241246
elementAboveTheFold(element) {
247+
// Return false if element doesn't exist
248+
if (!element) {
249+
return false;
250+
}
251+
242252
// Determine if this element would be above the fold.
243253
// If this is off screen, instead create an ad in the footer.
244254
// Assumes the ad would be AD_SIZE pixels high.

tests/__snapshots__/ethicalads.test.snap.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ snapshots["EthicalAd addon ad placement injected"] =
2020
data-ea-keywords="docs|data-science"
2121
data-ea-manual="true"
2222
data-ea-publisher="readthedocs"
23-
data-ea-style="stickybox"
24-
data-ea-type="image"
25-
id="readthedocs-ea"
23+
data-ea-type="text"
24+
id="readthedocs-ea-text-footer"
2625
>
2726
</div>
2827
`;

tests/ethicalads.test.html

+10-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,17 @@
6666
});
6767

6868
it("ad placement injected", async () => {
69-
// We need the width to be bigger than 1300px for the ad to be injected
70-
await setViewport({ width: 1600, height: 640 });
71-
7269
const addon = new ethicalads.EthicalAdsAddon(config);
73-
const element = document.querySelector("#readthedocs-ea");
70+
const element = document.querySelector(
71+
"#readthedocs-ea-text-footer",
72+
);
73+
74+
expect(element).to.exist;
75+
expect(element).to.have.attribute("data-ea-type", "text");
76+
expect(element).to.have.attribute(
77+
"data-ea-publisher",
78+
"readthedocs",
79+
);
7480
await expect(element).dom.to.equalSnapshot();
7581
});
7682

0 commit comments

Comments
 (0)