Skip to content

Commit 44eb7db

Browse files
committed
Modify ads config
1 parent 63392fe commit 44eb7db

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/containers/post/HorizontalAd.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { useEffect, useRef } from 'react';
2+
import styled from 'styled-components';
3+
interface Props {}
4+
5+
function HorizontalAd({}: Props) {
6+
const ref = useRef<HTMLDivElement>(null);
7+
const initializedRef = useRef(false);
8+
9+
useEffect(() => {
10+
const observer = new IntersectionObserver(
11+
(entries) => {
12+
entries.forEach((entry) => {
13+
if (entry.isIntersecting && !initializedRef.current) {
14+
initializedRef.current = true;
15+
(window.adsbygoogle = window.adsbygoogle || []).push({});
16+
console.log('initialized!');
17+
}
18+
});
19+
},
20+
{
21+
rootMargin: '90px',
22+
threshold: 0,
23+
},
24+
);
25+
observer.observe(ref.current!);
26+
return () => {
27+
observer.disconnect();
28+
};
29+
}, []);
30+
31+
return (
32+
<Wrapper>
33+
<ins
34+
className="adsbygoogle"
35+
style={{ display: 'block' }}
36+
data-ad-client="ca-pub-5574866530496701"
37+
data-ad-slot="8809887603"
38+
data-ad-format="auto"
39+
data-full-width-responsive="true"
40+
/>
41+
</Wrapper>
42+
);
43+
}
44+
45+
const Wrapper = styled.div`
46+
margin-top: 1.5rem;
47+
margin-bottom: 1.5rem;
48+
`;
49+
50+
export default HorizontalAd;

src/containers/post/PostViewer.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import MobileLikeButton from '../../components/post/MobileLikeButton';
3636
import RelatedPost from './RelatedPost';
3737
import optimizeImage from '../../lib/optimizeImage';
3838
import RelatedPostsForGuest from './RelatedPostsForGuest';
39+
import HorizontalAd from './HorizontalAd';
3940

4041
const UserProfileWrapper = styled(VelogResponsive)`
4142
margin-top: 16rem;
@@ -308,6 +309,10 @@ const PostViewer: React.FC<PostViewerProps> = ({
308309

309310
const { post } = data;
310311

312+
const isVeryOld =
313+
Date.now() - new Date(post.released_at).getTime() >
314+
1000 * 60 * 60 * 24 * 365;
315+
311316
const url = `https://velog.io/@${username}/${post.url_slug}`;
312317

313318
return (
@@ -387,11 +392,13 @@ const PostViewer: React.FC<PostViewerProps> = ({
387392
<RelatedPostsForGuest
388393
postId={post.id}
389394
showAds={
395+
!isVeryOld &&
390396
Date.now() - new Date(post.released_at).getTime() >
391-
1000 * 60 * 60 * 24 * 21
397+
1000 * 60 * 60 * 24 * 21
392398
}
393399
/>
394400
)}
401+
{isVeryOld && <HorizontalAd />}
395402
<PostComments
396403
count={post.comments_count}
397404
comments={post.comments}
@@ -401,6 +408,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
401408
<RelatedPost
402409
postId={post.id}
403410
showAds={
411+
!isVeryOld &&
404412
post.user.id !== userId &&
405413
Date.now() - new Date(post.released_at).getTime() >
406414
1000 * 60 * 60 * 24 * 30

0 commit comments

Comments
 (0)