我想创建一个仅在访问者同意时才触发 GA4 测量的函数。我创建它如下:
document.querySelector('.cookie').addEventListener('click', function(){
        var ga = document.createElement('script')
        ga.type = 'text/javascript'
        ga.async = true
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.googletagmanager.com/gtag/js?id=UA-XXXXXX-X'
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        let gat = window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());
                gtag('config', 'UA-XXXXXX-X');
                setCookie('cookie','1',365);
        return false;
    })
Run Code Online (Sandbox Code Playgroud)
问题是打字稿报告错误:Property 'dataLayer' does not exist on type 'Window & typeof globalThisa gtag - Expected 0 arguments, but got 2.
我已经尝试了各种教程,已安装@types/gtag.js,但错误仍然存在。你能告诉我我做错了什么吗?谢谢