-
Notifications
You must be signed in to change notification settings - Fork 428
Adsense won't load with turbolinks #151
Comments
Can you paste a sample of the adsense code? |
I've setup an rails app that demonstrate this. I will put the link here later, maybe tomorrow. |
@davydotcom , check out this. You may not see any ads because your domain name isn't allowed, but you can see a blank box. Now click to anther page, the box is gone, although the new page has adsense. The expected result is a blank box always be there. |
Same issue here. |
I found this article: http://www.redkart.com/google_adsense.html ...and used the basic premise to come up with this solution, which appears to work on the sample app you provided:
And you should be set. @guangnan @caarlos0 Can you guys try this out and see if it works? |
Hi @reed , I removed the My changes:
|
Cool, thanks for the feedback. I'm glad it worked for you. |
Hi guys, further updates are moved to here. |
Thanks for this solution! However there is one drawback, it adds 3 render-blocking javascript files which can slow the site down. I go from a pagespeed score of 93 to 84 when I add adsense via this method. What about doing something as simple as this? (There would be no need to add the script tag to the head then - which is what causes the slow down.)
Where you have a div with and id of 'ad_one' that contains the adsense code. Any thoughts on this? I'm a JS noob so I might be missing something. |
I think I've developed a good solution for the newest adsbygoogle.js script. MyApp.Turbolinks.GoogleAds = {
reset: function() {
if (typeof adsbygoogle !== 'undefined') {
// It's okay to use forEach since browsers that don't support it
// don't support Turbolinks anyway. If browser doesn't support Turbolinks,
// its events (hence this) won't be triggered.
[
"adk2_experiment",
"async_config",
"correlator",
"exp_persistent",
"iframe_oncopy",
"jobrunner",
"num_0ad_slots",
"num_ad_slots",
"num_reactive_ad_slots",
"num_sdo_slots",
"num_slot_to_show",
"num_slots_by_channel",
"onload_fired",
"persistent_language",
"persistent_state",
"persistent_state_async",
"prev_ad_formats_by_region",
"prev_ad_slotnames_by_region",
"pstate_expt",
"pstate_rc_expt",
"reactive_ads_global_state",
"top_js_status",
"unique_id",
"viewed_host_channels"
].forEach(function(val) {
delete window["google_" + val];
});
delete window.adsbygoogle;
}
},
enable: function() {
$(document).on("page:fetch", this.reset);
}
};
MyApp.Turbolinks.GoogleAds.enable(); |
@leods92 Is the code you provided the whole solution? When I add it to my site I still don't get the ads to show up consistently. I've inserted it in the head after application.js is loaded. |
Well, looks like I was getting an error says undefined MyApp. I changed "MyApp.Turbolinks.GoogleAds" to "AdsenseFix" and it seems to be working...I guess all the '.' was throwing it off. |
@jpstokes It's just a namespace. I like to have a single namespace for the whole app and I also have another one for Turbolinks fixes. To fix your issue you just need to define those namespaces, for instance |
Cool...thx. |
leods92 - thank you for the fix. To keep code structure clean I placed your code to a file required by application.js which is set download asynchronously and it works as well. Is there any reason to put the code in or ? |
@tomasgregor Sorry for the delay in answering. Could you elaborate? I don't understand your question. |
@leods92 I meant you placed your script in the tag and elaborated on placing it in the as another option. In my Rails project I put the code in a separate file turoblinks-settings.js which is required by application.js. My question was whether there's a reason to put the script directly in the body or head tag or it can be uploaded with javascript_inlcude_tag. |
Google adsense won't appear if you get to the page through a turbolink.
I have no idea but perhaps it's due to the dom onload event?
JFYI.
The text was updated successfully, but these errors were encountered: