There is a new module for Drupal 8 and 9: COOKiES. It supports the modules Google Analytics and Google Tag Manager (and more). Maybe that's what you are looking for.
COOKiES
User consent management module for Drupal 8 and 9.
The module enables existing third-party-integration modules continued to be used in compliance with the GDPR (of course without manipulating their code). It solves the basic problem that when using third-party-integration modules, the user must agree to the use of cookies (according to GDPR) before they are installed.
IMPORTANT: No liability is assumed regarding compliance with the GDPR.
The COOKiES module provides (with the Library Cookies JSR) a fully configurable user interface for user decisions. It also supports out-of-the-box some key third-party integration modules (especially those included in the Thunder distribution):
- Google Analytics module
- Google Tag Manager
- Video embed, core:media module
- Twitter media module
- Instagram media module