Forum Moderators: open

Message Too Old, No Replies

Loading Adsense inside of a <script>

         

csdude55

9:15 am on Apr 17, 2017 (gmt 0)

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



I swear, guys, sometimes I think I ask the most remedial questions! lol

I'm trying to load SYNC Adsense tags within a <script>. This is what I have:

document.write('<div data-b="' + lB + '" class="med_rect" style="margin: 10px 0 20px 0">');

google_ad_client = "ca-pub-xxxx";
google_ad_slot = "xxxx";
google_ad_width = 300;
google_ad_height = 250;

document.write('<scr' + 'ipt lang' + 'uage="jav' + 'ascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"></scr' + 'ipt>');
document.write('</div>');


In reality, this is inside of a loop, so I show one banner, followed by an Ajax call to some local content, followed by a second banner, and so on until the end of the page.

The first Adsense banner loads, but then everything after that fails. If I remove the chopped up document.write('<scr' + 'ipt lang'... line, though, everything else works correctly. So my problem is definitely there.

Any suggestions?

NickMNS

4:14 am on Apr 19, 2017 (gmt 0)

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



Why would anybody scroll all the way down 50000px? Most users will never see those ads, and pasting ads all the way down a page is an even worse idea. You will destroy your AVV numbers doing this. Not to mention that the page will be so big it will likely crash the browser for users with insufficient memory. For really long content either paginate or infinite scroll the content (while removing content that has already been seen).

csdude55

5:21 am on Apr 19, 2017 (gmt 0)

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



50000px was just a random number... but for the record, I have the forum on WebmasterWorld set to show as many posts on a page as possible, and this page is over 20000px. Showing a new banner every 800px would be 25 banners.

My sites have message boards, too, and some of the posts can get very long... especially when a user copies a news article into the thread. And I was thinking about setting up infinite scroll to eliminate paginating, which could result in very, very long pages.

So maybe I should only move the banners to the dynamic DIV when they have scrolled near that section? That shouldn't be too hard, I could just modify it using $(window).scrollTop()

keyplyr

5:50 am on Apr 19, 2017 (gmt 0)

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



Yes the max number of ads was lifted.

But as the last update (so called Fred) showed, Google may devalue a page with too many ads and not enough rich content. Google may also limit the number of ads displayed.

the content you provide should add value and be the focal point for users visiting your page. For this reason, we may limit or disable ad serving on pages with little to no value and/or excessive advertising until changes are made.

[support.google.com...]

csdude55

7:18 am on Apr 19, 2017 (gmt 0)

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



Would you think that one banner every 800px going along the right side of this page (in place of "My Threads", "Hot Threads", etc) would be too many in relation to rich content?

keyplyr

7:32 am on Apr 19, 2017 (gmt 0)

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



Don't think anyone really knows except Google. Maybe read what those who lost ranking say about it in the April SERP update thread [webmasterworld.com]

NickMNS

12:44 pm on Apr 19, 2017 (gmt 0)

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



I don't think the biggest issue will be too many ads from a Google penalty(algorithmic or even manual) perspective. This assumes that there is sufficient quality content to justify the ad placements and you are not clustering ads. Think about, you have a single page with content displayed vertically. You place three ads near the top, then nothing because you don't want to "over do it". Or, you take your same content and paginated it over three pages, then include three ads per page on each pages, because you can. No one would question the latter.

If your content can warrant 9 ads, then the type of layout should not really matter. In my example 9 ads is likely over doing it, but one ad for each block that would normally be paginated is not excessive by any objective measure.

The benefit of the paginated content is that the if the user reads the first page and realizes that the content sucks and is MFA, they never read on to the next pages, so 6 ad impressions are never credited to your account. Whereas, if you stack it all vertically and load 9 ads on page load. The 6 ads appearing deep below the fold are credited but never seen. Your AVV went from 66% (assuming two of three ads are seen on the first page of the paginated layout) vs, 22% (2/9) for the single page layout.

The issue is really viewability. The $(window)scrollTop() would solve that.

csdude55

2:12 am on Apr 22, 2017 (gmt 0)

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



Just for the sake of posterity, I sent the finished page to my AdX management company, who sent it to their Google rep. He replied with no changes at all, so it's apparently OK?

I'll probably still add the $(window).scrollTop() function to it, but he didn't have any concerns with the way it's loading now. I've noticed that the ads don't seem to load until their visible, anyway, so I'm kinda thinking that Google might have already implemented their own version of lazy loading that would make my work irrelevant.

csdude55

8:48 am on Apr 26, 2017 (gmt 0)

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



Continuing for posterity, even though the ad manager had no problem with the script, I still didn't like it so I've been working on it. After getting a little help from "galeksic" on the Google support forum, I finally ended up using:

var scriptConf = document.createElement('script');
scriptConf.innerHTML = 'google_ad_client = "ca-pub-xxxx";google_ad_slot = "xxxx";google_ad_width = 300;google_ad_height = 250;';
document.write(scriptConf.outerHTML);

var scriptExec = document.createElement('script');
scriptExec.src = '//pagead2.googlesyndication.com/pagead/show_ads.js';
document.write(scriptExec.outerHTML);


I had tried using document.body.appendChild(script) before and it didn't work. I honestly don't know what the real difference between that and the document.write(scriptExec.outerHTML) command is, and I wish that I didn't have to use document.write() here... but it works.

I still had a problem in the loop, though; apparently, the ajax script was looking for the dynamic DIV before it was being created, so "loading" worked but not the ajax. It took DAYS to figure that out! But eventually I figured out to move the ajax scripts to a completely separate $(function() { });.

// Determine the numeric data-b to assign
var lB = $('[data-b]').last().data('b');

// Measure full height of <main> (rich content) </main>
var pH = document.querySelector('main').clientHeight;

// Determine how many iterations to have; 800px for each iteration
var sH = parseInt(pH / 800);

for (var i=0; i <= sH; i++) {
lB++;
document.write('<div data-b="' + lB + '" class="med_rect" style="margin: 10px 0 20px 0">');

var scriptConf = document.createElement('script');
scriptConf.innerHTML = 'google_ad_client = "ca-pub-xxxx"; google_ad_slot = "xxxx"; google_ad_width = 300; google_ad_height = 250;';
document.write(scriptConf.outerHTML);

var scriptExec = document.createElement('script');
scriptExec.src = '//pagead2.googlesyndication.com/pagead/show_ads.js';
document.write(scriptExec.outerHTML);

document.write('</div>');

document.write('<div id="local_' + lB + '">' + loading + '</div>');

// Had to move this to a separate function to load at the end
// $('#local_' + lB).ajax('local.php?lb=' + lB);
}

$(function() {
for (var j=0; j <= lB; j++) {
if ($('#local_' + j).length)
$('#local_' + j).ajax('local.php?lb=' + j);
}
});
This 38 message thread spans 2 pages: 38