Skip to content

Commit

Permalink
Merge pull request #146 from reuters-graphics/ads-fix
Browse files Browse the repository at this point in the history
Ads fix
  • Loading branch information
hobbes7878 authored Mar 15, 2024
2 parents e81349a + e6436dd commit 4d133ce
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 214 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@storybook/theming": "^7.4.2",
"@sveltejs/vite-plugin-svelte": "^2.4.1",
"@tsconfig/svelte": "^4.0.1",
"@types/google-publisher-tag": "^1.20240219.0",
"@types/gtag.js": "^0.0.12",
"@types/mdx": "^2.0.5",
"@types/proper-url-join": "^2.1.1",
Expand Down
31 changes: 31 additions & 0 deletions src/components/AdSlot/@types/ads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export type AdType =
| 'leaderboard'
| 'sponsorlogo'
| 'native'
| 'mpu'
| 'billboard';

export type PlacementName =
| 'reuters_desktop_leaderboard_atf'
| 'reuters_mobile_leaderboard'
| 'reuters_desktop_native_1'
| 'reuters_mobile_mpu_1'
| 'reuters_sponsorlogo'
| 'reuters_billboard_desktop';

export type DesktopPlacementName =
| 'reuters_desktop_leaderboard_atf'
| 'reuters_desktop_native_1'
| 'reuters_sponsorlogo'
| 'reuters_billboard_desktop';

export type MobilePlacementName<DesktopPlacementName extends string> =
DesktopPlacementName extends 'reuters_desktop_leaderboard_atf'
? 'reuters_mobile_leaderboard'
: DesktopPlacementName extends 'reuters_desktop_native_1'
? 'reuters_mobile_mpu_1'
: DesktopPlacementName extends 'reuters_sponsorlogo'
? 'reuters_sponsorlogo'
: DesktopPlacementName extends 'reuters_billboard_desktop'
? 'reuters_mobile_mpu_1'
: never;
35 changes: 3 additions & 32 deletions src/components/AdSlot/AdScripts.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,14 @@
import { onMount } from 'svelte';
import { loadBootstrap } from './adScripts/bootstrap';
import { loadScript } from './adScripts/loadScript';
import { throttle } from 'lodash-es';
let lastScroll = 0;
let showManagePreferences = true;
const togglePrefs = (on = true) => {
const btn = document.getElementById('ot-sdk-btn-floating');
if (!btn) return;
if (on) {
showManagePreferences = true;
btn.style.bottom = '';
} else {
showManagePreferences = false;
btn.style.bottom = '-5rem';
}
};
const handleScroll = () => {
if (lastScroll > window.scrollY) {
if (!showManagePreferences) {
togglePrefs(true);
}
} else {
if (showManagePreferences && window.scrollY > 250) {
togglePrefs(false);
}
}
lastScroll = window.scrollY;
};
import OneTrust from './OneTrust.svelte';
onMount(() => {
window.graphicsAdQueue = window.graphicsAdQueue || [];
loadScript(
'https://graphics.thomsonreuters.com/cdn/js/bootstrap.static.js',
loadBootstrap
);
window.addEventListener('scroll', throttle(handleScroll, 250), {
passive: true,
});
});
</script>

Expand All @@ -60,3 +29,5 @@
/>
<link rel="stylesheet" href="https://a.pub.network/reuters-com/cls.css" />
</svelte:head>

<OneTrust />
46 changes: 0 additions & 46 deletions src/components/AdSlot/AdSlot.stories.svelte

This file was deleted.

123 changes: 13 additions & 110 deletions src/components/AdSlot/AdSlot.svelte
Original file line number Diff line number Diff line change
@@ -1,126 +1,29 @@
<script lang="ts">
/** ✏️ DOCUMENT your chart's props using TypeScript and JSDoc comments like below! */
type PlacementName =
// Disabling leaderboard for now...
// | 'reuters_desktop_leaderboard_atf'
// | 'reuters_mobile_leaderboard'
| 'reuters_desktop_native_1'
| 'reuters_mobile_mpu_1'
| 'reuters_sponsorlogo'
| 'reuters_desktop_canvas';
import type { PlacementName, AdType } from './@types/ads';
import { onMount } from 'svelte';
import { getRandomAdId } from './utils';
/**
* The unique placement name from FreeStar dashboard.
* @required
*/
export let placementName: PlacementName = 'reuters_desktop_native_1';
export let placementName: PlacementName;
export let adType: AdType;
/**
* The unique slot Id from FreeStar dashboard.
* @TODO Unclear at what level this bit of config is used with placements...
*/
export let dataFreestarAd: string = '__970x250';
/** Add an ID to target with SCSS. */
export let id: string = '';
/** Add a class to target with SCSS. */
let cls: string = 'my-12';
export { cls as class };
import { onMount } from 'svelte';
import Block from '../Block/Block.svelte';
const random4 = () =>
Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
const randomAdId = 'ad-' + random4() + random4();
const getAdType = (placementName: PlacementName) => {
switch (placementName) {
// case 'reuters_desktop_leaderboard_atf':
// case 'reuters_mobile_leaderboard':
// return 'leaderboard';
case 'reuters_sponsorlogo':
return 'Sponsorlogo';
case 'reuters_mobile_mpu_1':
return 'mpu';
default:
return 'native';
}
};
const adId = getRandomAdId();
onMount(() => {
const urlParams = new URLSearchParams(window.location.search);
const adstest = urlParams.get('adstest');
window.graphicsAdQueue = window.graphicsAdQueue || [];
window.graphicsAdQueue.push({
placementName,
slotId: randomAdId,
type: getAdType(placementName),
graphicId: window.location.pathname,
adstest,
slotId: adId,
targeting: {
div_id: adId,
type: adType,
},
});
});
</script>

<!-- @component `AdSlot` [Read the docs.](https://reuters-graphics.github.io/graphics-components/?path=/docs/components-AdSlot--default) -->
<Block id="{id}" class="freestar-adslot {cls}">
<div class="ad-block">
<div class="ad-label">Advertisement · Scroll to continue</div>
<div class="ad-container">
<div class="ad-slot__inner">
<div>
<div
data-freestar-ad="{dataFreestarAd || null}"
id="{randomAdId}"
></div>
</div>
</div>
</div>
</div>
</Block>

<style lang="scss">
div.ad-block {
border-bottom: 1px solid var(--theme-colour-brand-rules);
border-top: 1px solid var(--theme-colour-brand-rules);
div.ad-label {
font-family: Knowledge, 'Source Sans Pro', Arial, Helvetica, sans-serif;
font-size: 14px;
margin: 6px 0;
line-height: 1.333;
color: var(--theme-colour-text-secondary);
width: 100%;
text-align: center;
}
div.ad-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 415px;
@media (max-width: 767.9px) {
min-height: 320px;
}
div.ad-slot__inner {
margin: auto 0;
width: 100%;
max-width: 100%;
flex: unset;
& > div {
display: block;
div[data-freestar-ad] {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
}
}
}
}
</style>
<div data-freestar-ad="{dataFreestarAd || null}" id="{adId}"></div>
29 changes: 29 additions & 0 deletions src/components/AdSlot/InlineAd.stories.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script>
import { Meta, Template, Story } from '@storybook/addon-svelte-csf';
// @ts-ignore
import componentDocs from './stories/docs/component.md?raw';
import AdScripts from './AdScripts.svelte';
import InlineAd from './InlineAd.svelte';
import { withComponentDocs } from '$docs/utils/withParams.js';
const meta = {
title: 'Components/InlineAd',
component: InlineAd,
...withComponentDocs(componentDocs),
};
</script>

<Meta title="Components/InlineAd" {...meta} />

<Template let:args>
<div>
<AdScripts />
<InlineAd />
<InlineAd />
</div>
</Template>

<Story name="Default" />
69 changes: 69 additions & 0 deletions src/components/AdSlot/InlineAd.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<script lang="ts">
import Block from '../Block/Block.svelte';
import { DesktopPlacementName } from './@types/ads';
import ResponsiveAd from './ResponsiveAd.svelte';
/** Add an ID to target with SCSS. */
export let id: string = '';
/** Add a class to target with SCSS. */
let cls: string = 'my-12';
export { cls as class };
const desktopPlacementName: DesktopPlacementName = 'reuters_desktop_native_1';
</script>

<!-- @component `InlineAd` [Read the docs.](https://reuters-graphics.github.io/graphics-components/?path=/docs/components-InlineAd--default) -->
<Block id="{id}" class="freestar-adslot {cls}">
<div class="ad-block">
<div class="ad-label">Advertisement · Scroll to continue</div>
<div class="ad-container">
<div class="ad-slot__inner">
<div>
<ResponsiveAd desktopPlacementName="{desktopPlacementName}" />
</div>
</div>
</div>
</div>
</Block>

<style lang="scss">
div.ad-block {
border-bottom: 1px solid var(--theme-colour-brand-rules);
border-top: 1px solid var(--theme-colour-brand-rules);
div.ad-label {
font-family: Knowledge, 'Source Sans Pro', Arial, Helvetica, sans-serif;
font-size: 14px;
margin: 6px 0;
line-height: 1.333;
color: var(--theme-colour-text-secondary);
width: 100%;
text-align: center;
}
div.ad-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 415px;
@media (max-width: 767.9px) {
min-height: 320px;
}
div.ad-slot__inner {
margin: auto 0;
width: 100%;
max-width: 100%;
flex: unset;
& > div {
display: block;
:global(div[data-freestar-ad]) {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
}
}
}
}
</style>
Loading

0 comments on commit 4d133ce

Please sign in to comment.