Skip to content
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

[Error] GA4React is being initialized #11

Closed
hashimwebsul opened this issue Mar 5, 2021 · 2 comments
Closed

[Error] GA4React is being initialized #11

hashimwebsul opened this issue Mar 5, 2021 · 2 comments

Comments

@hashimwebsul
Copy link

hashimwebsul commented Mar 5, 2021

i am trying to send pageview but i get this error when i am navigating to any page
image

useEffect(() => { const ga4react = new GA4React('G-1JXXXXX'); ga4react.initialize().then((ga4) => { ga4.pageview(location.path + location.search) }, (err) => { console.log('error sending page view', err) }) }, [location])

@hashimwebsul hashimwebsul changed the title [Error] GA4React us being initialized [Error] GA4React is being initialized Mar 5, 2021
@unrealmanu
Copy link
Owner

unrealmanu commented Mar 6, 2021

You should only initialize ga-4-react once, so every react life cycle, you restart it, it's not allowed.

I'll give you an example to start it uniquely and then reuse it:

import React from "react";
import ReactDOM from "react-dom";
import GA4React, { useGA4React } from "ga-4-react";

const ga4react = new GA4React("G-1JXXXXX");

function MyApp() {
  const ga = useGA4React();
  console.log(ga);

  return <div className="App">hi!</div>;
}

(async () => {
  await ga4react.initialize();

  ReactDOM.render(
    <React.StrictMode>
      <MyApp />
    </React.StrictMode>,
    document.getElementById("root")
  );
})();

@unrealmanu
Copy link
Owner

otherwise, let the library start by itself:

import React from "react";
import ReactDOM from "react-dom";
import { useGA4React } from "ga-4-react";

function MyApp() {
  const ga = useGA4React("G-1JXXXXX");
  console.log(ga);

  return <div className="App">hi!</div>;
}

ReactDOM.render(
  <React.StrictMode>
    <MyApp />
  </React.StrictMode>,
  document.getElementById("root")
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants