Hi Anastasiia. Thank you for taking the time to point out the problems.
I have already written an article on how to add google analytics with NextJS application.
I will be grateful if you give it a quick glance.
I will update this one according to your advice as well.
Thank you :)