Ad

Can't Connect Google Analitycs With React.js

- 1 answer

how are you doing? I badly cannot connect GA with React, im very stucked and all the videos that i saw the ID connector its start with UA, please help me

import './App.css';
// import Header from "../Header/header"
import Header2 from "../Header2/header"
import Footer from "../Footer/footer"
import Switch from "../Switch/switch"
import {BrowserRouter as Router} from "react-router-dom";
import React, {useEffect} from "react"
import ReactGa from "react-ga";

/**** Icons ****/

import { library } from '@fortawesome/fontawesome-svg-core'
import { fab, faFacebookSquare, faGoogle, faInstagramSquare, faWhatsappSquare} from '@fortawesome/free-brands-svg-icons'
import { fas, faBars, faIdCard, faGlobeAmericas, faCalendarAlt, faArrowUp} from '@fortawesome/free-solid-svg-icons'

library.add(fas, fab, faBars, faFacebookSquare, faInstagramSquare, faWhatsappSquare, faGoogle, faIdCard, faGlobeAmericas, faCalendarAlt, faArrowUp)


    function App() {
    
      useEffect(() => {
          ReactGa.initialize('G-YZ9S63BMYY')
    
          ReactGa.pageview(window.location.pathname)
      }, [])
    
      useEffect(() => {
        console.log("hola", window.location.pathname)
      })
    
      return (
          <Router>
           <Header2 />
            <React.StrictMode>
              <Switch />
            </React.StrictMode>
              <Footer />
          </Router>
      );  
    }

export default App;

From now on thanks for all folks :)

Ad

Answer

You have to use React for GA4: https://www.npmjs.com/package/react-ga4

Ad
source: stackoverflow.com
Ad