Skip to content

Commit fc1d7d8

Browse files
authored
Merge pull request velopert#449 from velopert/feature/fix-banner-logic
feat: change banner logic
2 parents da6c9b2 + 6f81cc1 commit fc1d7d8

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/components/velog/VelogResponsive.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ const VelogResponsiveBlock = styled.div`
1414
export interface VelogResponsiveProps {
1515
className?: string;
1616
style?: React.CSSProperties;
17+
onClick?: () => void;
1718
}
1819

1920
const VelogResponsive: React.FC<VelogResponsiveProps> = ({
2021
children,
2122
className,
2223
style,
24+
onClick,
2325
}) => {
2426
return (
2527
<VelogResponsiveBlock
2628
children={children}
2729
className={className}
2830
style={style}
31+
onClick={onClick}
2932
/>
3033
);
3134
};

src/containers/post/HorizontalBanner.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import React, { useEffect } from 'react';
22
import styled from 'styled-components';
33
import VelogResponsive from '../../components/velog/VelogResponsive';
4+
import gtag from '../../lib/gtag';
45

56
function HorizontalBanner() {
67
useEffect(() => {
78
(window.adsbygoogle = window.adsbygoogle || []).push({});
89
}, []);
910

11+
const onClick = () => {
12+
gtag('event', 'banner_click');
13+
};
14+
1015
return (
11-
<StyledResponsive>
16+
<StyledResponsive onClick={onClick}>
1217
<ins
1318
className="adsbygoogle"
1419
style={{ display: 'block', textAlign: 'center' }}

src/containers/post/PostViewer.tsx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useEffect, useCallback, useRef, useState } from 'react';
1+
import React, {
2+
useEffect,
3+
useCallback,
4+
useRef,
5+
useState,
6+
useMemo,
7+
} from 'react';
28
import { useDispatch } from 'react-redux';
39
import {
410
READ_POST,
@@ -37,6 +43,7 @@ import RelatedPost from './RelatedPost';
3743
import optimizeImage from '../../lib/optimizeImage';
3844
import { useSetShowFooter } from '../../components/velog/VelogPageTemplate';
3945
import HorizontalBanner from './HorizontalBanner';
46+
import gtag from '../../lib/gtag';
4047

4148
const UserProfileWrapper = styled(VelogResponsive)`
4249
margin-top: 16rem;
@@ -189,6 +196,27 @@ const PostViewer: React.FC<PostViewerProps> = ({
189196
};
190197
}, [onScroll]);
191198

199+
const shouldShowBanner = useMemo(() => {
200+
if (!data?.post) return;
201+
202+
const post = data.post;
203+
const isOwnPost = post.user.id === userId;
204+
const isVeryOld =
205+
Date.now() - new Date(post.released_at).getTime() >
206+
1000 * 60 * 60 * 24 * 30;
207+
208+
if (isOwnPost) return false;
209+
if (!isVeryOld) return false;
210+
return true;
211+
}, [data?.post]);
212+
213+
useEffect(() => {
214+
if (!data?.post?.id) return;
215+
if (!shouldShowBanner) return;
216+
gtag('event', 'banner_view');
217+
console.log('banner_view');
218+
}, [data?.post?.id, shouldShowBanner]);
219+
192220
const onRemove = async () => {
193221
if (!data || !data.post) return;
194222
try {
@@ -320,9 +348,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
320348

321349
const { post } = data;
322350

323-
const isVeryOld =
324-
Date.now() - new Date(post.released_at).getTime() >
325-
1000 * 60 * 60 * 24 * 180;
351+
const isContentLongEnough = post.body.length > 500;
326352

327353
const url = `https://velog.io/@${username}/${post.url_slug}`;
328354

@@ -388,7 +414,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
388414
/>
389415
}
390416
/>
391-
{userId === null && isVeryOld ? <HorizontalBanner /> : null}
417+
{shouldShowBanner ? <HorizontalBanner /> : null}
392418
<PostContent isMarkdown={post.is_markdown} body={post.body} />
393419
<UserProfileWrapper>
394420
<UserProfile
@@ -411,9 +437,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
411437
}
412438
/>
413439
)} */}
414-
{userId === null && isVeryOld && post.body.length > 300 ? (
415-
<HorizontalBanner />
416-
) : null}
440+
{shouldShowBanner && isContentLongEnough ? <HorizontalBanner /> : null}
417441

418442
<PostComments
419443
count={post.comments_count}

0 commit comments

Comments
 (0)