Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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:

pagead2.googlesyndication.com
googleads.g.doubleclick.net
www.google-analytics.com
altfarm.mediaplex.com
img-cdn.mediaplex.com
eu.bid.invitemedia.com
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?

share|improve this question
add comment

6 Answers 6

up vote 7 down vote accepted

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.

share|improve this answer
    
Are you really seeing a significant delay in page load times because of Google adsense? I have doubts about that. –  delete Jul 25 '10 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. :) –  Virtuosi Media Jul 25 '10 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. –  DisgruntledGoat Jul 25 '10 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 Sep 3 '10 at 8:29
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
excellent, we'll be looking into this as a general code pattern for JavaScript we want to load but not block (ads, basically..) –  Jeff Atwood Sep 3 '10 at 8:35
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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:

$(document).ready(function(){
    $('#adsenseBanner').append('<script>alert("this");</script>')
});

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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