Read all about this approach on Lessons of a CTO. The heavily-commented code powering this is available open-source on GitHub.
For the purpose of the demo, I've added US/Canada/Australia/New Zealand to the list EU+UK countries requiring consent. If you're outside of these, you'll need to use a VPN or test with something like WebPageTest.
This is the regular homepage where JavaScript will show the dialog if necessary.