Skip to content

Commit

Permalink
Merge pull request #27286 from guardian/doml/adverts-on-podcasts
Browse files Browse the repository at this point in the history
Include adverts in podcasts for ad free users
  • Loading branch information
domlander authored Jul 8, 2024
2 parents 261ac31 + 741ebb8 commit bf42f39
Showing 1 changed file with 84 additions and 94 deletions.
178 changes: 84 additions & 94 deletions static/src/javascripts/projects/common/modules/audio/index.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,108 @@
import React, { Component } from 'react';
import { render } from 'react-dom';
import {
onConsentChange,
getConsentFor,
} from '@guardian/libs';
import { isAdFreeUser } from 'common/modules/commercial/user-features';
import { onConsentChange, getConsentFor } from '@guardian/libs';
import config from 'lib/config';
import { AudioPlayer } from './AudioPlayer';
import { sendToOphan, registerOphanListeners } from './utils';



class AudioContainer extends Component {
constructor(props) {
super(props);
this.state = {
acastConsent: false,
};
}
constructor(props) {
super(props);
this.state = {
acastConsent: false,
};
}

componentDidMount() {
onConsentChange(consentState => {
const acast = getConsentFor('acast', consentState);
this.setState({
acastConsent: acast,
});
});
}
componentDidMount() {
onConsentChange((consentState) => {
const acast = getConsentFor('acast', consentState);
this.setState({
acastConsent: acast,
});
});
}

render() {
const acastEnabled = config.get('switches.acast');
const isPodcast = config.get('page.isPodcast');
const sourceUrl =
acastEnabled &&
isPodcast &&
this.state.acastConsent &&
!isAdFreeUser()
? this.props.source.replace(
'https://',
'https://flex.acast.com/'
)
: this.props.source;
render() {
const isAcastEnabled = config.get('switches.acast');
const isPodcast = config.get('page.isPodcast');
const sourceUrl =
isAcastEnabled && isPodcast && this.state.acastConsent
? this.props.source.replace(
'https://',
'https://flex.acast.com/',
)
: this.props.source;

return (
<AudioPlayer
sourceUrl={sourceUrl}
mediaId={this.props.mediaId}
duration={this.props.duration}
/>
);
}
return (
<AudioPlayer
sourceUrl={sourceUrl}
mediaId={this.props.mediaId}
duration={this.props.duration}
/>
);
}
}

const getPillar = pillarClass => {
const pillarMatches = /pillar-([a-z]+)/g.exec(pillarClass);
const pillar = pillarMatches ? pillarMatches[1].toLowerCase() : 'news';
if (pillar === 'books' || pillar === 'arts') return 'culture';
return pillar;
const getPillar = (pillarClass) => {
const pillarMatches = /pillar-([a-z]+)/g.exec(pillarClass);
const pillar = pillarMatches ? pillarMatches[1].toLowerCase() : 'news';
if (pillar === 'books' || pillar === 'arts') return 'culture';
return pillar;
};

const supportsCSSGrid =
window.CSS && window.CSS.supports && window.CSS.supports('display', 'grid');
window.CSS && window.CSS.supports && window.CSS.supports('display', 'grid');

const init = () => {
const placeholder = document.getElementById(
'audio-component-container'
);
const article = document.getElementsByTagName('article')[0];
const placeholder = document.getElementById('audio-component-container');
const article = document.getElementsByTagName('article')[0];

if (placeholder && article) {
const source = placeholder.dataset.source;
const mediaId = placeholder.dataset.mediaId;
const downloadUrl = placeholder.dataset.downloadUrl;
const duration = placeholder.dataset.duration;
if (placeholder && article) {
const source = placeholder.dataset.source;
const mediaId = placeholder.dataset.mediaId;
const downloadUrl = placeholder.dataset.downloadUrl;
const duration = placeholder.dataset.duration;

sendToOphan(mediaId, 'ready');
sendToOphan(mediaId, 'ready');

const pillarClassName = Array.from(article.classList).filter(x =>
x.includes('pillar')
)[0];
const pillar = getPillar(pillarClassName);
const pillarClassName = Array.from(article.classList).filter((x) =>
x.includes('pillar'),
)[0];
const pillar = getPillar(pillarClassName);

render(
supportsCSSGrid ? (
<AudioContainer
source={source}
mediaId={mediaId}
downloadUrl={downloadUrl}
duration={duration}
pillar={pillar}
/>
) : (
<audio
src={source}
controls
data-media-id={mediaId}
ref={el => {
if (el) registerOphanListeners(el);
}}>
<track
src={source}
kind="captions"
srcLang="en"
label="English"
/>
<p>
Sorry your browser does not support audio, here is
<a href={source}>a link to the audio</a> instead.
</p>
</audio>
),
placeholder
);
}
render(
supportsCSSGrid ? (
<AudioContainer
source={source}
mediaId={mediaId}
downloadUrl={downloadUrl}
duration={duration}
pillar={pillar}
/>
) : (
<audio
src={source}
controls
data-media-id={mediaId}
ref={(el) => {
if (el) registerOphanListeners(el);
}}
>
<track
src={source}
kind="captions"
srcLang="en"
label="English"
/>
<p>
Sorry your browser does not support audio, here is
<a href={source}>a link to the audio</a> instead.
</p>
</audio>
),
placeholder,
);
}
};

export { init };

0 comments on commit bf42f39

Please sign in to comment.