Skip to content

Commit a472aba

Browse files
committed
Initialize ads only when visible
1 parent 92b25b8 commit a472aba

File tree

3 files changed

+95
-44
lines changed

3 files changed

+95
-44
lines changed

src/components/common/AdFeed.tsx

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useEffect, useRef } from 'react';
22
import styled from 'styled-components';
33
import { mediaQuery } from '../../lib/styles/media';
44
import gtag from '../../lib/gtag';
55

66
function AdFeed({ forPost, index }: { forPost?: boolean; index: number }) {
7+
const ref = useRef<HTMLDivElement>(null);
8+
const initializedRef = useRef(false);
79
// const [isMobile, setIsMobile] = useState(false);
810

911
// useEffect(() => {
@@ -17,26 +19,59 @@ function AdFeed({ forPost, index }: { forPost?: boolean; index: number }) {
1719
// }, 250);
1820
// }, []);
1921
useEffect(() => {
22+
if (forPost) return;
2023
(window.adsbygoogle = window.adsbygoogle || []).push({});
21-
}, []);
24+
}, [forPost]);
25+
26+
useEffect(() => {
27+
if (!forPost) return;
28+
const observer = new IntersectionObserver(
29+
(entries) => {
30+
entries.forEach((entry) => {
31+
if (entry.isIntersecting && !initializedRef.current) {
32+
initializedRef.current = true;
33+
(window.adsbygoogle = window.adsbygoogle || []).push({});
34+
console.log('initialized!');
35+
}
36+
});
37+
},
38+
{
39+
rootMargin: '90px',
40+
threshold: 0,
41+
},
42+
);
43+
observer.observe(ref.current!);
44+
}, [forPost]);
2245

2346
return (
2447
<Block
48+
ref={ref}
2549
forPost={forPost}
2650
onClick={() => {
2751
if (forPost) {
2852
gtag('event', 'ads_click', { event_label: index });
2953
}
3054
}}
3155
>
32-
<ins
33-
className="adsbygoogle"
34-
style={{ display: 'block' }}
35-
data-ad-format="fluid"
36-
data-ad-layout-key="-6u+e7+18-4k+8t"
37-
data-ad-client="ca-pub-5574866530496701"
38-
data-ad-slot="3828701581"
39-
></ins>
56+
{forPost ? (
57+
<ins
58+
className="adsbygoogle"
59+
style={{ display: 'block' }}
60+
data-ad-format="fluid"
61+
data-ad-layout-key="-6u+e5+1a-3q+77"
62+
data-ad-client="ca-pub-5574866530496701"
63+
data-ad-slot="8480422066"
64+
></ins>
65+
) : (
66+
<ins
67+
className="adsbygoogle"
68+
style={{ display: 'block' }}
69+
data-ad-format="fluid"
70+
data-ad-layout-key="-6u+e7+18-4k+8t"
71+
data-ad-client="ca-pub-5574866530496701"
72+
data-ad-slot="3828701581"
73+
></ins>
74+
)}
4075
{/* {isMobile ? (
4176
<ins
4277
className="adsbygoogle"

src/containers/post/PostViewer.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,16 @@ const PostViewer: React.FC<PostViewerProps> = ({
397397
comments={post.comments}
398398
postId={post.id}
399399
/>
400-
{showRecommends && userId !== null && <RelatedPost postId={post.id} />}
400+
{showRecommends && userId !== null && (
401+
<RelatedPost
402+
postId={post.id}
403+
showAds={
404+
post.user.id !== userId &&
405+
Date.now() - new Date(post.released_at).getTime() >
406+
1000 * 60 * 60 * 24 * 30
407+
}
408+
/>
409+
)}
401410
</PostViewerProvider>
402411
);
403412
};

src/containers/post/RelatedPost.tsx

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,68 @@
1-
import React from 'react';
1+
import React, { useState, useEffect, useMemo } from 'react';
22
import styled from 'styled-components';
33
import media from '../../lib/styles/media';
44
import { useQuery } from '@apollo/react-hooks';
55
import {
66
GetRecommendedPostResponse,
77
GET_RECOMMENDED_POST,
8+
PartialPost,
89
} from '../../lib/graphql/post';
910
import PostCardGrid from '../../components/common/PostCardGrid';
1011
import palette from '../../lib/styles/palette';
11-
// import { detectAnyAdblocker } from 'just-detect-adblock';
12+
import { detectAnyAdblocker } from 'just-detect-adblock';
1213

13-
function RelatedPost({ postId }: { postId: string }) {
14-
// const [adBlocked, setAdBlocked] = useState(false);
15-
// useEffect(() => {
16-
// detectAnyAdblocker().then((detected: boolean) => {
17-
// if (detected) {
18-
// setAdBlocked(true);
19-
// }
20-
// });
21-
// }, []);
14+
function RelatedPost({
15+
postId,
16+
showAds,
17+
}: {
18+
postId: string;
19+
showAds: boolean;
20+
}) {
21+
const [adBlocked, setAdBlocked] = useState(false);
22+
useEffect(() => {
23+
detectAnyAdblocker().then((detected: boolean) => {
24+
if (detected) {
25+
setAdBlocked(true);
26+
}
27+
});
28+
}, []);
2229

2330
const { data } = useQuery<GetRecommendedPostResponse>(GET_RECOMMENDED_POST, {
2431
variables: {
2532
id: postId,
2633
},
2734
});
2835

29-
// const postWithAds = useMemo(() => {
30-
// if (!data?.post) return null;
31-
// if (!showAds || adBlocked) return data.post.recommended_posts;
32-
// const cloned: (PartialPost | undefined)[] = [
33-
// ...data.post.recommended_posts,
34-
// ];
35-
// // get random number between 0 and length of array
36-
// const randomIndex = () => Math.floor(Math.random() * 8);
37-
// const firstAdIndex = randomIndex();
38-
// const secondAdIndex = (() => {
39-
// let index = randomIndex();
40-
// while (index === firstAdIndex) {
41-
// index = randomIndex();
42-
// }
43-
// return index;
44-
// })();
36+
const postWithAds = useMemo(() => {
37+
if (!data?.post) return null;
38+
if (!showAds || adBlocked) return data.post.recommended_posts;
39+
const cloned: (PartialPost | undefined)[] = [
40+
...data.post.recommended_posts,
41+
];
42+
// get random number between 0 and length of array
43+
const randomIndex = () => Math.floor(Math.random() * 8);
44+
const firstAdIndex = randomIndex();
45+
const secondAdIndex = (() => {
46+
let index = randomIndex();
47+
while (index === firstAdIndex) {
48+
index = randomIndex();
49+
}
50+
return index;
51+
})();
4552

46-
// cloned[firstAdIndex] = undefined;
47-
// cloned[secondAdIndex] = undefined;
48-
// return cloned;
49-
// }, [data, showAds, adBlocked]);
53+
cloned[firstAdIndex] = undefined;
54+
cloned[secondAdIndex] = undefined;
55+
return cloned;
56+
}, [data, showAds, adBlocked]);
5057

51-
if (!data?.post.recommended_posts) return null;
58+
if (!postWithAds) return null;
5259

5360
return (
5461
<>
5562
<Background>
5663
<Title>관심 있을 만한 포스트</Title>
5764
<Wrapper>
58-
<PostCardGrid posts={data?.post.recommended_posts} forPost />
65+
<PostCardGrid posts={postWithAds} forPost />
5966
</Wrapper>
6067
</Background>
6168
<PullUp />

0 commit comments

Comments
 (0)