Skip to content

Commit 66745f0

Browse files
committed
fix: ad logic
1 parent 10029e8 commit 66745f0

File tree

3 files changed

+40
-45
lines changed

3 files changed

+40
-45
lines changed

src/containers/post/HorizontalBanner.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import styled from 'styled-components';
33
import VelogResponsive from '../../components/velog/VelogResponsive';
44
import gtag from '../../lib/gtag';
55

6-
function HorizontalBanner() {
6+
type Props = {
7+
isDisplayAd?: boolean;
8+
};
9+
10+
function HorizontalBanner({ isDisplayAd = false }: Props) {
711
useEffect(() => {
812
(window.adsbygoogle = window.adsbygoogle || []).push({});
913
}, []);
@@ -14,14 +18,25 @@ function HorizontalBanner() {
1418

1519
return (
1620
<StyledResponsive onClick={onClick}>
17-
<ins
18-
className="adsbygoogle"
19-
style={{ display: 'block', textAlign: 'center' }}
20-
data-ad-layout="in-article"
21-
data-ad-format="fluid"
22-
data-ad-client="ca-pub-9161852896103498"
23-
data-ad-slot="6869845586"
24-
></ins>
21+
{isDisplayAd ? (
22+
<ins
23+
className="adsbygoogle"
24+
style={{ display: 'block', textAlign: 'center' }}
25+
data-ad-client="ca-pub-9161852896103498"
26+
data-ad-slot="5950320973"
27+
data-ad-format="auto"
28+
data-full-width-responsive="true"
29+
></ins>
30+
) : (
31+
<ins
32+
className="adsbygoogle"
33+
style={{ display: 'block', textAlign: 'center' }}
34+
data-ad-layout="in-article"
35+
data-ad-format="fluid"
36+
data-ad-client="ca-pub-9161852896103498"
37+
data-ad-slot="6869845586"
38+
></ins>
39+
)}
2540
</StyledResponsive>
2641
);
2742
}

src/containers/post/PostViewer.tsx

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,24 @@ const PostViewer: React.FC<PostViewerProps> = ({
208208
if (isOwnPost) return false;
209209
if (!isVeryOld) return false;
210210
return true;
211-
}, [data?.post]);
211+
}, [data?.post, userId]);
212+
213+
const shouldShowFooterBanner = useMemo(() => {
214+
if (shouldShowBanner) return false;
215+
if (!data?.post) return false;
216+
if (userId) return false;
217+
return true;
218+
}, [userId, data?.post]);
212219

213220
useEffect(() => {
214221
if (!data?.post?.id) return;
215222
if (!shouldShowBanner) return;
216223
gtag('event', 'banner_view');
217224
console.log('banner_view');
218-
}, [data?.post?.id, shouldShowBanner]);
225+
if (userId) {
226+
gtag('event', 'banner_view_user');
227+
}
228+
}, [data?.post?.id, shouldShowBanner, userId]);
219229

220230
const onRemove = async () => {
221231
if (!data || !data.post) return;
@@ -426,37 +436,14 @@ const PostViewer: React.FC<PostViewerProps> = ({
426436
/>
427437
</UserProfileWrapper>
428438
<LinkedPostList linkedPosts={post.linked_posts} />
429-
{/* {showRecommends && userId === null && !isVeryOld && (
430-
<RelatedPostsForGuest
431-
postId={post.id}
432-
showAds={
433-
false
434-
// !isVeryOld &&
435-
// Date.now() - new Date(post.released_at).getTime() >
436-
// 1000 * 60 * 60 * 24 * 21
437-
}
438-
/>
439-
)} */}
440439
{shouldShowBanner && isContentLongEnough ? <HorizontalBanner /> : null}
441-
440+
{shouldShowFooterBanner ? <HorizontalBanner isDisplayAd /> : null}
442441
<PostComments
443442
count={post.comments_count}
444443
comments={post.comments}
445444
postId={post.id}
446445
ownPost={post.user.id === userId}
447446
/>
448-
{/* {showRecommends && (userId !== null || isVeryOld) && (
449-
<RelatedPost
450-
postId={post.id}
451-
showAds={
452-
false
453-
// !isVeryOld &&
454-
// post.user.id !== userId &&
455-
// Date.now() - new Date(post.released_at).getTime() >
456-
// 1000 * 60 * 60 * 24 * 30
457-
}
458-
/>
459-
)} */}
460447
{showRecommends ? (
461448
<RelatedPost postId={post.id} showAds={post?.user.id !== userId} />
462449
) : null}

src/containers/post/RelatedPost.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,12 @@ function RelatedPost({
3939
const cloned: (PartialPost | undefined)[] = [
4040
...data.post.recommended_posts,
4141
];
42-
// get random number between 0 and length of array
43-
const randomIndex = () => Math.floor(Math.random() * 8);
42+
// get random number between 0 and 3
43+
const randomIndex = () => Math.floor(Math.random() * 3);
4444
const firstAdIndex = randomIndex();
45-
const secondAdIndex = (() => {
46-
let index = randomIndex();
47-
while (index === firstAdIndex) {
48-
index = randomIndex();
49-
}
50-
return index;
51-
})();
5245

5346
cloned[firstAdIndex] = undefined;
54-
cloned[secondAdIndex] = undefined;
47+
5548
return cloned;
5649
}, [data, showAds, adBlocked]);
5750

0 commit comments

Comments
 (0)