Only Include one adsbygoogle.js on Your Pages
I recently found out I was repeating code provided by Google. FYI you should only place one adsbygoogle.js file per HTML page.
By. Jacob
Edited: 2018-07-04 13:15
Recently I was looking into some stuff related to Adsense ads, and happened to notice I was including adsbygoogle.js multiple times on each page. I think it is unlikely this will significantly affect the speed of my site in any way, but nevertheless, it is just redundant, and a bad practice when following the DRY (Don't repeat yourself) in web development.
Logically, you do not need to load a .js file multiple times. But, just to be sure, I first investigated if it was really necessary to have multiple copies of the file, and quickly found the following in the Adsense FAQ:
If I have multiple ad units on a page, do I have to include <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> for each ad unit?
No, this is not necessary, adsbygoogle.js can be loaded once.
Placement of adsbygoogle.js in the HTML
The adsbygoogle.js file should only be loaded once. We may place it in the head part of the HTML, like shown in the below example:
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
I quickly modified the relevant CMS code, and created a custom field for inserting the code in the head, rather than along with each Ad-unit. I can not help you with how to do this in your own CMS, but in Drupal and Wordpress, this should be fairly easy to do, so I recommend you search around and see what other people have to say.
It is generally never a good idea to include files multiple times, except when you really need to do it. For example, decorative images can be base64 encoded and embedded directly in your HTML/CSS files, which will save HTTP requests and speed up your website. I would really only use img for actual images, such as an article image or images in the content. Everything else should likely be embedded in the CSS.
For me personally, I think what happened was, I just tend to insert my ad-code exactly as provided, without modification, because that was how I learned to do it when I first joined Adsense. It turns out, you are allowed to make certain modifications, you just have to be careful, and check with their FAQ and forums if your changes are acceptable. This can be important, because modifying the code in a incorrect way can lead to termination of your account (Or so I was informed in the past).
Anyway, I am glad I got this mess cleaned up. It surprises me I did not notice earlier, as I usually have very high attention to detail when coding. The CMS I am using is home-build in PHP, so I need to create most things on my own, including functions to insert ads dynamically on the server-side.
Links
- About the asynchronous ad code - support.google.com
Tell us what you think: