• Resolved mojamba

    (@mojamba)


    This may have been asked before but I couldn’t find it with a search.

    When I add AdSense to multiple blocks, the adsbygoogle.js file:

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

    gets added to the HTML multiple times.

    I would like to only include it once. I know I could put it in the Header (<head></head>) section separately, which is sort of a solution. BUT, I also uses filters (device, URL) so that there are times when I want no ads to show on a page. In that case, if I put the .js file in the <head> it will load even though it isn’t actually needed, which is inefficient.

    I have come up with a solution, which works but is a bit complicated. I am wondering if there is some simple solution that already exists in the settings that I just didn’t think/know about?

    • This topic was modified 3 years, 11 months ago by mojamba.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Spacetime

    (@spacetime)

    You can easily separate header and body code:
    https://adinserter.pro/documentation/header-and-footer-code#header-code-in-blocks

    When the block is not inserted (serverside) the header code is also not inserted.

    Thread Starter mojamba

    (@mojamba)

    Thanks. I thought there might be something I was missing. But, I wonder if that will really do what I want? I will try it out, but the documentation says:

    “When a block is inserted multiple times then the head code is also inserted multiple times. In some cases this may be undesirable. In such cases you can specify value once for the HEAD separator so the head code will be inserted only once”

    That makes sense and is good for ONE block inserted multiple times. But, what if you have two or three blocks all inserted different places on the same page? Won’t the header code for each be included? And, if that is the same .js file, that means it will be included multiple times, right?

    Or, am I misreading that?

    Plugin Author Spacetime

    (@spacetime)

    If it is the same block then it will be inserted once (if set so).
    But what is the problem if the code is inserted more than once?

    Thread Starter mojamba

    (@mojamba)

    My concern is page speed optimization. My understanding is that some browsers will load and/or execute multiple instances of the same JavaScript file multiple times. That is inefficient and hurts page speed. So, I am trying to avoid it. As I said, I found my own workaround solution but I was just wondering if there is a solution that already exists in your code/settings. If not, perhaps you might consider adding some logic to prevent the same JavaScript file from being called more than once in a future version? Either way, your plugin is fantastic (will be leaving a 5-star review) so thanks.

    Plugin Author Spacetime

    (@spacetime)

    There is no impact on page loading as the script is loaded asynchronously.

    I wouldn’t care much as Google would already suggest different code.

    Thanks for your comments 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add adsbygoogle.js file only once?’ is closed to new replies.