Prevent  polution if ad code is getting rendered inside a loop:

Most of conditionals are getting checked against global , getting matched ad code inside the loop might result in wrong ad code matched. Calling wp_reset_postdata() before trying to match the ad code.