Error: “A tag read consent state before a default was set” for Google consent mode v2

I’m configuring Google consent mode v2 for PHP based project. And after I open the web page in browser, in Google tag assistant I receive the data I sent (such as ‘ad_user_data’) but receive and error “A tag read consent state before a default was set”
Google Tag Assistant result

This is the beginning of the html code of my project which contains the code for Google consent mode v2.

<head>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments)};
        gtag('consent', 'default', {
            'ad_user_data': 'denied',
            'ad_personalization': 'denied',
            'ad_storage': 'denied',
            'analytics_storage': 'denied',
        })

I want to find out where this error is coming from and remove it.