-
Notifications
You must be signed in to change notification settings - Fork 0
[chore] Google Adsense/Analytics 적용 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 'use client'; // 클라이언트 컴포넌트임을 명시합니다. | ||
|
|
||
| import Script from 'next/script'; | ||
|
|
||
| const GoogleAnalytics = () => { | ||
| const gaId = process.env.NEXT_PUBLIC_GA_ID; | ||
|
|
||
| // 환경 변수가 설정되지 않았다면 아무것도 렌더링하지 않습니다. | ||
| if (!gaId) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {/* 외부 gtag.js 스크립트를 로드합니다. */} | ||
| <Script | ||
| strategy="afterInteractive" // 페이지가 상호작용 가능해진 후에 스크립트를 로드합니다. | ||
| src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`} | ||
| /> | ||
| {/* gtag 초기화 및 설정 스크립트를 삽입합니다. */} | ||
| <Script | ||
| id="gtag-init" | ||
| strategy="afterInteractive" | ||
| dangerouslySetInnerHTML={{ | ||
| __html: ` | ||
| window.dataLayer = window.dataLayer || []; | ||
| function gtag(){dataLayer.push(arguments);} | ||
| gtag('js', new Date()); | ||
| gtag('config', '${gaId}'); | ||
| `, | ||
| }} | ||
| /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default GoogleAnalytics; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
AdSense 클라이언트 ID도 환경 변수로 관리해야 합니다.
현재 AdSense 클라이언트 ID가 하드코딩되어 있는데, GoogleAnalytics 컴포넌트와 동일하게 환경 변수로 관리하는 것이 일관성 있고 안전합니다. 또한 Next.js 13+ App Router에서는
next/script의 Script 컴포넌트를 사용하는 것이 권장됩니다.다음과 같이 별도의 GoogleAdsense 컴포넌트를 생성하여 일관성을 유지할 수 있습니다:
src/components/GoogleAdsense.tsx 파일을 새로 생성:
src/app/layout.tsx에 적용:
그런 다음
.env파일에 다음을 추가:🤖 Prompt for AI Agents