Multi domains with one database

I want to have one database with multi domains with next feature:
If I add javascript to domain1 I want in that javascript referring to domain1
and domain2 to domain2

so in any javascript domain have to go to that domain.

I think then the all installs should go on that domain, but if I could have one main database or maybe with api get all stats to one install.

example javascript: