Skip to content

analytics

Yenowme edited this page Dec 18, 2021 · 1 revision

react-ga๋ฅผ ํ†ตํ•œ ์ ์šฉ

  1. ๊ตฌ๊ธ€ ์• ๋„๋ฆฌํ‹ฑ์Šค ๊ณ„์ • ์ƒ์„ฑ

    • ๊ตฌ๊ธ€ ์• ๋„๋ฆฌํ‹ฑ์Šค ์‚ฌ์ดํŠธ ์—์„œ ๊ณ„์ • ์„ค์ • ์ง„ํ–‰
    • ์†์„ฑ์„ ์ตœ์‹ ๋ฒ„์ „์ด ์•„๋‹Œ ์œ ๋‹ˆ๋ฒ„์…œ ๋ฒ„์ „์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผ react-ga ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ตœ์‹ ๋ฒ„์ „์€ ์ง์ ‘ ์„ค์ •ํ•ด์ค˜์•ผํ•จ
  2. App.js ์— react-ga ์ ์šฉ

import { Switch, Route, useLocation } from "react-router-dom";
import ReactGA from "react-ga";

ReactGA.event({
  category: "User",
  action: "Created an Account",
});
ReactGA.exception({
  description: "An error ocurred",
  fatal: true,
});

const App: React.FC = () => {
  const location = useLocation();
  useEffect(() => {
    if (import.meta.env.MODE !== "development") {
      ReactGA.initialize(import.meta.env.VITE_GA_ID);
      ReactGA.set({ page: location.pathname }); // Update the user's current page
      ReactGA.pageview(location.pathname); // Record a pageview for the given page
    }
  }, [location]);
  return (

ReactGA.Initialize

  • ๊ฐ์ข… ์†์„ฑ ๋ฐ ์ถ”์ ID๋ฅผ ์‹ฌ์–ด ์ดˆ๊ธฐํ™” ์ง„ํ–‰

  • useEffect ๋กœ ํ•œ๋ฒˆ๋งŒ ์ง„ํ–‰

  • ์ถ”์  ID ๋Š” envํŒŒ์ผ๋กœ ๊ด€๋ฆฌ

  • ์‹ค์ œ ๋ฐฐํฌ๋ชจ๋“œ์ผ๋•Œ๋งŒ GA๋ฅผ ํ™œ์„ฑํ™”ํ–ˆ๋‹ค. ๊ฐœ๋ฐœ๋ชจ๋“œ์—์„œ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š”

    ReactGA.initialize("",{debug: true}) ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ์ฝ˜์†”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Œ.

๐Ÿšฉย ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ฐ์ง€

  • ๋ฆฌ์•กํŠธ๋Š” ํŽ˜์ด์ง€๋„ค์ด์…˜์ด ์›น์•ฑ ๊ธฐ๋ฐ˜. ์ฆ‰ window๋กœ ํŽ˜์ด์ง€๋„ค์ด์…˜์ด ๊ฐ์ง€๋˜์ง€ ์•Š์•„์„œ react-router๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํŽ˜์ด์ง€๋„ค์ด์…˜ ๊ฐ์ฒด์ธ location ์„ ์‚ฌ์šฉํ•ด์•ผํ•จ.
  • location ๊ฐ์ฒด๋Š”. useLocation() ํ›…์„ react-router-dom ์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
  • useLocation์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” App.tsx์˜ ์ƒ์œ„ ๊ฐ์ฒด์ธ Main.tsx์—์„œ App์„ Router๋กœ ๊ฐ์‹ธ์ค˜์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

์ฐธ๊ณ 

[react] react google analytics ์—ฐ๋™

Clone this wiki locally