Skip to content

Commit

Permalink
feat(adTargeting): use SWR to share targetting
Browse files Browse the repository at this point in the history
This simplifies the logic of handling ad targetting
across all Islands by setting it once via SWR.
  • Loading branch information
mxdvl committed Jun 30, 2023
1 parent ed0e66f commit f2538d7
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 212 deletions.
4 changes: 0 additions & 4 deletions dotcom-rendering/src/components/ArticleBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type Props = {
format: ArticleFormat;
blocks: Block[];
pinnedPost?: Block;
adTargeting: AdTargeting;
host?: string;
pageId: string;
webTitle: string;
Expand Down Expand Up @@ -123,7 +122,6 @@ export const ArticleBody = ({
format,
blocks,
pinnedPost,
adTargeting,
host,
pageId,
webTitle,
Expand Down Expand Up @@ -183,7 +181,6 @@ export const ArticleBody = ({
format={format}
blocks={blocks}
pinnedPost={pinnedPost}
adTargeting={adTargeting}
host={host}
pageId={pageId}
webTitle={webTitle}
Expand Down Expand Up @@ -239,7 +236,6 @@ export const ArticleBody = ({
<ArticleRenderer
format={format}
elements={blocks[0] ? blocks[0].elements : []}
adTargeting={adTargeting}
host={host}
pageId={pageId}
webTitle={webTitle}
Expand Down
15 changes: 15 additions & 0 deletions dotcom-rendering/src/components/ArticlePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ArticleDesign } from '@guardian/libs';
import { brandAlt, focusHalo, neutral } from '@guardian/source-foundations';
import { StrictMode } from 'react';
import { DecideLayout } from '../layouts/DecideLayout';
import { buildAdTargeting } from '../lib/ad-targeting';
import { filterABTestSwitches } from '../model/enhance-switches';
import type { NavType } from '../model/extract-nav';
import type { FEArticleType } from '../types/frontend';
Expand All @@ -16,6 +17,7 @@ import { Island } from './Island';
import { Metrics } from './Metrics.importable';
import { ReaderRevenueDev } from './ReaderRevenueDev.importable';
import { SetABTests } from './SetABTests.importable';
import { SetAdTargeting } from './SetAdTargeting.importable';
import { SkipTo } from './SkipTo';

interface BaseProps {
Expand All @@ -39,6 +41,16 @@ interface AppProps extends BaseProps {
* */
export const ArticlePage = (props: WebProps | AppProps) => {
const { article, format, renderingTarget } = props;

const adTargeting = buildAdTargeting({
isAdFreeUser: article.isAdFreeUser,
isSensitive: article.config.isSensitive,
edition: article.config.edition,
section: article.config.section,
sharedAdTargeting: article.config.sharedAdTargeting,
adUnit: article.config.adUnit,
});

return (
<StrictMode>
<Global
Expand Down Expand Up @@ -102,6 +114,9 @@ export const ArticlePage = (props: WebProps | AppProps) => {
isDev={!!article.config.isDev}
/>
</Island>
<Island clientOnly={true}>
<SetAdTargeting adTargeting={adTargeting} />
</Island>
</>
)}
{renderingTarget === 'Apps' ? (
Expand Down
14 changes: 14 additions & 0 deletions dotcom-rendering/src/components/FrontPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { css, Global } from '@emotion/react';
import { brandAlt, focusHalo, neutral } from '@guardian/source-foundations';
import { StrictMode } from 'react';
import { FrontLayout } from '../layouts/FrontLayout';
import { buildAdTargeting } from '../lib/ad-targeting';
import { filterABTestSwitches } from '../model/enhance-switches';
import type { NavType } from '../model/extract-nav';
import type { DCRFrontType } from '../types/front';
Expand All @@ -12,6 +13,7 @@ import { FocusStyles } from './FocusStyles.importable';
import { Island } from './Island';
import { Metrics } from './Metrics.importable';
import { SetABTests } from './SetABTests.importable';
import { SetAdTargeting } from './SetAdTargeting.importable';
import { ShowHideContainers } from './ShowHideContainers.importable';
import { SkipTo } from './SkipTo';

Expand All @@ -29,6 +31,15 @@ type Props = {
* @param {NAVType} props.NAV - The article JSON data
* */
export const FrontPage = ({ front, NAV }: Props) => {
const adTargeting = buildAdTargeting({
isAdFreeUser: front.isAdFreeUser,
isSensitive: front.config.isSensitive,
edition: front.config.edition,
section: front.config.section,
sharedAdTargeting: front.config.sharedAdTargeting,
adUnit: front.config.adUnit,
});

return (
<StrictMode>
<Global
Expand Down Expand Up @@ -75,6 +86,9 @@ export const FrontPage = ({ front, NAV }: Props) => {
isDev={!!front.config.isDev}
/>
</Island>
<Island clientOnly={true}>
<SetAdTargeting adTargeting={adTargeting} />
</Island>
<FrontLayout front={front} NAV={NAV} />
</StrictMode>
);
Expand Down
48 changes: 0 additions & 48 deletions dotcom-rendering/src/components/LiveBlock.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export const VideoAsSecond = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -113,10 +109,6 @@ export const Title = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -183,10 +175,6 @@ export const Video = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -228,10 +216,6 @@ export const RichLink = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -264,10 +248,6 @@ export const FirstImage = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -326,10 +306,6 @@ export const ImageRoles = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -377,10 +353,6 @@ export const Thumbnail = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -414,10 +386,6 @@ export const ImageAndTitle = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -447,10 +415,6 @@ export const Updated = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -484,10 +448,6 @@ export const Contributor = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.News,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -519,10 +479,6 @@ export const NoAvatar = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.Opinion,
design: ArticleDesign.LiveBlog,
Expand Down Expand Up @@ -557,10 +513,6 @@ export const TitleAndContributor = () => {
return (
<Wrapper>
<LiveBlock
adTargeting={{
customParams: { sens: 'f', urlkw: [] },
adUnit: '',
}}
format={{
theme: ArticlePillar.Sport,
design: ArticleDesign.LiveBlog,
Expand Down
3 changes: 0 additions & 3 deletions dotcom-rendering/src/components/LiveBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type Props = {
block: Block;
pageId: string;
webTitle: string;
adTargeting: AdTargeting;
host?: string;
ajaxUrl: string;
isAdFreeUser: boolean;
Expand All @@ -26,7 +25,6 @@ export const LiveBlock = ({
block,
pageId,
webTitle,
adTargeting,
host = 'https://www.theguardian.com',
ajaxUrl,
isAdFreeUser,
Expand Down Expand Up @@ -68,7 +66,6 @@ export const LiveBlock = ({
key={index}
format={format}
element={element}
adTargeting={adTargeting}
ajaxUrl={ajaxUrl}
host={host}
index={index}
Expand Down
4 changes: 0 additions & 4 deletions dotcom-rendering/src/components/LiveBlogBlocksAndAdverts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { LiveBlock } from './LiveBlock';
type Props = {
format: ArticleFormat;
blocks: Block[];
adTargeting: AdTargeting;
pinnedPost?: Block;
host?: string;
pageId: string;
Expand All @@ -26,7 +25,6 @@ export const LiveBlogBlocksAndAdverts = ({
format,
blocks,
pinnedPost,
adTargeting,
host,
pageId,
webTitle,
Expand All @@ -47,7 +45,6 @@ export const LiveBlogBlocksAndAdverts = ({
block={block}
pageId={pageId}
webTitle={webTitle}
adTargeting={adTargeting}
host={host}
ajaxUrl={ajaxUrl}
isLiveUpdate={isLiveUpdate}
Expand Down Expand Up @@ -93,7 +90,6 @@ export const LiveBlogBlocksAndAdverts = ({
block={block}
pageId={pageId}
webTitle={webTitle}
adTargeting={adTargeting}
host={host}
ajaxUrl={ajaxUrl}
isLiveUpdate={isLiveUpdate}
Expand Down
3 changes: 0 additions & 3 deletions dotcom-rendering/src/components/MainMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ type Props = {
format: ArticleFormat;
elements: FEElement[];
hideCaption?: boolean;
adTargeting?: AdTargeting;
starRating?: number;
host?: string;
pageId: string;
Expand All @@ -80,7 +79,6 @@ export const MainMedia = ({
elements,
format,
hideCaption,
adTargeting,
starRating,
host,
pageId,
Expand All @@ -98,7 +96,6 @@ export const MainMedia = ({
key={index}
format={format}
element={element}
adTargeting={adTargeting}
ajaxUrl={ajaxUrl}
host={host}
index={index}
Expand Down
Loading

0 comments on commit f2538d7

Please sign in to comment.