-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27286 from guardian/doml/adverts-on-podcasts
Include adverts in podcasts for ad free users
- Loading branch information
Showing
1 changed file
with
84 additions
and
94 deletions.
There are no files selected for viewing
178 changes: 84 additions & 94 deletions
178
static/src/javascripts/projects/common/modules/audio/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |