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

injectScript as init param possible? #1

Open
TheWalkingDad3 opened this issue Jan 31, 2025 · 0 comments
Open

injectScript as init param possible? #1

TheWalkingDad3 opened this issue Jan 31, 2025 · 0 comments

Comments

@TheWalkingDad3
Copy link

TheWalkingDad3 commented Jan 31, 2025

Hi acomanescu,
first of all, thank you very much for the fork and the react 19 support.

I am missing a feature from the "old" lib - with the condition injectScript it was possible to dynamically decide if the GTM Script should be injected or not. The use case is for example when a cookie banner has to be confirmed before gtm is allowed to be included.

Is there any other possibility to achieve this goal or do you plan to add such a feature?

With the old library we did it this way

function App({ Component, pageProps }: AppProps) {
  const [injectScript, setInjectScript] = useState(false);
  useEffect(() => {
    if (typeof window !== 'undefined' && !window.OptanonWrapper) {
      window.OptanonWrapper = () => {
        window.OneTrust.OnConsentChanged(() => {
          if (window.OptanonActiveGroups.indexOf('C0002') > -1) {
            setInjectScript(true);
          }
        });
      };
    }
    if (
      typeof window !== 'undefined' &&
      document.cookie.replace(/(?:(?:^|.*;\s*)OptanonConsent\s*=\s*([^;]*).*$)|^.*$/, '$1').indexOf('C0002') > -1
    ) {
      setInjectScript(true);
    }
  }, []);

  return (
    <GTMProvider state={{ injectScript, ...gtmParams }}>
      <Component {...pageProps} />
    </GTMProvider>
  );
}
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

1 participant