a web server records the ip address of the
requestor (that would be you) when the
request is received. it's part of the http(s)
protocol. advertisers (whose code is
embedded in web pages - via contracts)
take it from there. how badly or well they
perform is an ongoing matter for debate.
your experience is not uncommon.
in effect, it has nothing to do with your
location, only your reported ip address.
in order for a web server to know your location,
you have to give it permission to know.
turning on location is not enough; you have
to respond affirmatively to a prompt from the
browser. in that case, the browser reports
your location using several built-in methods.
there is a spec, and different browsers have
different ways of implementing it.
if you use anonymizing browsers, your ip
address can reported as being in kazakhstan
(very nice!) regardless of the ip address
assigned to you by your isp.
discrete apps more or less have to conform
to similar rules, but they often try to get you to
agree to more instrusive tracking.