Skip to content

Commit

Permalink
Implement banner ads for articles (#66)
Browse files Browse the repository at this point in the history
* Rename advertisements to MixedAdvertisements

* Add Banner Ads that show up occasionally on top of article

* Disable ads on humor and opinions articles as per charter

* Slightly nerf ad chances
  • Loading branch information
leomet07 authored Jun 4, 2024
1 parent 009a88e commit d300b64
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 35 deletions.
26 changes: 26 additions & 0 deletions advertisements/BannerAdvertisement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Image from "next/image";
import Link from "next/link";
import banner_advertisements from "./BannerAdvertisements";
import styles from "../styles/Advertisement.module.css";

const BannerAdvertisement = (props: { index: number; show_ad: boolean }) => {
let index = props.index % banner_advertisements.length;

return (
<Link
href={banner_advertisements[index].url}
style={{ display: props.show_ad ? "block" : "none" }}
>
<div id={styles.parent_div}>
<Image
id={styles.image}
src={banner_advertisements[index].image_src}
fill
alt={banner_advertisements[index].name + " ad"}
/>
</div>
</Link>
);
};

export default BannerAdvertisement;
8 changes: 8 additions & 0 deletions advertisements/BannerAdvertisements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const BannerAdvertisements = [
{
name: "Prompt College Application and Essay Coaching",
image_src: "/images/ads/prompt_banner.png",
url: "https://www.myprompt.com/",
},
];
export default BannerAdvertisements;
23 changes: 23 additions & 0 deletions advertisements/MixedAdvertisement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Image from "next/image";
import Link from "next/link";
import mixed_advertisements from "./MixedAdvertisements";
import styles from "../styles/Advertisement.module.css";

const MixedAdvertisement = (props: { index: number }) => {
let index = props.index % mixed_advertisements.length;

return (
<Link href={mixed_advertisements[index].url}>
<div id={styles.parent_div}>
<Image
id={styles.image}
src={mixed_advertisements[index].image_src}
fill
alt={mixed_advertisements[index].name + " ad"}
/>
</div>
</Link>
);
};

export default MixedAdvertisement;
4 changes: 2 additions & 2 deletions advertisements.ts → advertisements/MixedAdvertisements.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const advertisements = [
const MixedAdvertisements = [
{
name: "Kweller Prep",
image_src: "/images/ads/kweller_prep.png",
Expand All @@ -15,4 +15,4 @@ const advertisements = [
url: "http://www.collegeessaytutors.com/",
},
];
export default advertisements;
export default MixedAdvertisements;
23 changes: 0 additions & 23 deletions components/Advertisement.tsx

This file was deleted.

18 changes: 11 additions & 7 deletions components/MixedArticleDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import styles from "../styles/MixedArticleDisplay.module.css";
import groupByImageExists from "../utils/groupArticles";
import generate_contributors_jsx from "./GenerateContributorsJSX";
import { useCallback, useEffect, useState, Fragment } from "react";
import Advertisment from "./Advertisement";
import advertisements from "../advertisements";
import MixedAdvertisment from "../advertisements/MixedAdvertisement";

function CenterArticle(props: {
article: ReceivedArticle;
Expand Down Expand Up @@ -193,7 +192,7 @@ export default function MixedArticleDisplay(props: {
/>
{index % ad_spacing == 0 ? (
<div className={styles.ad_parent}>
<Advertisment
<MixedAdvertisment
index={index / ad_spacing}
/>
</div>
Expand Down Expand Up @@ -249,8 +248,11 @@ export default function MixedArticleDisplay(props: {
/>
{index % ad_spacing == 1 ? (
<div className={styles.ad_parent}>
<Advertisment
index={Math.floor(index / ad_spacing) + 2}
<MixedAdvertisment
index={
Math.floor(index / ad_spacing) +
2
}
/>
</div>
) : (
Expand All @@ -273,8 +275,10 @@ export default function MixedArticleDisplay(props: {
/>
{index % ad_spacing == 1 && index != 0 ? (
<div className={styles.ad_parent}>
<Advertisment
index={Math.floor(index / ad_spacing)}
<MixedAdvertisment
index={Math.floor(
index / ad_spacing
)}
/>
</div>
) : (
Expand Down
24 changes: 22 additions & 2 deletions pages/article/[article_slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import ShareButton from "../../components/ShareButton";
import Link from "next/link";
import generate_contributors_jsx from "../../components/GenerateContributorsJSX";
import { generateMetaTags } from "../../utils/generateMetaTags";
import BannerAdvertisements from "../../advertisements/BannerAdvertisements";
import BannerAdvertisement from "../../advertisements/BannerAdvertisement";

interface Props {
article: ReceivedArticle;
banner_ad_index: number;
show_ad: boolean; // temporary to not have the same banner ad on every article
}

function Article(props: Props) {
Expand Down Expand Up @@ -47,6 +51,8 @@ function Article(props: Props) {

const providers = ["facebook", "twitter", "linkedin", "email"];

const isAdvertisingAllowed: boolean = section_id != 2 && section_id != 4; // forbid ads on Opinions and Humor as per charter

return (
<div>
<Head>
Expand Down Expand Up @@ -77,6 +83,15 @@ function Article(props: Props) {
</Head>

<main id={styles.main}>
{isAdvertisingAllowed && (
<div className={styles.advertisements}>
<BannerAdvertisement
index={props.banner_ad_index}
show_ad={props.show_ad}
/>
</div>
)}

<article id={styles.article}>
<p id={styles.section} className="discrete-link">
<Link
Expand Down Expand Up @@ -160,7 +175,6 @@ function Article(props: Props) {

{/* <RecommendedArticles /> */}
</article>
<div id={styles.advertisements}></div>
</main>
</div>
);
Expand All @@ -174,7 +188,13 @@ export async function getServerSideProps(context: NextPageContext) {
let article = await get_article_by_slug(article_slug);
if (article) {
return {
props: { article: JSON.parse(JSON.stringify(article)) },
props: {
article: JSON.parse(JSON.stringify(article)),
banner_ad_index: Math.floor(
Math.random() * BannerAdvertisements.length
),
show_ad: Math.random() * 100 < 30, // 30% chance of showing the ad
},
};
} else {
return {
Expand Down
Binary file added public/images/ads/prompt_banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion styles/[article_slug].module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#main {
width: 95vw;
max-width: 900px;
max-width: 800px;
padding-top: 1rem;
margin: 0 auto;
}
Expand Down Expand Up @@ -136,3 +136,7 @@
width: 100%;
font-style: italic;
}

.advertisements {
height: fit-content;
}

0 comments on commit d300b64

Please sign in to comment.