Skip to content

Load the Google AdSense external script to my remix site. #8570

Answered by KienTPhan
anuragnandi asked this question in Q&A

You must be logged in to vote

After manually adding the script in a useEffect, I use setTimeout before pushing the ad and this seems to work for me. This is inside a component I made to hold an AdUnit.

const handleAds = () => {
    if (!document.getElementById("adsbygoogleaftermount")) {
      const script = document.createElement("script");
      script.id = "adsbygoogleaftermount";
      script.type = "text/javascript";
      script.async = true;
      script.src =
        "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-xxxxxxxxxxxxxxx";
      script.crossOrigin = "anonymous";
      document.head.appendChild(script);
    }

    setTimeout(() => {
      // @ts-expect-error adsbygoogle ex…

Replies: 5 comments 2 replies

You must be logged in to vote
0 replies

You must be logged in to vote
1 reply
@anuragnandi

You must be logged in to vote
0 replies

You must be logged in to vote
0 replies

You must be logged in to vote
1 reply
@anuragnandi

Answer selected by anuragnandi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants