AdSense for content (AFC)

Modification of the AdSense ad code

Our program policies do not permit any alteration to AdSense code which artificially inflates ad performance or harms advertiser conversions. Your publisher account offers a number of options when generating the ad code, which we hope will allow you to create an ad layout that fits in with your site. In general, we recommend copying and pasting the ad code. In some situations though, we understand that modifications are crucial to a clean user experience.

Techniques to avoid

Here are some techniques you’ll want to avoid:

  • Hiding ad units at anytime (e.g., display:none)
  • Implementing AdSense ad code in a way that covers content
  • Using any means to force the display of more than three AdSense ad units on a page
  • Manipulating the ad targeting using hidden keywords, IFRAMEs, or any other method
  • Distributing ads in emails or software
  • Floating ads or units that slide to attract unwarranted attention

Acceptable modifications

Here are some acceptable modifications:

Responsive design

The new asynchronous ad code allows you to set the size of an ad through CSS, and optionally media queries, to fit in with your responsive website. Learn more about how to use responsive ad units.

A/B testing

AdSense experiments allow you to compare the performance of different ad type and/or text ad style settings of your ad units. Experiments help you make informed decisions about how to configure your ad units and can help you to increase your earnings. Learn more about experiments.

Should you need more control over your A/B tests, please see the following code snippets:

  • Synchronous ad code:
    <script type="text/javascript">
        var random_number = Math.random();
        google_ad_client = "ca-publisher-id";
        google_ad_width = 728;
        google_ad_height = 90;

        if (random_number < .5){
            google_ad_slot = "1234567890";
        } else {
            google_ad_slot = "2345678901";
        }

    </script>
    <script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
  • Asynchronous ad code:
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
        style="display:inline-block;width:728px;height:90px"
        data-ad-client="ca-publisher-id">
    </ins>
    <script>
        if (Math.random() < .5) {
          mySlotId = '1234567890';
        } else {
          mySlotId = '2345678901';
        }
        (adsbygoogle = window.adsbygoogle || []).push({
          params: { google_ad_slot: mySlotId }
        });
    </script>

Setting custom channels dynamically

  • Synchronous ad code:
    <script type="text/javascript">
        var channel_condition = object.booleanMethod();
        google_ad_client = "ca-publisher-id";
        google_ad_slot = "1234567890";
        google_ad_width = 728;
        google_ad_height = 90;
        if (channel_condition){
            google_ad_channel = "123457789";
        } else {
            google_ad_channel = "263477489";
        }

    </script>
    <script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
  • Asynchronous ad code:
    <script async src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
    <ins class="adsbygoogle"
        style="display:inline-block;width:728px;height:90px"
        data-ad-client="ca-publisher-id"
        data-ad-slot="1234567890">
    </ins>
    <script>
        channel_condition = object.booleanMethod();
        if (channel_condition) {
          my_google_ad_channel = '123457789';
        }
        else {
          my_google_ad_channel = '263477489';
        }
        (adsbygoogle = window.adsbygoogle || []).push({
          params: { google_ad_channel: my_google_ad_channel}
        });
    </script>

Ad tag minification (make sure to remove tag comments)

  • Synchronous ad code:
    <script type="text/javascript">
        google_ad_client = "ca-publisher-id";
        google_ad_slot = "1234567890";
        google_ad_width = 728;google_ad_height = 90;
    </script>
    <script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
  • Asynchronous ad code:
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
        style="display:inline-block;width:728px;height:90px"
        data-ad-client="ca-publisher-id"
        data-ad-slot="1234567890"></ins>
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

For details on what alterations you're permitted to make to AdSense for search code, see the AdSense for search policies.