Skip to content

Commit 846ba2d

Browse files
committed
refactor: GTM 전송 로직 유틸리티 분리 및 Lint 에러 수정
1 parent e977b16 commit 846ba2d

7 files changed

Lines changed: 23 additions & 28 deletions

File tree

app/create/page.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useCreateMeeting } from '@/hooks/api/mutation/useCreateMeeting';
77
import type { MeetingCreateRequest } from '@/types/api';
88
import { useToast } from '@/hooks/useToast';
99
import Toast from '@/components/ui/toast';
10+
import { pushDataLayer } from '@/lib/gtm';
1011

1112
export default function Page() {
1213
const [meetingName, setMeetingName] = useState('');
@@ -157,9 +158,7 @@ export default function Page() {
157158
localStorage.setItem(`is_host_${meetingId}`, 'true');
158159

159160
// ⭐️ 방 만든 브라우저가 누구인지 식별자를 담아서 이벤트 전송 (dataLayer 직접 Push)
160-
const w = window as any;
161-
w.dataLayer = w.dataLayer || [];
162-
w.dataLayer.push({
161+
pushDataLayer({
163162
event: 'url_created', // 객체 내부의 키로 이벤트명 삽입
164163
meeting_url_id: meetingId,
165164
participant_count_expected: capacity,

app/meeting/[id]/page.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import MeetingInfoSection from '@/components/meeting/MeetingInfoSection';
1515
import { useToast } from '@/hooks/useToast';
1616
import Toast from '@/components/ui/toast';
1717
import { getMeetingUserId, removeMeetingUserId } from '@/lib/storage';
18+
import { pushDataLayer } from '@/lib/gtm';
1819

1920
interface StationInfo {
2021
line: string;
@@ -82,9 +83,7 @@ export default function Page() {
8283
localStorage.setItem('browser_id', browserId);
8384
}
8485

85-
const w = window as any;
86-
w.dataLayer = w.dataLayer || [];
87-
w.dataLayer.push({
86+
pushDataLayer({
8887
event: 'share_link',
8988
meeting_url_id: id,
9089
location: 'creation_complete',
@@ -148,9 +147,7 @@ export default function Page() {
148147
const isHost = localStorage.getItem(`is_host_${id}`) === 'true';
149148
const userRole = isHost ? 'host' : 'participant';
150149

151-
const w = window as any;
152-
w.dataLayer = w.dataLayer || [];
153-
w.dataLayer.push({
150+
pushDataLayer({
154151
event: 'departure_location_submitted',
155152
meeting_url_id: id,
156153
user_cookie_id: browserId,
@@ -182,9 +179,7 @@ export default function Page() {
182179
const userRole = isHost ? 'host' : 'participant';
183180
const browserId = localStorage.getItem('browser_id');
184181

185-
const w = window as any;
186-
w.dataLayer = w.dataLayer || [];
187-
w.dataLayer.push({
182+
pushDataLayer({
188183
event: 'midpoint_calculated',
189184
meeting_url_id: id,
190185
browser_id: browserId,

app/recommend/page.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Image from 'next/image';
66
import KakaoMapRecommend from '@/components/map/kakaoMapRecommend';
77
import { useRecommend } from '@/hooks/api/query/useRecommend';
88
import { useCheckMeeting } from '@/hooks/api/query/useCheckMeeting';
9+
import { pushDataLayer } from '@/lib/gtm';
910

1011
function RecommendContent() {
1112
const router = useRouter();
@@ -153,9 +154,7 @@ function RecommendContent() {
153154
const userRole = isHost ? 'host' : 'participant';
154155
const candidateId = `place_${String(place.id).padStart(2, '0')}`;
155156

156-
const w = window as any;
157-
w.dataLayer = w.dataLayer || [];
158-
w.dataLayer.push({
157+
pushDataLayer({
159158
event: 'external_map_opened',
160159
meeting_url_id: meetingId,
161160
user_cookie_id: browserId,
@@ -177,9 +176,7 @@ function RecommendContent() {
177176
if (typeof window !== 'undefined' && meetingId) {
178177
const candidateId = `place_${String(place.id).padStart(2, '0')}`;
179178

180-
const w = window as any;
181-
w.dataLayer = w.dataLayer || [];
182-
w.dataLayer.push({
179+
pushDataLayer({
183180
event: 'place_list_viewed',
184181
meeting_url_id: meetingId,
185182
candidate_id: candidateId,

app/result/[id]/page.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getMeetingUserId } from '@/lib/storage';
1111
import { useQueryClient } from '@tanstack/react-query';
1212
import Loading from '@/components/loading/loading';
1313
import { getRandomHexColor } from '@/lib/color';
14+
import { pushDataLayer } from '@/lib/gtm';
1415

1516
export default function Page() {
1617
const queryClient = useQueryClient();
@@ -168,9 +169,7 @@ export default function Page() {
168169
localStorage.setItem('browser_id', browserId);
169170
}
170171

171-
const w = window as any;
172-
w.dataLayer = w.dataLayer || [];
173-
w.dataLayer.push({
172+
pushDataLayer({
174173
event: 'share_link',
175174
meeting_url_id: id,
176175
location: 'place_list', // PM님 명세: 결과 리스트 페이지

components/join/joinForm.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useIsLoggedIn } from '@/hooks/useIsLoggedIn';
1010
import { setMeetingUserId } from '@/lib/storage';
1111
import Image from 'next/image';
1212
import { getRandomHexColor } from '@/lib/color';
13+
import { pushDataLayer } from '@/lib/gtm';
1314

1415
interface JoinFormProps {
1516
meetingId: string;
@@ -77,10 +78,7 @@ export default function JoinForm({ meetingId }: JoinFormProps) {
7778
const isHost = localStorage.getItem(`is_host_${meetingId}`) === 'true';
7879
const userRole = isHost ? 'host' : 'participant';
7980

80-
// dataLayer 직접 Push
81-
const w = window as any;
82-
w.dataLayer = w.dataLayer || [];
83-
w.dataLayer.push({
81+
pushDataLayer({
8482
event: 'participant_registration',
8583
meeting_url_id: meetingId,
8684
user_cookie_id: browserId,

components/share/shareContent.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Link from 'next/link';
55
import Toast from '@/components/ui/toast';
66
import { useShareMeeting } from '@/hooks/api/query/useShareMeeting';
77
import Image from 'next/image';
8+
import { pushDataLayer } from '@/lib/gtm';
89

910
interface ShareContentProps {
1011
id: string;
@@ -27,9 +28,7 @@ export default function ShareContent({ id }: ShareContentProps) {
2728
}
2829

2930
// dataLayer 직접 Push (이벤트명을 키값으로!)
30-
const w = window as any;
31-
w.dataLayer = w.dataLayer || [];
32-
w.dataLayer.push({
31+
pushDataLayer({
3332
event: 'share_link',
3433
meeting_url_id: id,
3534
location: 'creation_complete',

lib/gtm.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const pushDataLayer = (data: Record<string, unknown>) => {
2+
if (typeof window !== 'undefined') {
3+
const w = window as unknown as { dataLayer: object[] };
4+
5+
w.dataLayer = w.dataLayer || [];
6+
w.dataLayer.push(data);
7+
}
8+
};

0 commit comments

Comments
 (0)