NextJS automatic noindex meta tag preventing site indexing with Google Analytics

I have a NextJS 13.5 application where I integrated Google Analytics and it tracks page visits correctly. The problem is that a <meta name="robots" content="noindex"/> tag gets inserted automatically, which blocks search engines from indexing my site.

Here’s my analytics tracking component:

import Script from "next/script";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { trackPageView } from "@/utils/analyticsHelper";

export default function AnalyticsTracker() {
  const currentPath = usePathname();
  const queryParams = useSearchParams();

  const ANALYTICS_ID = `${process.env.NEXT_PUBLIC_ANALYTICS_TRACKING_ID}`;
  
  useEffect(() => {
    const fullUrl = currentPath + queryParams.toString();
    trackPageView(ANALYTICS_ID, fullUrl);
  }, [currentPath, queryParams, ANALYTICS_ID]);

  return (
    <>
      <Script
        strategy="afterInteractive"
        src={`https://www.googletagmanager.com/gtag/js?id=${ANALYTICS_ID}`}
      />
      <Script
        id="analytics-config"
        strategy="afterInteractive"
        dangerouslySetInnerHTML={{
          __html: `
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            
            gtag('consent', 'default', {
                'analytics_storage': 'granted'
            });
            
            gtag('config', '${ANALYTICS_ID}', {
                page_path: window.location.pathname,
            });
            `,
        }}
      />
    </>
  );
}

And the helper function:

export const trackPageView = (analyticsId: string, pageUrl: string) => {
  window.gtag("config", analyticsId, {
    page_path: pageUrl,
  });
};

How can I prevent this noindex tag from being added automatically?

This caught me off guard too when I first deployed my NextJS app. The noindex tag has nothing to do with your Google Analytics setup - your tracking code looks fine. NextJS automatically adds this tag during development so search engines don’t index your localhost or staging sites. It should disappear once you build for production and deploy to your actual domain. If you’re still seeing it live, check that NODE_ENV is set to ‘production’ and you’re using ‘next build’ + ‘next start’ instead of ‘next dev’. Just compare the page source on your live site vs your local dev server to confirm.

ya, it’s not ur GA code. next.js adds noindex in dev mode. make sure NODE_ENV is prod when u deploy, that usually solves it.