I am in the progress of testing a setup of ajaxify and google adsense, which has been causing headaches to quite a lot of people. I will document my testing here, hoping it might be of value to someone.
As you will see, my approach is rather on the safe side concerning googles TOS. I do not guarantee google will agree about this, so please dont blame me should this turn out to be not the right solution for you or them.
Observation
When ajaxifying my project, I noticed the google banners didn't work as they used to.
To be precise, they would be displayed on the first page I refreshed, and on the next couple of pages I would browse to by ajaxified links, but after about the fourth page change, the banners seemed to "run dry" - there was no new ads fetched, and my project was showing white spaces where the ads were supposed to be.
Even during the first pages in which the ad spots were still populated, it was noticeable that adsense was messing up if the ad size varied from page to page- it would display narrow ads in wide spaces if no wide spaces had been present on the initial page.
Conclusion
Without ever looking into any code, I'd conclude something within the adsense script (the external javascript) does register the format of ads on the page it is requested by, and obviously prepares a certain amount of suitable ads. Without re-fetching the google js within my ajaxifiy page calls, it would fit in those prepared ads from the initial page load into my ad-slots, until all "prepared" ads have been shown.
Any attempt to re-fetch the google js to trigger fresh ads fails if it is done by ajax.
I assume they have a very simple and rudimentary security lock built in that just states: If anything tries to fetch this script via ajax, proceed without complaining, but dont show any ads either.
Thats googles politics, so lets just respect that for now.
Side-note: Since the refetching doesnt work, I actually prevented the output of
<script type='text/javascript' src='http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' async></script>
on server side when pages are fetched via ajaxy, see issue #30 )
Solution I - count along
My very first working solution was quite a crude hack:
Since ads would appear for around four pages, I just built in some kind of counter which counts along any page changes done with ajaxify.js loading, and which prevents the ajax load on each fourth page, just as if all links were suddenly given the "no-ajaxy" class - so I had three nice ajaxified page transitions and one standard page load which would say "hoohoo" to google and fetch new ads. Given, that is crude, but it worked, and since the users first impression of a sites behavior is settled after three page loads, I felt the occasional "harsh load" in between was still acceptable - and this solution didnt require any changes to my page other than the added counter functionality in the ajaxify file I customized.
If you do not have
- different ad formats on diffrent pages
- any other reason than a slightly compromised visual effect on every fourth page load
I'd say, use such a counter and be happy to see adsense and ajaxify get along.
However, I had to deal with both of above issues - different ad sizes and the fact that I wanted to rely on staying "ajaxified" all the time, mostly because I enjoy the presence of a chatbox outside of the #content which would disappear and need to be reopened on every fourth page load.
To say it straight away - I have not found any solution that would do without the eventual harsh load to refresh the google hookup, but I have come up with a solution that feels at least a bit less crude than the one mentioned above.
Lets go over things step by step:
Recognizing different layouts needing different ad sizes
A simple scenario: You have some pages with a big square ad, lets call those "layout A", and some pages with a narrow banner, "Layout B".
Using the count-along solution, you will always see the right ads as long as a user stays on pages of the same layout - if a user navigates from one layout to another by ajaxify, you might see portions of a square ad in a narrow spot and vice versa.
What we'll need on top of the count along to prevent that, is some kind of detection/comparison between the current pages layout and the layout of the page to come.
If that detects a layout change, it should again disable the ajax loading and cause a "harsh load" to make sure the diffrent ad layout requests a new set of ads.
How to do that?
on any page, add a line of script within the #content div to pass the current pages layout to javascript:
<script> window.layout = 'A'</script>
Next, we need to prepare the comparison between that variable and the layout of the page we are about to load.
I solved this by letting ajaxify check the URI to call for certain identifiers - for a simple example, lets assume all pages in the folders "products/" and "offers/" have layout A, and the page "home.php" has layout B, this looks like:
var target=0;
url = n.href; /* grabbing the URI from ajaxy function here, before it calles the ajax load */
if ((url.indexOf('products/') >= 0) || (url.indexOf('offers/') >= 0) {
target='A';
} else if ( url.indexOf('home.php') >= 0) {
target='B';
}
if (window.layout==target) {
/* target page has identical layout, let ajaxify proceed */
} else {
/* target page has diffrent layout, stop ajaxify = let harsh load happen*/
}
Of course, you can just ad "no-ajaxy" classes to all links that lead to a different layout page instead, but that may be a lot more work to do, and besides that, we'll be needing the layout detection by JS some more later.
Now, you may believe I am just disabling ajaxify on more and more instances, and depending on your page structure, using it like this may already seem absurd (if you have layout changes all the time, no call would ever be ajaxified). I was not happy about this myself, even tho this solution still guarantees to show google ads all the time and without annoying ad size messups.
Placing ads outside of the content
So, if at this point we are still not entirely happy, lets take the final step into a crazy implementation.
This last one has some benefits which I'll demonstrate, but also some clear downsides which I want to share right ahead:
- it will absolutely reduce the number of ad impressions noticed by google, so if you earn by impression rather than per-click, this will not be good for you. I will share my own testing results as soon as I have some reliable data.
- again, it will require "harsh load" on ad layout change.
It all comes down to the idea to move your ads out of the #content div, so they are loaded on the initial page once and just stay right where they are as a user navigates the ajaxified site.
Try it, you will be surprised how awesomly quick your page suddenly feels, because it doesnt take the extra second untill the ads appear :) Lets not forget, our users dont distinguish between document.ready and "this page looks ready", so by having ads that are right there, they will say your site is much quicker than before.
If you are using a fade effect, you might also enjoy the way anything but the ads fades in and out, one might possibly even get better click thru rates because of this.
Positioning out-of-content ads on the content
Now, in my case my best performing ad block happens to be within the #content, so I really felt bad about letting go of that. So again, heres a workaround for this case:
I added another <div> and let it float:left of my #content div with position:relative .
within this container, I added a second div which holds the google ad.
By assigning position:absolute and top:123px;right:123px to this ad holding div, it can be placed to the spot inside the #content div where it belongs.
edit on oct 29th - If you are worried about positioning ads with position:absolute - that is actually something google promotes! Here: https://support.google.com/adsense/answer/187769?hl=en
summed up that looks like this:
<div id='content'>
/* your content */
</div>
<div id='adpositioner' style='position:relative;float:left'>
<div id='centerad' style='position:absolute;top:123px;right:123px'>
<ins /*google - ad*/>
</ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
</div>
Of course you may move the 'centerad' div around by jQuery, since adsense bot might not do that I felt more on the safe side with this approach which places the ads at a given position on page load.
Since my page content is dynamic and ad spaces move down depending on the content text length,
I have added a slight placing correction via jQuery on top of that, but to make sure googlebot can tell if an ad is "above fold" or not, I believe the js-independent placement is crucial.
All that being said and looking really promising, I have still found no way to deal with the need for diffrently sized ads on diffrent page layouts than the "listen for layout changes and do a harsh load if necessary" move explained above.
I'll be sure to update you on the effect of loading fewer ads and showing them a longer time as used by this solution. My first impression when surfing around was a very positive user experience.
Testing results (updated on oct 28th)
Well, after about two weeks of testing, I believe its safe to say that I experienced no negative monetary effects from implementing adsense the way described in detail above.
The number of counted ad impressions gets reduced as expected, but since I am making most revenue by CPC, there has been no difference in earnings since the implementation.
have a look at http://www.wikiloops.com to see it in action.
I am in the progress of testing a setup of ajaxify and google adsense, which has been causing headaches to quite a lot of people. I will document my testing here, hoping it might be of value to someone.
As you will see, my approach is rather on the safe side concerning googles TOS. I do not guarantee google will agree about this, so please dont blame me should this turn out to be not the right solution for you or them.
Observation
When ajaxifying my project, I noticed the google banners didn't work as they used to.
To be precise, they would be displayed on the first page I refreshed, and on the next couple of pages I would browse to by ajaxified links, but after about the fourth page change, the banners seemed to "run dry" - there was no new ads fetched, and my project was showing white spaces where the ads were supposed to be.
Even during the first pages in which the ad spots were still populated, it was noticeable that adsense was messing up if the ad size varied from page to page- it would display narrow ads in wide spaces if no wide spaces had been present on the initial page.
Conclusion
Without ever looking into any code, I'd conclude something within the adsense script (the external javascript) does register the format of ads on the page it is requested by, and obviously prepares a certain amount of suitable ads. Without re-fetching the google js within my ajaxifiy page calls, it would fit in those prepared ads from the initial page load into my ad-slots, until all "prepared" ads have been shown.
Any attempt to re-fetch the google js to trigger fresh ads fails if it is done by ajax.
I assume they have a very simple and rudimentary security lock built in that just states: If anything tries to fetch this script via ajax, proceed without complaining, but dont show any ads either.
Thats googles politics, so lets just respect that for now.
Side-note: Since the refetching doesnt work, I actually prevented the output of
on server side when pages are fetched via ajaxy, see issue #30 )
Solution I - count along
My very first working solution was quite a crude hack:
Since ads would appear for around four pages, I just built in some kind of counter which counts along any page changes done with ajaxify.js loading, and which prevents the ajax load on each fourth page, just as if all links were suddenly given the "no-ajaxy" class - so I had three nice ajaxified page transitions and one standard page load which would say "hoohoo" to google and fetch new ads. Given, that is crude, but it worked, and since the users first impression of a sites behavior is settled after three page loads, I felt the occasional "harsh load" in between was still acceptable - and this solution didnt require any changes to my page other than the added counter functionality in the ajaxify file I customized.
If you do not have
I'd say, use such a counter and be happy to see adsense and ajaxify get along.
However, I had to deal with both of above issues - different ad sizes and the fact that I wanted to rely on staying "ajaxified" all the time, mostly because I enjoy the presence of a chatbox outside of the #content which would disappear and need to be reopened on every fourth page load.
To say it straight away - I have not found any solution that would do without the eventual harsh load to refresh the google hookup, but I have come up with a solution that feels at least a bit less crude than the one mentioned above.
Lets go over things step by step:
Recognizing different layouts needing different ad sizes
A simple scenario: You have some pages with a big square ad, lets call those "layout A", and some pages with a narrow banner, "Layout B".
Using the count-along solution, you will always see the right ads as long as a user stays on pages of the same layout - if a user navigates from one layout to another by ajaxify, you might see portions of a square ad in a narrow spot and vice versa.
What we'll need on top of the count along to prevent that, is some kind of detection/comparison between the current pages layout and the layout of the page to come.
If that detects a layout change, it should again disable the ajax loading and cause a "harsh load" to make sure the diffrent ad layout requests a new set of ads.
How to do that?
on any page, add a line of script within the #content div to pass the current pages layout to javascript:
Next, we need to prepare the comparison between that variable and the layout of the page we are about to load.
I solved this by letting ajaxify check the URI to call for certain identifiers - for a simple example, lets assume all pages in the folders "products/" and "offers/" have layout A, and the page "home.php" has layout B, this looks like:
Of course, you can just ad "no-ajaxy" classes to all links that lead to a different layout page instead, but that may be a lot more work to do, and besides that, we'll be needing the layout detection by JS some more later.
Now, you may believe I am just disabling ajaxify on more and more instances, and depending on your page structure, using it like this may already seem absurd (if you have layout changes all the time, no call would ever be ajaxified). I was not happy about this myself, even tho this solution still guarantees to show google ads all the time and without annoying ad size messups.
Placing ads outside of the content
So, if at this point we are still not entirely happy, lets take the final step into a crazy implementation.
This last one has some benefits which I'll demonstrate, but also some clear downsides which I want to share right ahead:
It all comes down to the idea to move your ads out of the #content div, so they are loaded on the initial page once and just stay right where they are as a user navigates the ajaxified site.
Try it, you will be surprised how awesomly quick your page suddenly feels, because it doesnt take the extra second untill the ads appear :) Lets not forget, our users dont distinguish between
document.readyand "this page looks ready", so by having ads that are right there, they will say your site is much quicker than before.If you are using a fade effect, you might also enjoy the way anything but the ads fades in and out, one might possibly even get better click thru rates because of this.
Positioning out-of-content ads on the content
Now, in my case my best performing ad block happens to be within the #content, so I really felt bad about letting go of that. So again, heres a workaround for this case:
I added another
<div>and let itfloat:leftof my #content div withposition:relative.within this container, I added a second div which holds the google ad.
By assigning
position:absoluteandtop:123px;right:123pxto this ad holding div, it can be placed to the spot inside the #content div where it belongs.edit on oct 29th - If you are worried about positioning ads with
position:absolute- that is actually something google promotes! Here: https://support.google.com/adsense/answer/187769?hl=ensummed up that looks like this:
Of course you may move the 'centerad' div around by jQuery, since adsense bot might not do that I felt more on the safe side with this approach which places the ads at a given position on page load.
Since my page content is dynamic and ad spaces move down depending on the content text length,
I have added a slight placing correction via jQuery on top of that, but to make sure googlebot can tell if an ad is "above fold" or not, I believe the js-independent placement is crucial.
All that being said and looking really promising, I have still found no way to deal with the need for diffrently sized ads on diffrent page layouts than the "listen for layout changes and do a harsh load if necessary" move explained above.
I'll be sure to update you on the effect of loading fewer ads and showing them a longer time as used by this solution. My first impression when surfing around was a very positive user experience.
Testing results (updated on oct 28th)
Well, after about two weeks of testing, I believe its safe to say that I experienced no negative monetary effects from implementing adsense the way described in detail above.
The number of counted ad impressions gets reduced as expected, but since I am making most revenue by CPC, there has been no difference in earnings since the implementation.
have a look at http://www.wikiloops.com to see it in action.