Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinitiate/reload ads on page change #48

Open
maxeth opened this issue Dec 18, 2023 · 2 comments
Open

Reinitiate/reload ads on page change #48

maxeth opened this issue Dec 18, 2023 · 2 comments

Comments

@maxeth
Copy link

maxeth commented Dec 18, 2023

Hey,

I was wondering how you're handling page changes so that the ads get reinitiated/readjusted to the layout changes.

For example, on the adsense dashboard, I excluded the index and contact page to NOT contain any ads. When I visit example.com/ this works just as expected, but when I visit some other blog post first, and then use next.js's client-side router to change the route to the index page example.com/, the ads just remain in the "blog layout" as if the window location never actually changed. This breaks the whole page layout of the newly navigated-to page.

I saw something like this to "reload" ads on page changes online, but it seems to do absolutely nothing in my case:

  const router = useRouter();


  useEffect(() => {
    if ((window as any).adsbygoogle) {
      const adsC = document.getElementsByClassName("adsbygoogle").length;
      for (let i = 0; i < adsC; i++) {
        try {
          ((window as any).adsbygoogle =
            (window as any).adsbygoogle || []).push({});
        } catch (e) {}
      }
    }
    return () => {
      if ((window as any).adsbygoogle) {
        const adsC = document.getElementsByClassName("adsbygoogle").length;
        for (let i = 0; i < adsC; i++) {
          try {
            ((window as any).adsbygoogle =
              (window as any).adsbygoogle || []).push({});
          } catch (e) {}
        }
      }
    };
  }, [router, router.pathname]);

I see you do something similar for ResponsiveAdUnit.tsx, but what about the auto ads spread all around the pages?

Thanks 🙂

@Yahav
Copy link

Yahav commented Aug 17, 2024

Have you ever figured out a way to "reload" the Auto Ads on page change?

@maxeth
Copy link
Author

maxeth commented Aug 17, 2024

Have you ever figured out a way to "reload" the Auto Ads on page change?

I didn't. I switched to anchor tag navigation. Not that dramatic on a redactional/blog-type website. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants