Skip to content

How can i add adsense ads units in nextjs project #57181

Closed Answered by shriekdj
gsahab0007 asked this question in Help
Discussion options

You must be logged in to vote

For App Router

'use client';
import React, { Fragment, ReactNode, useEffect } from 'react';
import { usePathname, useSearchParams } from 'next/navigation';

type Props = {
  children: ReactNode;
};

declare global {
  interface Window {
    adsbygoogle?: any | any[];
  }
}

const GoogleAdUnit = ({ children }: Props) => {
  const pathname = usePathname();
  const searchParams = useSearchParams();
  useEffect(() => {
    try {
      (window.adsbygoogle = window.adsbygoogle || []).push({});
    } catch (err) {
      console.error(err);
    }
  }, [pathname, searchParams]);
  return <Fragment>{children}</Fragment>;
}


export default GoogleAdUnit;

And Use That Component with children as ins s…

Replies: 6 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by gsahab0007
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@gsahab0007
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@shriekdj
Comment options

@shriekdj
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants