Skip to content

Commit 5fa39c4

Browse files
committed
replace: ad provider
1 parent c17ab38 commit 5fa39c4

File tree

1 file changed

+130
-9
lines changed

1 file changed

+130
-9
lines changed

src/containers/post/HorizontalAd.tsx

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,92 @@
11
import React, { useEffect, useRef } from 'react';
22
import styled from 'styled-components';
33
import VelogResponsive from '../../components/velog/VelogResponsive';
4+
import media from '../../lib/styles/media';
5+
46
interface Props {}
57

8+
function initDesktopAd() {
9+
if (window.innerWidth < 768) return;
10+
(function () {
11+
var size = '728x90',
12+
adunit = 'velog.io_728x90_leaderboard_DFP',
13+
childNetworkId = '22738196472',
14+
xmlhttp = new XMLHttpRequest();
15+
xmlhttp.onreadystatechange = function () {
16+
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
17+
var es = document.querySelectorAll("[data-id='" + adunit + "']");
18+
var e = Array.from(es).filter(function (e) {
19+
return !e.hasAttribute('data-rendered');
20+
});
21+
if (e.length > 0) {
22+
e.forEach(function (el: any) {
23+
var iframe = el.contentWindow.document;
24+
iframe.open();
25+
iframe.write(xmlhttp.responseText);
26+
iframe.close();
27+
el.setAttribute('data-rendered', true as any);
28+
});
29+
}
30+
}
31+
};
32+
var child = childNetworkId.trim() ? ',' + childNetworkId.trim() : '';
33+
xmlhttp.open(
34+
'GET',
35+
'https://pubads.g.doubleclick.net/gampad/adx?iu=/147246189' +
36+
child +
37+
'/' +
38+
adunit +
39+
'&sz=' +
40+
encodeURI(size) +
41+
'&t=Placement_type%3Dserving&' +
42+
Date.now(),
43+
true,
44+
);
45+
xmlhttp.send();
46+
})();
47+
}
48+
49+
function initMobileAd() {
50+
(function () {
51+
if (window.innerWidth > 580) return;
52+
var size = '320x100',
53+
adunit = 'velog.io_320x100_leaderboard_DFP',
54+
childNetworkId = '22738196472',
55+
xmlhttp = new XMLHttpRequest();
56+
xmlhttp.onreadystatechange = function () {
57+
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
58+
var es = document.querySelectorAll("[data-id='" + adunit + "']");
59+
var e = Array.from(es).filter(function (e) {
60+
return !e.hasAttribute('data-rendered');
61+
});
62+
if (e.length > 0) {
63+
e.forEach(function (el: any) {
64+
var iframe = el.contentWindow.document;
65+
iframe.open();
66+
iframe.write(xmlhttp.responseText);
67+
iframe.close();
68+
el.setAttribute('data-rendered', true);
69+
});
70+
}
71+
}
72+
};
73+
var child = childNetworkId.trim() ? ',' + childNetworkId.trim() : '';
74+
xmlhttp.open(
75+
'GET',
76+
'https://pubads.g.doubleclick.net/gampad/adx?iu=/147246189' +
77+
child +
78+
'/' +
79+
adunit +
80+
'&sz=' +
81+
encodeURI(size) +
82+
'&t=Placement_type%3Dserving&' +
83+
Date.now(),
84+
true,
85+
);
86+
xmlhttp.send();
87+
})();
88+
}
89+
690
function HorizontalAd({}: Props) {
791
const ref = useRef<HTMLDivElement>(null);
892
const initializedRef = useRef(false);
@@ -13,7 +97,8 @@ function HorizontalAd({}: Props) {
1397
entries.forEach((entry) => {
1498
if (entry.isIntersecting && !initializedRef.current) {
1599
initializedRef.current = true;
16-
(window.adsbygoogle = window.adsbygoogle || []).push({});
100+
initDesktopAd();
101+
initMobileAd();
17102
console.log('initialized!');
18103
}
19104
});
@@ -33,14 +118,30 @@ function HorizontalAd({}: Props) {
33118
return (
34119
<VelogResponsive>
35120
<Wrapper ref={ref}>
36-
<ins
37-
className="adsbygoogle"
38-
style={{ display: 'block', maxHeight: 400, width: '100%' }}
39-
data-ad-client="ca-pub-5574866530496701"
40-
data-ad-slot="8809887603"
41-
data-ad-format="auto"
42-
data-full-width-responsive="true"
43-
/>
121+
<Desktop>
122+
<iframe
123+
title="desktop-horizontal"
124+
data-id="velog.io_728x90_leaderboard_DFP"
125+
frameBorder="0"
126+
scrolling="no"
127+
marginHeight={0}
128+
marginWidth={0}
129+
width="1"
130+
height="1"
131+
></iframe>
132+
</Desktop>
133+
<Mobile>
134+
<iframe
135+
title="mobile-horizontal"
136+
data-id="velog.io_320x100_leaderboard_DFP"
137+
frameBorder="0"
138+
scrolling="no"
139+
marginHeight={0}
140+
marginWidth={0}
141+
width="1"
142+
height="1"
143+
></iframe>
144+
</Mobile>
44145
</Wrapper>
45146
</VelogResponsive>
46147
);
@@ -52,4 +153,24 @@ const Wrapper = styled.div`
52153
margin-bottom: 1.5rem;
53154
`;
54155

156+
const Desktop = styled.div`
157+
display: flex;
158+
justify-content: center;
159+
iframe {
160+
width: 728px;
161+
height: 90px;
162+
}
163+
${media.small} {
164+
display: none;
165+
}
166+
`;
167+
168+
const Mobile = styled.div`
169+
display: none;
170+
justify-content: center;
171+
${media.custom(580)} {
172+
display: flex;
173+
}
174+
`;
175+
55176
export default HorizontalAd;

0 commit comments

Comments
 (0)