You could do this using plain Javascript.
let el = document.createElement('script');
if(condition) {
el.src = 'someUrl';
}
else {
el.src = 'otherUrl';
}
document.head.append(el);
However, it's against the Angular style guide to interact directly with the DOM. If you do this in Angular, the 'Angular way' of creating this would be by injecting and using the Renderer2
service:
constructor(private _renderer: Renderer2) {}
public someMethod() {
let el = this._renderer.createElement('script');
el.src = 'conditionalUrl';
this._renderer.appendChild(el, this_renderer.selectRootElement('body'));
}
As far as when to do this in Angular, you could hijack the APP_INITIALIZER
service and provide your own initialization code. Check out this answer
for more information.