LaughingMan

Google Ads - Auto-size your ads

Jun 1st, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ShowGoogleAds() {
  2.     var ads = $('.adContainer');
  3.     $('.adContainer > ins').remove();
  4.     setTimeout(function () {
  5.         ads.each(function () {
  6.             GetGoogleAd($(this), $(this).data("istext"), $(this).data("isvertical"));
  7.         });
  8.     }, 0);
  9. }
  10.  
  11. function GetGoogleAd(ad, istext, isvertical) {
  12.     if (ad == undefined) {
  13.         console.log("The element to place the Google ad inside is NULL!");
  14.         return;
  15.     }
  16.  
  17.     istext = istext == undefined ? false : istext;
  18.     isvertical = isvertical == undefined ? false : isvertical;
  19.  
  20.     var google_ad_client = "ca-pub-1231231231231231"; // Use your google ad ID here
  21.     var google_ad_slot = "1231231231"; // Use your own ad slot ID here
  22.  
  23.     var adWidth = ad.width();
  24.     var adHeight = ad.height();
  25.  
  26.     var google_ad_size;
  27.  
  28.     if (adWidth >= 970) {
  29.         google_ad_size = ["970", "90"]; /* Large Leaderboard */
  30.     }
  31.     else if (adWidth >= 728) {
  32.         if (istext)
  33.             google_ad_size = ["728", "15"]; /* Text - Horizontal Large */
  34.         else
  35.             google_ad_size = ["728", "90"]; /* Leaderboard */
  36.     }
  37.     else if (adWidth >= 468) {
  38.         if (istext)
  39.             google_ad_size = ["468", "15"]; /* Text - Horizontal Medium */
  40.         else {
  41.             if (isvertical)
  42.                 google_ad_size = ["336", "280"]; /* Large Rectangle */
  43.             else
  44.                 google_ad_size = ["468", "90"]; /* Banner */
  45.         }
  46.     }
  47.     else if (adWidth >= 320) {
  48.         if (isvertical)
  49.             google_ad_size = ["320", "180"]; /* Large Mobile Banner */
  50.         else
  51.             google_ad_size = ["320", "50"]; /* Mobile Banner */
  52.     }
  53.     else if (adWidth >= 300) {
  54.         if (isvertical)
  55.             google_ad_size = ["300", "600"]; /* Large Skyscraper */
  56.         else
  57.             google_ad_size = ["300", "200"]; /* Medium Rectangle */
  58.     }
  59.     else if (adWidth >= 250) {
  60.         if (isvertical)
  61.             google_ad_size = ["250", "250"]; /* Square */
  62.         else
  63.             google_ad_size = ["234", "60"]; /* Half Banner */
  64.     }
  65.     else if (adWidth >= 200) {
  66.         if (isvertical)
  67.             google_ad_size = ["200", "200"]; /*  */
  68.         else
  69.             google_ad_size = ["200", "90"]; /* RTL: Vertical X-Large */
  70.     }
  71.     else if (adWidth >= 180) {
  72.         if (isvertical)
  73.             google_ad_size = ["180", "150"]; /* Small Rectangle */
  74.         else
  75.             google_ad_size = ["180", "90"]; /* Vertical Large */
  76.     }
  77.     else if (adWidth >= 160) {
  78.         if (isvertical)
  79.             google_ad_size = ["160", "600"]; /* Wide Skyscraper */
  80.         else
  81.             google_ad_size = ["160", "90"]; /* Vertical Medium */
  82.     }
  83.     else if (adWidth >= 125) {
  84.         google_ad_size = ["125", "125"]; /* Button */
  85.     }
  86.     else if (adWidth >= 120) {
  87.         if (istext)
  88.             google_ad_size = ["120", "90"]; /* Vertical Small */
  89.         else {
  90.             if (isvertical)
  91.                 google_ad_size = ["120", "600"]; /* Skyscraper */
  92.             else
  93.                 google_ad_size = ["120", "240"]; /* Vertical Banner */
  94.         }
  95.     }
  96.     else
  97.         return;
  98.  
  99.     var adInsert = '<ins class="adsbygoogle" style="display:inline-block;width:'
  100.         + google_ad_size[0] + 'px;height:'
  101.         + google_ad_size[1] + 'px" data-ad-client="'
  102.         + google_ad_client + '" data-ad-slot="'
  103.         + google_ad_slot + '"></ins>';
  104.     var scriptInsert = '<script type="text/javascript">(adsbygoogle = window.adsbygoogle || []).push({});</script>';
  105.  
  106.     ad.append(adInsert);
  107.     ad.append(scriptInsert);
  108. }
Add Comment
Please, Sign In to add comment