How to place In-feed ad code on your page

After you create an In-feed ad, the next step is to place the code inside the HTML of your feed.

Be sure to place your In-feed ad code inside your feed. Placing your In-feed ad code outside your feed could result in poor ad performance and could cause you to violate AdSense policies.

Where to place your In-feed ad code

Place your In-feed ad code in between the content of your feed, or where your feed begins or ends. You can either do this by directly editing the HTML code of your feed or, if you're using a Content Management System (CMS), you might be able to use a plugin to insert the code for you.

An example In-feed ad setup might look like this:

 

Content block 1:

 

Content block 2:

 

Native In-feed ad:

 

Content block 3:

 

Content block 4:

If you want to place more than one ad in your feed you can either create multiple In-feed ads or place the same In-feed ad code multiple times in your feed.

How to place your In-feed ad

Typically, your CMS will have 1) a database that contains your content and 2) a piece of code that runs your content through a loop, creating your feed. You can find this piece of code in your template file, which will be stored in your CMS's template folder (sometimes called a theme folder).

Modify the code in your template file

To place your In-feed ad, modify the code in your template file like this:

  1. Open the template file of the page where you have your feed.
  2. Find the loop that creates your feed. (Hint: The loop code will most likely contain a "while" or a "for each" command.)
  3. Modify the loop code to insert your In-feed ad into your feed, specifically:
    • If your loop doesn't have a counter, add a counter to set how frequently you want your In-feed ad to be inserted.
    • Add an "if" statement inside the loop to make your In-feed ad appear at the frequency you defined in the counter.
For specific instructions for your CMS, we recommend you search Google.

We've provided some pseudo code below. Your code will have a similar structure to this pseudo code but, depending on which programming language you're using, your actual implementation will likely be different.

Posts[] posts;
Var count=1; // If your code does not include a counter define one
While (count < posts.length) // Increase the value of "count" by 1 until the final post in the database
{

   If (count%3=0) //If the value of "count" equals 3 or 6 or 9 insert the ad
   {
   <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
   </script>
   <ins class="adsbygoogle" style="display:block"
   data-ad-format="fluid"
   data-ad-client="ca-pub-1234567891234567"
   data-ad-slot="1234567890"></ins>
   <script>
   (adsbygoogle = window.adsbygoogle || []).push({});
   </script>
   }

   <h3>{{ post.title }}</h3> // for all count values insert the content block title
   <p>{{ post.body }}<p> //Insert the content block body
   <img src="{{ post.thumbnail}}"> //Insert the content block image

}

Important

Make sure that you place your ad code within a parent container (<div>, <iframe>, etc.) that has the following:

  • A valid width. If you place your ad code within a parent container that doesn’t have an explicit width set, e.g., in a floating element, your ads won't show. We use the width of the parent container to calculate the width for your ads. Note that the minimum width for In-feed ads is 250px.
  • A variable height. If you place your ad code in a parent container with a fixed height, your ads might be distorted.
Was this article helpful?
How can we improve it?