One more re: next.js install-> Is there a more...
# support
c
One more re: next.js install-> Is there a more efficient way to include the tag as opposed to including it on each page? If I include in _app.tsx it won’t load. Do I have to initialize on each page?
m
Hey There! 👋 Your message has been received by the RudderStack team. We will forward this request to your Technical Account Manager, and they will get back to you shortly. Please use the thread for any additional comments.
Here are sample repos that the team built on NextJS. You can check them out.
c
I looked at that sample repos and now I get inconsistent event page data
Copy code
import '@/styles/globals.css'
import type { AppProps } from 'next/app'
import { useEffect } from 'react'
import { useRouter } from 'next/router'
import useRudderStackAnalytics from '../useRudderAnalytics';

export default function App({ Component, pageProps }: AppProps) {
  const router = useRouter();
  const analytics = useRudderStackAnalytics();

  useEffect(() => {
    const trackPageChange = () => {
      analytics?.page(
        'PageView',
        {
          path: router.pathname,
          search: router.query
        }
      );
    }

    const completeHandler = () => {
      trackPageChange();
    };

    router.events.on('routeChangeComplete', completeHandler);
    trackPageChange()

    return () => {
      router.events.off('routeChangeComplete', completeHandler);
    };
  }, [router, analytics]);

  return <Component {...pageProps} />
}
I used the above in _app.tsx and I get 2 views when I am navigating from page to page
m
Hi Jim, if you are seeing multiple page calls it may be due to the following - One is the page(xhr) call and the other is the page (options) call. The options call is not counted towards your overall events and is sent only the first time when you navigate to a page. - You are making a page call while rudderStack SDK is also making a page call by default. You may want to remove one of the page calls.