Tengo un sitio web con analytics.js y una política de seguridad de contenido (CSP). Me gustaría agregar una audiencia de remarketing de anuncios de Google.
La documentación es bastante confusa porque parece que gtag.js que se usa con Google Analytics es similar pero diferente a Google Tag Manager (GTM).
Este es mi fragmento de análisis actual:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXX-1', 'auto'); ga('set', 'dimension1', 'value'); ga('set', 'userId', 'X'); ga('send', 'pageview');Google tiene instrucciones para usar GTM con un CSP aquí . Aunque la URL de GTM es diferente de la URL proporcionada por Analytics para usar gtag.js. suspiro
De todos modos, después de revisar un montón de documentos, llegué a esto:
(function(w,d,s,l,i){ w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','UA-XXX-1'); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-XXX-1', {'user_id': uuid, 'custom_map': {'dimension1': 'user_status'}}); gtag('event', 'user_status_event', {'user_status': user_status});pero los datos de audiencia de Google Analytics aún están en blanco.
¿Alguna idea sobre cómo hacer que esto funcione?
He cambiado a esto porque es menos complicado:
<script nonce='{{ nonce }}' async src="https://www.googletagmanager.com/gtag/js?id=UA-XXX-1"></script> <script nonce='{{ nonce }}'> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-XXX-1', {'custom_map': {'dimension1': 'user_status'}}); gtag('event', 'user_status_event', {'user_status': user_status}); </script>Actualizaré si funciona.