Skip to content

Commit 340f388

Browse files
committed
refactor: use gpt ads instead of adsense automatic implementation
1 parent 5d2fed7 commit 340f388

File tree

4 files changed

+59
-94
lines changed

4 files changed

+59
-94
lines changed

docs/docusaurus.config.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ const config = {
3030
},
3131

3232
scripts: [
33-
// {
34-
// src: 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7420210265158247',
35-
// async: true,
36-
// },
37-
// {
38-
// src: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
39-
// async: true,
40-
// },
41-
// {
42-
// src: '/js/gpt.js',
43-
// async: true,
44-
// },
33+
{
34+
src: 'https://securepubads.g.doubleclick.net/tag/js/gpt.js',
35+
async: true,
36+
},
37+
{
38+
src: '/js/gpt.js',
39+
async: true,
40+
},
4541
],
4642

4743
presets: [

docs/src/theme/Footer/index.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/src/theme/TOC/index.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1-
// import React, { useEffect } from 'react'
2-
import React from 'react'
1+
import React, { useEffect } from 'react'
32
import TOC from '@theme-original/TOC'
43

5-
// import styles from './index.module.css'
4+
import styles from './index.module.css'
65

7-
// const AdsContainerElement = ({ id }) => {
8-
// useEffect(() => {
9-
// if (typeof window !== 'undefined') {
10-
// window.dispatchEvent(new CustomEvent('AdsContainerMounted', { detail: { id } }))
11-
// }
12-
// }, [])
6+
const AdsContainerElement = ({ id }) => {
7+
useEffect(() => {
8+
if (typeof window !== 'undefined') {
9+
window.dispatchEvent(new CustomEvent('AdsContainerMounted', { detail: { id } }))
10+
}
11+
}, [])
1312

14-
// return <div id={id} className={styles.adsContainer} />
15-
// }
13+
return <div id={id} className={styles.adsContainer} />
14+
}
1615

1716
export default function TOCWrapper(props) {
1817
return (
1918
<>
2019
<TOC {...props} />
21-
{/* <AdsContainerElement id="right-sidebar-ads" /> */}
22-
<ins
23-
className="adsbygoogle"
24-
style={{ display: 'block', position: 'fixed' }}
25-
data-ad-client="ca-pub-7420210265158247"
26-
data-ad-slot="8711379942"
27-
data-ad-format="auto"
28-
data-full-width-responsive="true"
29-
/>
20+
<AdsContainerElement id="right-sidebar-ads" />
3021
</>
3122
)
3223
}

docs/static/js/gpt.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
window.onload = () => {
2-
;(window.adsbygoogle = window.adsbygoogle || []).push({})
1+
// prevent loading ads in localhost
2+
const development = window.location.host !== 'react-tooltip.com'
3+
// use sandbox ads to test in localhost
4+
const slotPath = development
5+
? '/6355419/Travel/Europe/France/Paris'
6+
: '/22862227500/desktop-sidebar-right'
7+
8+
window.reactTooltipAds = {
9+
initialized: {},
310
}
411

5-
// window.reactTooltipAds = {
6-
// initialized: {},
7-
// }
8-
// window.googletag = window.googletag || { cmd: [] }
9-
10-
// window.googletag.cmd.push(function () {
11-
// window.googletag
12-
// .defineSlot('/6355419/Travel/Europe/France/Paris', [300, 250], 'right-sidebar-ads')
13-
// .addService(window.googletag.pubads())
14-
// window.googletag.enableServices()
15-
// })
16-
17-
// const handleLoadAds = (event) => {
18-
// const { id } = event.detail
19-
20-
// if (window.innerWidth < 1024) {
21-
// return
22-
// }
23-
24-
// console.log('loading ads for container: ', id)
25-
// window.googletag.cmd.push(function () {
26-
// if (window.reactTooltipAds.initialized[id]) {
27-
// window.googletag.cmd.push(function () {
28-
// window.googletag.pubads().refresh()
29-
// })
30-
31-
// window.googletag.display(id)
32-
// } else {
33-
// window.googletag.display(id)
34-
// window.reactTooltipAds.initialized[id] = true
35-
// }
36-
37-
// console.log('displayed: ', id)
38-
// })
39-
// }
40-
41-
// window.addEventListener('AdsContainerMounted', handleLoadAds)
12+
window.googletag = window.googletag || { cmd: [] }
13+
14+
window.googletag.cmd.push(function () {
15+
window.googletag
16+
.defineSlot(slotPath, [300, 250], 'right-sidebar-ads')
17+
.addService(window.googletag.pubads())
18+
window.googletag.enableServices()
19+
})
20+
21+
const handleLoadAds = (event) => {
22+
const { id } = event.detail
23+
24+
if (window.innerWidth < 1024) {
25+
return
26+
}
27+
28+
window.googletag.cmd.push(function () {
29+
if (window.reactTooltipAds.initialized[id]) {
30+
window.googletag.cmd.push(function () {
31+
window.googletag.pubads().refresh()
32+
})
33+
34+
window.googletag.display(id)
35+
} else {
36+
window.googletag.display(id)
37+
window.reactTooltipAds.initialized[id] = true
38+
}
39+
})
40+
}
41+
42+
window.addEventListener('AdsContainerMounted', handleLoadAds)

0 commit comments

Comments
 (0)