Forum Moderators: martinibuster

Message Too Old, No Replies

AdSense+AMP: Ads in the middle of the article?

How to correctly implement AdSense in AMP Pages

         

Tech4

6:21 pm on Apr 1, 2020 (gmt 0)

5+ Year Member



Hi guys!
I was postponing this issue for about two years, but decided to turn to studying it after looking through GA reports. I see that the majority of my visitors attend AMP pages but the majority of income is from non-AMP pages. Auto ads on AMP pages works bad for some reason and the AMP WP Plugin by Kaludi that I use allows to insert ads before the content or after it but not in the middle. Long story short, I often see that some websites successfully insert their ads before H2 or a certain paragraph.
Can anybody suggest a well-working free or paid WP plugin that is capable of correctly inserting AdSense ads in the middle of a post? Thank you in advance.

lammert

8:44 pm on Apr 1, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why use a plugin, if you can just paste the ad code on the exact location on the pages where you want? Google has a support page for implementing the ad code for Amp compatible ads [support.google.com]. If you really want to do it through a plugin, there are multiple AMP AdSense plugins [wordpress.org] available for Wordpress in their plugin repository.

Broaster

5:07 am on Apr 7, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



I thought AMP doesn't allow ads

Do you run a google news website?

Putting an Ad in the article will increase the ad click right for sure.

Saver

4:04 pm on Apr 7, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



This may help, I have been using this code to put into my functions.php for for atleast half a decade now, however I have not tested this on Amp pages. Perhaps if you have a functions.php file for your amp display, then you maybe able to add this to the bottom of it, just add your adsense code (where it says put your adsense code here) and to control how many paragraphs / line breaks to have before displaying the ad. Which is currently set to 3.


add_filter('the_content', 'prefix_insert_post_ads');

function prefix_insert_post_ads($content) {

$insertion = 'put your adsense code here';

if (is_single() && ! is_admin()) {
return prefix_insert_after_paragraphs($content, $insertion, array(3));
}

return $content;

}

function prefix_insert_after_paragraphs($content, $insertion, $paragraph_indexes) {

preg_match_all('#</p>#i', $content, $matches, PREG_SET_ORDER+PREG_OFFSET_CAPTURE);

$matches = array_map(function($match) {
return $match[0][1] + 4; // return string offset + length of </p> Tag
}, $matches);

rsort($paragraph_indexes);

foreach ($paragraph_indexes as $paragraph_index) {
if ($paragraph_index <= count($matches)) {
$offset_position = $matches[$paragraph_index-1];
$content = substr($content, 0, $offset_position) . $insertion . substr($content, $offset_position);
}
}

return $content;

}

Saver

6:03 pm on Apr 26, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Has anyone tried the above code on AMP templates?