Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/analytics/ga.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import ReactGA from 'react-ga4';
// GTM dataLayer를 직접 사용하는 방식으로 변경
declare global {
interface Window {
dataLayer: any[];
}
}

let isInitialized = false;

Expand All @@ -8,7 +13,10 @@ export const initGA = () => {
return;
}

ReactGA.initialize('G-MFJ749RKHH'); // 측정 ID
// dataLayer 초기화 확인
if (typeof window !== 'undefined') {
window.dataLayer = window.dataLayer || [];
}
isInitialized = true;
};

Expand All @@ -17,10 +25,16 @@ export const trackPageView = (path: string) => {
console.warn('GA4가 초기화되지 않았습니다. initGA()를 먼저 호출하세요.');
return;
}
ReactGA.send({ hitType: 'pageview', page: path });

if (typeof window !== 'undefined' && window.dataLayer) {
window.dataLayer.push({
event: 'page_view',
page_path: path,
});
}
};

// GA4 이벤트 추적 함수
// GA4 이벤트 추적 함수 (GTM dataLayer 사용)
export const trackEvent = (
eventName: string,
parameters?: Record<string, any>,
Expand All @@ -29,7 +43,13 @@ export const trackEvent = (
console.warn('GA4가 초기화되지 않았습니다. initGA()를 먼저 호출하세요.');
return;
}
ReactGA.event(eventName, parameters);

if (typeof window !== 'undefined' && window.dataLayer) {
window.dataLayer.push({
event: eventName,
...parameters,
});
}
};

// 검색 이벤트
Expand Down