How to Use Google AdSense Ads on Responsive Websites

How to Use Google AdSense Ads on Responsive Websites

Learn how to make your Google AdSense ads responsive with JavaScript. The code detects the size of the visitor’s browser and serves appropriate AdSense ads that fit in the available space.

couch mode print story

Google AdSense has officially approved Responsive Design and what that means is you can serve Google ads of varying dimensions corresponding to the viewport size (screen resolution) of the visitor’s device.

For instance, if a visitor is reading your web page on desktop, you can choose to serve the large 728×90 (leaderboard) unit but if another visitor is viewing the same web page on a mobile phone, you can display the smaller 468×60 ad unit. The AdSense code detects the size of the visitor’s screen and serves the appropriate ad unit that will best fit the available space.

You can serve responsive Google AdSense ads in both synchronous as well as asynchronous (non-blocking) fashion. The latter is a more efficient and recommended method as the JavaScript ad code loads in parallel and therefore does not block the other elements of the web page from rendering. In other words, your pages will load faster improving user experience.

How to Generate Responsive AdSense Ads

Open your AdSense dashboard and under My Ads, click “Create new ad unit.” Set Ad Size as “Responsive Ad Unit” and click the “Save and Get Code” button to generate the JavaScript code for your Responsive AdSense ad. The default code is something like this:

 
 
  1. <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  2. <ins class="adsbygoogle"
  3. style="display:block"
  4. data-ad-client="ca-pub-12345"
  5. data-ad-slot="xxxxx"
  6. data-ad-format="auto"></ins>
  7. <script>
  8. (adsbygoogle = window.adsbygoogle || []).push({});
  9. </script>

AdSense computes the available width and renders the largest ad that will fit that space. This may not always be the most appropriate approach since the best performing ads are rectangles and skyscrapers and not necessarily the leaderboards.

There is however an option to force the ad unit to always serve the rectangle or the skyscraper. You can change the value of the data-ad-format variable in the JavaScript code from “auto” to “rectangle” and it will always serve one of the rectangular formats. Similarly, you can set data-ad-format to “vertical” to always render a 120×600 or 160×600 rectangle. The modified ad code would be:

 
 
  1. <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  2. <ins class="adsbygoogle"
  3. style="display:block"
  4. data-ad-client="ca-pub-12345"
  5. data-ad-slot="xxxxx"
  6. data-ad-format="rectangle"></ins>
  7. <script>
  8. (adsbygoogle = window.adsbygoogle || []).push({});
  9. </script>

Responsive AdSense Ads (Another Approach)

Whether you set data-ad-format as “auto” or “vertical” or “horizontal” or vertical, the Google AdSense algorithms will still decide which ad to serve. For instance, if you ask for an rectangle, you may either get a medium rectangle or a large rectangle.

SHould you wish to force AdSense to serve ads of a particular size while staying responsive, you can consider custom-sized ads. The size of these ads is determined based on the screen but the publisher has more control over the banner size that is served.

 
 
  1. <div id="google-ads-1"></div>
  2.  
  3. <script type="text/javascript">
  4.  
  5. /* Calculate the width of available ad space */
  6. ad = document.getElementById('google-ads-1');
  7.  
  8. if (ad.getBoundingClientRect().width) {
  9. adWidth = ad.getBoundingClientRect().width; // for modern browsers
  10. } else {
  11. adWidth = ad.offsetWidth; // for old IE
  12. }
  13.  
  14. /* Replace ca-pub-XXX with your AdSense Publisher ID */
  15. google_ad_client = "ca-pub-XXX";
  16.  
  17. /* Replace 1234567890 with the AdSense Ad Slot ID */
  18. google_ad_slot = "1234567890";
  19. /* Do not change anything after this line */
  20. if ( adWidth >= 728 )
  21. google_ad_size = ["728", "90"]; /* Leaderboard 728x90 */
  22. else if ( adWidth >= 468 )
  23. google_ad_size = ["468", "60"]; /* Banner (468 x 60) */
  24. else if ( adWidth >= 336 )
  25. google_ad_size = ["336", "280"]; /* Large Rectangle (336 x 280) */
  26. else if ( adWidth >= 300 )
  27. google_ad_size = ["300", "250"]; /* Medium Rectangle (300 x 250) */
  28. else if ( adWidth >= 250 )
  29. google_ad_size = ["250", "250"]; /* Square (250 x 250) */
  30. else if ( adWidth >= 200 )
  31. google_ad_size = ["200", "200"]; /* Small Square (200 x 200) */
  32. else if ( adWidth >= 180 )
  33. google_ad_size = ["180", "150"]; /* Small Rectangle (180 x 150) */
  34. else
  35. google_ad_size = ["125", "125"]; /* Button (125 x 125) */
  36.  
  37. document.write (
  38. '<ins class="adsbygoogle" style="display:inline-block;width:'
  39. + google_ad_size[0] + 'px;height:'
  40. + google_ad_size[1] + 'px" data-ad-client="'
  41. + google_ad_client + '" data-ad-slot="'
  42. + google_ad_slot + '"></ins>'
  43. );
  44. (adsbygoogle = window.adsbygoogle || []).push({});
  45.  
  46. </script>
  47.  
  48. <script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
  49. </script>

Go to your AdSense dashboard and either create a new ad unit or use one of your existing ad units. Make a note of the ID of your ad unit and also your AdSense Publisher ID and paste these values in Line #15 and #18.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值