I need to insert a DFP ad (Medium Rectangle) in between some dynamic contents. The contents load via AJAX and later populated as below in .items.
<div class="items">
<div>The DFP ad</div>
<div>New content</div>
<div>New content</div>
<div>New content</div>
<div>New content</div>
</div>
I need the ad to be displayed after third the dynamic content for example. Changing via JS (Jquery) causes erratic ad rendering, mostly just leaving the ad to be empty.
If I got the question correctly you just need to add an ad slot dynamically.
Here's a working example that dynamically creates ad slots: https://github.com/ffeast/gpt-tests/blob/master/dynamic_slots.html
And here it is live: https://jsfiddle.net/Lpqtrtgu/
This is the excerpt that creates a new ad slot and renders it:
The official docs on how to handle such cases in DFP: https://support.google.com/dfp_premium/answer/4578089?hl=en