hustcc / hustcc/react-adsense

Adsense stops loading ads after couple of clientside page navigations

Open
#25 1 comment 5 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
280
Forks
41
PR merge metrics
No merged PRs in 30d

Description

When switching pages in my nextjs blog using client side routing, it seems after a few navigations the ads are not showing anymore.

A full page reload resolves this.

I think this is caused by having a maximum number of ads loaded. Probably each navigation needs to do some cleanup on the window object so it behaves like a full page reload.

Anyone else experienced this?

Maybe we need to include some examples for nextjs _app.js or something on how to resolve this.

e.g. This is code I already have in my _app.js, but not sure how to fix google adsense.

```jsx
export default function App({ Component, pageProps }) {
const router = useRouter();

useEffect(() => {
const handleRouteChange = (url) => {
console.log(`navigated: ${url}`);
gtag.pageview(url); // this does the google-analytics pageview tracking.
// do something with the googleadsense stuff to make it behave like a full page reload.
};
router.events.on("routeChangeComplete", handleRouteChange);
return () => {
router.events.off("routeChangeComplete", handleRouteChange);
};
}, [router.events]);

.........
left for brevity
....
```

We might also offer a React hook like `useAdsense()` with instructions on where to add the hook in a nextjs app and where to add it in a plain react app etc.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.