Taming the GAM beast


One of the things I have to do in my day-job is manage our Google Ad Manager system, which we call GAM. One of the downsides of using GAM (actually a lot of different ad management systems) is their use of the javascript function document.write(), as well as their requirement to load in a big whomping load of javascript in the <head> of the page. One of the keys of website optimisation is to load all javascript at the end of your HTML page, so that the webpage renders as quickly as possible.

The way we can deal with this is overload the document.write() function to bend to our own wicked ways. Normally, you load the GAM code at the top of the page, but we’ll put it at the bottom and use a neat little function I wrote.

function insertAds(AdSlotName, width, height)
{
var slot = document.getElementById('my_ads_'+AdSlotName);
if (!slot)
return;
var oldwrite = document.write;
document.write = function(text){slot.innerHTML+=text};
GA_googleFillSlotWithSize("ca-pub-yournumhere", AdSlotName, width, height);
document.write = oldwrite;
}

Now we just slap in a <div> with our ad slots name, so we’ll replace
<script type="text/javascript">GA_googleFillSlotWithSize("ca-pub-yournumhere", "AdSlotName-760x120", 760, 120);</script>
with
<div id="my_ads_AdSlotName-760x120">&nbsp;</div>
(which will render faster)
Then load in the ad at the bottom of the page:
<script type='text/javascript'><!--
insertAds("AdSlotName-760x120", 760, 120);
// --></script>

And there you have it. You can take out some more javascript from your <head> and speed things up a bit. My own speed tests showed between one-and-a-half to two seconds decrease in start-rendering time. Would be interested to hear your own findings.

  1. #1 by Lee on 2010-02-03 - 4:24 pm

    I’ve been trying to do this, without success. Maybe something has changed on Google’s end? Or maybe I’m doing something wrong? Can you verify that this trick still works for you?

  2. #2 by Eion on 2010-02-03 - 7:07 pm

    Lee : I’ve been trying to do this, without success. Maybe something has changed on Google’s end? Or maybe I’m doing something wrong? Can you verify that this trick still works for you?

    Nope, still working. Check http://myjobspace.co.nz/ or http://www.dinesmart.co.nz/ for a demo

  3. #3 by Daniel on 2010-11-23 - 4:11 pm

    I’ve just loaded up http://myjobspace.co.nz/ and it looks like you aren’t using this any more. Did it stop working?

    • #4 by Eion on 2010-11-23 - 4:49 pm

      I’m no longer developing that site any more, and it looks like the new developers have disabled it.

Comments are closed.