Skip to content

Conversation

@ethangardner
Copy link

This is a proposed change to fire a custom event when the universal federated analytics script is loaded. The reason for this is because DAP advises its users to load the script as async, and there are scenarios where a consumer of the DAP script also would like to call gas4 to send other event details.

Since the gas4 function is defined by the Universal-Federated-Analytics script, there is potential to have JS errors if the call to the gas4 function is made before the Universal-Federated-Analytics script loads. Since the Universal-Federated-Analytics is defined as async, there is currently no way to make sure the gas4 function can be called reliably due to how the browser loads script files with the async attribute.

Adding the custom event as proposed would allow consumers of the script to call the gas4 function after it has been defined.

if ('gas4' in window) {
  // gas4 has already been defined and is available to call right away
 // -> call gas4()
} else {
  window.addEventListener('dap-universal-federated-analytics-load', () => {
    // the Universal-Federated-Analytics script is still loading and gas4 hasn't been defined yet. We can listen to the custom event and call when gas4 is available.
   // -> call gas4()
  });
}

@sanason sanason requested a review from a team December 11, 2024 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant