You are right, the sizes are estimated only at page loading. If you want to get a new screen width after you rotate the device, you will need to get the width in the resize call (and possibly override ad slots with these sizes).
$(window).resize(function(){ var width = document.documentElement.clientWidth; var size; if (width >= 320 && width < 728) size = [320, 50]; // smartphones else size = [728, 90]; // desktops and tablets var size2; if (width >= 320 && width < 728) size2 = [180, 150]; // smartphones else size2 = [160, 600]; // desktops and tablets var slot1=googletag.defineSlot('/XXXXXXX/tp_home_topboard', size, 'div-gpt-ad-1380075538670-3').addService(googletag.pubads()); var slot2=googletag.defineSlot('/XXXXXXX/tp_home_skyscraper', size2, 'div-gpt-ad-1380222668349-0').addService(googletag.pubads()) googletag.pubads().enableSingleRequest(); googletag.pubads().refresh([slot1, slot2]); });
This has not been verified, but I think it should lead you in the right direction. If rewriting ad slots won't work, you can also consider creating four different ad slots at the beginning and show only those based on the current screen size (and hide the rest).
source share