1. The ad/tracking industry seems to be incapable of writing JavaScript that is not awful. I don't have insight why that happens, but all the scripts I had to deal with were layers upon layers of document.write, iframes, redirects and copy&pasted JS from the Netscape 4 era. Even when somebody tries to fix it, the best they can do is to add one more layer of scripts on top of the rotting pile.
2. Business deals are made with no consideration of 3rd party's code quality and performance impact. Once the deal is signed the developers are just tasked with integrating all of the crap the 3rd party vendor wants. It's impossible to object: the vendor is not going to rewrite all of their shitty scripts, especially after the deal is already in place. Even when it's really bad, arguing about it just results in endless meetings, conference calls and at best the 3rd party vendor saying they'll think about putting some little tweak in the backlog for some future release.