How does call tracking with Dynamic Number Insertion work, from A to Z?

I’m asking about all the flow, from the click, to the data feedback to Google Ads (for example).

How softwares like CallRail, CallTrackingMetrics and Nimbata work? Can you explain how it works at each step?

I have some knowledge of programming and web development. But now I’m a bit lost.

I’d like to reproduce the same thing as these services (like CallRail). Let’s say I have 200 phone numbers at my disposal.

I understand that a user clicks on a Google Ads ad, for example.

The visitor arrives on the website.

What happens then?

I think there’s a Javascript code that chooses which of the 200 phone numbers to display. Right?

What happens then? A code is going to write to the database the important parameters such as the gclid, the search term, the ip address, and the phone number that was displayed?

Then it’s time to extract the call informations? Like call duration etc? And then it’s possible to mix this data with the previous ones and confirm the conversion of the visitor, right?

But how do you feed the information back to Google Ads (or Google Analytics)? Does the code in the website have to send this information to Google Ads via the API?