How can i add adsense ads units in nextjs project #57181
-
SummaryI have already added google adsense auto ads code successfully, but i want to add adsense ads units in website pages made in next js latest version. Kindly help me in this regard. Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 3 replies
-
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 script |
Beta Was this translation helpful? Give feedback.
-
if you are using pages router use |
Beta Was this translation helpful? Give feedback.
-
Thanks bro .............great....... |
Beta Was this translation helpful? Give feedback.
-
Any blog for full placement of code for ad units of adsense in next js latest version plz..... |
Beta Was this translation helpful? Give feedback.
-
No Problem @gsahab0007 , i also had that issue :). |
Beta Was this translation helpful? Give feedback.
-
i never tired this one, previous i have a tried various set of codes but didnt help for this site https://www.calcgenie.com/distance-calculators/cm-to-inch. this one is my last hope. because perfromance of my site is hurting because of this |
Beta Was this translation helpful? Give feedback.
For App Router
And Use That Component with children as ins s…