View Single Post
Old 02-02-2019, 02:48 PM   #73
psyang
Powerplay Quarterback
 
Join Date: Jan 2010
Exp:
Default

Ok, had some time to do more debugging this afternoon.

The script to generate the ads makes a call to the ad server to get the ad details. Unfortunately, this call sometimes returns "good" ads (ads that are positioned properly) and "bad" ads (ads that block the forum).

Using Fiddler, I can execute the exact same request to the ad server, and I will get both "good" and "bad" ads. In the below examples, there are two ads returned. The first is for the top banner, the second is for the side.

This is a bad ad:
Spoiler!

Notice the side banner ad has the _breakglass_mode_ = 2, which is what triggers the incorrect margin-left value. Also notice, it is tagged to "use_safe_frame", which is probably related.

A good ad doesn't set the _breakglass_mode_ value or tag the ad to use a safe frame. Instead, it sets "inabox_meta":
Spoiler!


Since the call is exactly the same, there's nothing CP can do (such as change the call's parameters) to ensure it gets good ads. It is entirely up to the ad server.

Because the margin-left style that gets set is inline, and tagged as important, there's no way to provide css to override it.

So, the only alternative is to add some javascript to the page that runs after the ads load, and changes the style directly. The tricky part is ensuring the javascript runs after the ads are displayed. The below script will poll every second to see if the ad is up. If so, it will reset the left-margin, then stop polling.

I'll leave it to CP if they want to use the script or not. Use at your own risk.
Also, if there is a new change to the ads, this script may not work. I think the script is harmless - at worst, it will poll every second and do nothing. However, it might be possible that this script violates the google ads TOS as they might not like post-manipulation of a served ad.

This is a formatted version with comments.
Code:
<script type="text/javascript">
var pollTime = 1000; // 1000ms, or 1 second.
var timerId = setInterval( 
  // this function will run every second.
  function(){

    // the id of the div tag that contains the ad. This doesn't seem to change.
    var adId = "div-gpt-ad-1461011238474-1";

    // get the ad's div tag.
    var element = document.getElementById(adId);

    // if we were successful, the ad is up. Remove the margin-left style.
    if ( element ) {
      element.style["margin-left"]="0"; // set margin-left to 0
      clearInterval(timerId);  // stop polling.
    } // if
  }, pollTime );
</script>
This is a one line compressed version.
Code:
<script type="text/javascript">var _itv=setInterval(function(){var e=document.getElementById("div-gpt-ad-1461011238474-1");if(e){e.style["margin-left"]="0";clearInterval(_itv);}},1000);</script>
psyang is offline   Reply With Quote
The Following 2 Users Say Thank You to psyang For This Useful Post: