Whenever I run Google Page Speed or YSlow, many of the optimisations suggested stem from my use of Google Adsense and Google Analytics.

For example, YSlow says there are "9 external Javascript scripts" - I only have one on my site, so the rest are obviously from either adsense or analytics. Also under "Reduce DNS lookups", these domains are listed:

ad.doubleclick.net s0.2mdn.net
pixel.invitemedia.com adadvisor.net

Is there anything I can do to reduce these or make other improvements to the speed?

7 Answers 7


Google Analytics can be set up so that it loads asynchronously, which will help improve the situation. It doesn't look like they currently have a similar script for AdSense.

  • Are you really seeing a significant delay in page load times because of Google adsense? I have doubts about that.
    – delete
    Commented Jul 25, 2010 at 5:09
  • On older browsers or poorly designed sites, AdSense could end up as a blocking script, preventing other page elements from downloading until it's finished. It could conceivably also affect the page paint time. I haven't noticed it much; however, this wasn't my question, it was DisgruntledGoat's. :) Commented Jul 25, 2010 at 5:59
  • @Kinopiko: No, I haven't experienced big delays, although if my connection slows a bit then it is noticeable when the ads are loading. As the question says, I'm asking since the recommendations in Yslow/PageSpeed are mostly related to elements I'm not sure I can control. Commented Jul 25, 2010 at 15:55
  • This may be micro-optimization, but if you like the asynchronous Google Analytics snippet - check out my answer for an even faster version: webmasters.stackexchange.com/questions/1555/…
    – leek
    Commented Sep 3, 2010 at 8:29

Loading Google Analytics Asynchronously is a good start. Beyond that, how long does it take your page to load? If it takes you page less than 2 seconds then you may not want to spend the time optimizing it any more.

If you are concerned that it would load slower from elsewhere, checkout http://www.webpagetest.org/ to test you page against other locations around the world and other internet speeds. This page will also show you how long each chunk of your site is taking to load.

You have to remember that it is probably not useful to make your page load at a "perfect speed" because the cost in time and energy is too high. If you can get your page below 2 seconds, according to Google, you will be in the top 20% of websites and that is probably good enough. If not, look where else you can increase you page speed. Sometimes fixing a bunch of smaller things will make you site fast enough.

The best way to put it, I think, is pick the low hanging fruit and Google Adsense isn't low hanging really. Analytics is and should be easy enough to fix by making it asynchronous and maybe you should leave it at that.


If you like the asynchronous Google Analytics script, check out Mathias Bynens optimization to it here. Obviously, you should keep up to date on whether Google changes the snippet significantly - but for now, his optimizations yields a 40% speedup (for me) over Google's version of the same script.

  • excellent, we'll be looking into this as a general code pattern for JavaScript we want to load but not block (ads, basically..) Commented Sep 3, 2010 at 8:35

Generally a lot of these ad scripts are 'cache-busted' purposefully, so that a hit is made to the server every single time to register the view.

Unfortunately just using adsense means that you've opened yourself up to a hell of a lot of external javascripts and there's really not much you can do about it other than decreasing the number of ad zones (TOS only allows 3 per page anyway) or just not running adsense.

You could try wrapping up your google ads javascript in your own hosted javascript for each zone that did a document.write and output the required code. I'm pretty sure this would cause it to load after the body has loaded and therefore mean your page will load first.


Just one point - there is a new version of google analytics code which should improve it's loading speed - so it might be worth changing to that.


If this is an issue, what you can do is load the page without those scripts, and then import the scripts into the page using ajax, after the page is fully loaded

For instance, let's say you have an empty div on your page, like this:

<div id="adsenseBanner"></div>

And you then append the javascript like this:


I added a JavaScript alert so you can see that it is being executed, but of course, you would replace it with the scripts you need to load.

This way, you can be sure that these external resources don't slow down your page load... at the cost of loading one extra script.

  • I didn't know $() was a standard JavaScript function...
    – Tom
    Commented Sep 3, 2010 at 12:17
  • @Tom: It's not, it's JQuery. Is there anyone foolish enough to code straight in JS these days?
    – Sylverdrag
    Commented Sep 6, 2010 at 15:12
  • Not everybody uses jQuery by default
    – Tom
    Commented Sep 9, 2010 at 10:07
  • @Tom: that was tongue in cheek. Yes, it would have been more accurate to say "JQuery" rather than JavaScript.
    – Sylverdrag
    Commented Sep 12, 2010 at 6:08

Implement to loading Google Adsense script when page finished following steps below:

First step: Remove default script from Google Adsense snippet:

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

Second step: Create function to inject this to bottom of page right before body:

function inject3rd() {
  // Defer loading Google Adsense
  var adsen = document.createElement('script');
  adsen.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-0374760140114293';
  adsen.crossorigin = 'anonymous';

  // Show GA units
  [].forEach.call(document.querySelectorAll('.adsbygoogle'), function(){
    (adsbygoogle = window.adsbygoogle || []).push({});

Final step: Call this function:

if (window.addEventListener) {
  window.addEventListener('load', inject3rd, false);
} else if (window.attachEvent) {
  window.attachEvent('onload', inject3rd);
} else {
  window.onload = inject3rd;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.