And are you going to display this consent-dialog on each page-fetch? Or maybe you are going to remember user choice using… cookies?
In my opinion forcing users to see and close those annoying boxes is far too much in case of most simple sites using Google Analytics. A footnote with info and link to GA opt-out is enough, according to GDPR (reasonable bussiness effort). But it’s just my opinion and a subject for another topic.
On websites that use Google Analytics only (and no ads, no Adwords, etc.) I have put this: