Repository: next.js
Issue: Bug: adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page in Next.js v14
Labels: bug,Script (next/script)

### Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/sleepy-yalow-h3d3n3

### To Reproduce

1. Setup a Next.js v14 Project:
2. Initialize a new Next.js project or use an existing one running on version 14.
3. Create the GoogleAdsense Component:
4. Add the GoogleAdsense.tsx component in the src/components/analytics/ directory with the following content (credentials hidden for security).

<details><summary>Details</summary>
<p>
src/components/analytics/GoogleAdsense.tsx
</p>
</details> 

```
'use client';

import { useEffect } from 'react';
import Script from 'next/script';

let adsenseInitialized = false;

const GoogleAdsense = () => {
  useEffect(() => {
    if (!adsenseInitialized && typeof window !== 'undefined') {
      try {
        (window.adsbygoogle = window.adsbygoogle || []).push({
          google_ad_client: 'ca-pub-XXXXXXXXXXXXXXX', // Replace with your AdSense client ID
          enable_page_level_ads: true,
        });
        adsenseInitialized = true;
      } catch (e) {
        console.error('Adsense error:', e);
      }
    }
  }, []);

  return (
    <>
      <Script
        async
        src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXX" // Replace with your AdSense client ID
        crossOrigin="anonymous"
        strategy="afterInteractive"
      />
    </>
  );
};

export default GoogleAdsense;
```

5. Integrate GoogleAdsense into RootLayout:
6. Modify your app/layout.tsx to include the GoogleAdsense component alongside GoogleAnalytics.

<details><summary>Details</summary>
<p>
app/layout.tsx
</p>
</details> 

```
import GoogleAnalytics from '@/components/analytics/GoogleAnalytics';
import GoogleAdsense from '@/components/analytics/GoogleAdsense';
import Footer from '@/components/layout/footer';
import Header from '@/components/layout/header';
import { ThemeProvider } from '@/components/theme-provider';

import '../../globals.css';

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <head>
        <GoogleAnalytics />
        <GoogleAdsense />
      </head>
      <body
        className="">
        <ThemeProvider
          attribute="class"
          defaultTheme="system"
          enableSystem
          disableTransitionOnChange>
          <Header />
          {children}
          <Footer />
        </ThemeProvider>
      </body>
    </html>
  );
}
```
7. Run the Project:
8. Start your Next.js development server.
9. npm run dev
10. Observe the Console:
11. Open the browser’s developer console and navigate to your website (http://localhost:3000).
12. You should encounter the following error:
	_TagError: adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page._

### Current vs. Expected behavior

Current Behavior:

When integrating both Google Analytics and Google AdSense into a Next.js v14 project, attempting to initialize Google AdSense results in the following error in the browser console:

TagError: adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page.

Expected Behavior:

Both Google Analytics and Google AdSense should coexist without conflicts, allowing successful initialization and functionality of both services. Specifically:
	•	Google Analytics should track user interactions and page views as intended.
	•	Google AdSense should load and display ads without triggering initialization errors.

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:00:32 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6030
  Available memory (MB): 18432
  Available CPU cores: 11
Binaries:
  Node: 20.17.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 9.15.0
Relevant Packages:
  next: 14.2.3 // An outdated version detected (latest is 15.0.4), upgrade is highly recommended!
  eslint-config-next: 14.2.3
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.5.2
Next.js Config:
  output: N/A
 ⚠ An outdated version detected (latest is 15.0.4), upgrade is highly recommended!
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue
```


### Which area(s) are affected? (Select all that apply)

Script (next/script)

### Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local), Vercel (Deployed)

### Additional context

_No response_