For those of us who run content websites and deal with Ad networks, combating malicious or malfunctioning rogue ads can be frustrating.
I own a site that embeds a lot of Youtube & Dailymotion videos. Once in awhile, a bad Ad will turn up and make the video playback stutter. I always dealt with these on a case-by-case basis. But, is there a way to detect (using javascript) whether or not the page is slow?
In my head, a very crude way is to have a setInterval running at 100ms. And if it detects a big delay in one interval, act accordingly.
Are there other more elegant approaches?
If a third-party script is slowing down your page load, you have several options to improve performance: Load the script using the async or defer attribute to avoid blocking document parsing. Consider self-hosting the script if the third-party server is slow.
The first step in reducing the impact of third-party code is auditing the scripts on your site to see which are slow. To estimate the potential improvement removing a script will have, Chrome allows specific scripts to be blocked for Lighthouse audits. Go to Network > JS > right click on a script > Block request URL.
First approach, if your slowness is on load, create placeholders for the ads and load them very last after everything else.
Second approach, create a Javascript timer or include a timer library to measure the page load time. If it is greater than your acceptable threshold then kill the ad with Javascript or log the slowness to a web service.
Third approach, if the timer does not pick up the slowness because it is incremental then use a setTimeout function that records a timestamp and calls itself every 200ms and compares the new timestamp each call to the older timestamp from the previous call.
If a setTimeout call set to run at 200ms takes 500ms to finally run then you've got substantial delay and should kill the ad with Javascript or log the slowness to a web service.
Each of these methods will need to be tuned to your actual site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With