Synchronous vs asynchronous
Often, libraries are loaded synchronously in the <head> tag of a page. For example:
Tag Management libraries can quickly grow large if you have a lot of tags to manage.
You can load any library asynchronously by adding an async attribute to the <script> tag. For example:
<script src="example.js" async></script>
Considerations to asynchronous deployment
As described above, in synchronous deployments, the browser pauses parsing and rendering the page while the Launch library is loaded and executed. In asynchronous deployments, on the other hand, the browser continues parsing and rendering the page while the library loads. The variability of when the Launch library might finish loading in relation to page parsing and rendering must be taken into consideration.
First, because the Launch library can finish loading before or after the bottom of the page has been parsed and executed, you should no longer call _satellite.pageBottom() from your page code ( _satellite won't be available until after the library has loaded). This is explained in Loading the Launch embed code asynchronously .
Second, the Launch library can finish loading before or after the DOMContentLoaded browser event (DOM Ready) has occurred.
Because of these two points, it's worth demonstrating how the Library Loaded , Page Bottom , DOM Ready , and Window Loaded event types from the Core extension function when loading a Launch library asynchronously.
Let's assume your Launch property contains the following four rules:
- Rule A: uses the Library Loaded event type
- Rule B: uses the Page Bottom event type
- Rule C: uses the DOM Ready event type
- Rule D: uses the Window Loaded event type
Regardless of when the Launch library finishes loading, all the rules are guaranteed to be executed and they will be executed in the following order:
Rule A → Rule B → Rule C → Rule D
Although the order is always enforced, some rules might be executed immediately when the Launch library finishes loading, while others might be executed later. The following occurs when the Launch library finishes loading:
- Rule A is executed immediately.
- If the DOMContentLoaded browser event (DOM Ready) has already occurred, Rule B and Rule C are executed immediately. Otherwise, Rule B and Rule C are executed later when the DOMContentLoaded browser event occurs.
- If the load browser event (Window Loaded) has already occurred, Rule D is executed immediately. Otherwise, Rule D will be executed later when the load browser event occurs. Note that if you've installed the Launch library according to the instructions, the Launch library always finishes loading before the load browser event occurs.
When applying these principles to your own website, consider the following:
- A rule using the Library Loaded event type might execute before your data layer is fully loaded. This can result in the rule's actions executing with missing data because the data was not yet available on the page. These types of issues can be mitigated by tweaking your rule configuration. As an example, instead of having a rule triggered by the Library Loaded event type, you could instead use the Custom Event or Direct Call event type that are triggered by your page code as soon as your data layer finishes loading.
- The Page Bottom event type doesn't particularly provide value when the library is loaded asynchronously. Instead, consider the Library Loaded, DOM Ready, Window Loaded, or other event types.
If you see things occurring out of order, it is likely that you have some timing issues to work through. Deployments requiring precise timing might need to use event listeners and the Custom Event or Direct Call event type to make their implementations more robust and consistent.
Loading the Launch embed code asynchronously
Launch provides a toggle to turn on asynchronous loading when creating an embed code when you configure an environment . You can also configure asynchronous loading yourself:
- Add an async attribute to the <script> tag to load the script asynchronously.For the Launch embed code, that means changing this:
<script src="//www.yoururl.com/launch-EN1a3807879cfd4acdc492427deca6c74e.min.js"></script>to this:
<script src="//www.yoururl.com/launch-EN1a3807879cfd4acdc492427deca6c74e.min.js" async></script>
- Remove any code you may have previously added at the bottom of your tag: