NodeBB and ads - A never ending story

General Discussion
  • Do you have ajaxify disabled? There's no way to do that currently with ajaxify enabled, due to the ad script calling document.write()

  • What exactly is not possible @yariplus? Realoding ads? If you mean that, I have to dissapoint you. They work just fine 😄

    Here a little how to for DFP users:
    Open up public/src/ajaxify.js and go to line 161 ( function renderTemplate(url, tpl_url, data, callback) { ).
    Below app.refreshTitle(data.title); add googletag.pubads().refresh(); .
    This will work just fine and complies with Google ToS.

  • I apologise I am not very familiar with DFP.

    The problem you describe here: synchronous rendering results in the DOM structure to break, causing the forum to disappear and just the ads to show up. It is basically like the page would only load the ads, but has no content. is the result of a script calling document.write() after a page has loaded.

    Since ajaxify asynchronously loads the widget data, I assume something in the widget script must be calling document.write().

  • @yariplus with tags without sync rendering it works just fine though.

  • Don't know if the problem is the same, but a few years ago I did a custom ad network (because I didn't like DFP) to serve custom banners and Adsense ads. And I had a problem with document.write() too.

    The problem was solved overriding the document.write function. Kinda tricky, but worked: before loading the adsense ad, the document.write function is replaced with a custom function that inserts the content into an element, instead of writing it to the dom directly. After inserting the content, the original document.write function is restored.
    That is, a one time function replacement.

    Here is some code:

    var originalFunction = document.write;
    
    overrideDocumentWriteFunction = function(container) {
    	document.write = function(content) {
    		container.innerHTML = content;
    		document.write = originalFunction;
    	};
    };
    

    And I used it like this:

    var div = document.getElementById('adPlacement');
    overrideDocumentWriteFunction(div);
    
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.innerHTML = '{adsense script content}'
    document.body.appendChild(script);
    

    This way the document.write function was customized to ensure the Adsense script worked after the page was loaded.

    I haven't tried DFP on NodeBB but maybe this helps.

  • Ha, that's tricky 😄 Does it run afoul of their TOS?

  • @julian I doubt it, since it should only be called once.

    This is a neat trick, I don't think it would work as-is with DFP though, how would you determine what content goes into what container? Page order maybe?

  • Thought it was working on the latest master. But nope...

  • @AOKP so is your only problem with sync rendering?

  • @PitaJ yep. I can get ads to reload fine, the ads work as well. But without synchronous ad rendering the UX is somewhat fucked up.


Suggested Topics