Hi,
if you set properly the Adobe Analytics tool, then in my personal experience every conditioning "trigger" available (top of page, bottom, dom-ready) except from "window-load", runs before the AA tracking request.
So you should write your javascript code in the dedicated custom code section on each of these two rules. Check load order here to decide better the trigger and synchronous/asynchronous loading: https://marketing.adobe.com/resources/help/en_US/dtm/load_order.html
You can create 1 rule with a "top of page" trigger where you write the first script code, then a second one with trigger "page bottom" which can use the first resource. Both of these rules should be executed before the AA tracking.
Hope to be clear enough
Gabriele