collapseEmptyDivs() is for ads (including AdSense ads) delivered through Google Ad Manager (previously called Doubleclick for Pusblishers or DFP in short), it will not work for direct Google AdSense ads neither manual nor auto-ads.
It does work just fine for DFP ads, though I am not sure if it is a good idea to use it is it most likely would increase CLS
I surrounded the ad space in a div, but it just leaves a large white space when nothing shows up. I have service workers so that the user can use the website while offline. I would like the ad space to collapse so that there is more room for content. This is what I have pasted into the my website.
AdSense is already doing that - though I'm not sure if it's rolled out to all accounts yet.
Simplifying "If no ads available"
We're continuously working to increase the quality and safety of our ad network and reduce the number of malicious ads that enter the AdSense network. As a result, we're removing the "If no ads available" option and we'll be defaulting to collapsing the ad space or showing a blank space instead. If your current setting is a color or another URL, it will be updated in the near future.
After reading a recent post about an individual getting warnings from their advertising provider (Adsense) about their site displaying ads on inaccessible pages (inaccessible to the advertiser). Then said advertiser no longer serving ads to the entire site (hopefully not a permanent ban)...
Replace the list at AdminCP >> Setup >> Advertising
Scroll down to "Prevent ads showing in these templates" and replace the existing shorter list with the lost in the first link below.
I just applied this to two Xenforo 2.2.1 forums now and so far it has removed the large blank space on those pages.
This might be helpful for also avoiding AdSense violation warnings:
Hi, like said in the title, I'm getting Google Adsense Policy Violations for mydomain.com/login page (Valuable Inventory: No content ; Restricted ad serving). I don't have ads on my login page (I don't see any), I prevent ads showing in login template in XF options and extra with a conditional...