Hey David,
If you really want to run your own platform for advertisement I would recommend you the opensource adserver.
https://www.revive-adserver.com/ . It is possible to build your own tracking software. Yes you maybe able to track from which site the visitor come from, from the PHP $_Server Array. Similarly you can do the samething with the window object in javascript. It is not that hard to know which visitor is viewing your page and from whence they came from.
However the hard part of this is you must write those information and value into an organised data structure to be able to use them later. You also need to let your ad publisher see this log and have an account for each publisher setup on your server for transparency.
Hence best of all is using an adserver. Nevertheless in the scenario where you only want to see which visitor come from where, if you are using a CMS install a plugin that track your visitor ip and compare ii's to the log where you buy your ad from. If you are not using a cms you can obtain a visitor counter that track ip and time. Compare the result to a GEO IP database addon.