uswds / uswds-site

USWDS website and documentation
https://designsystem.digital.gov
Other
184 stars 140 forks source link

USWDS-Site - Feature: Add GA4 analytics snippet #2390

Closed thisisdano closed 4 months ago

thisisdano commented 7 months ago

Is your feature request related to a problem? Please describe.

We'll need to add a new code snippet to our websites so we can transition to GA4 data collection under the GSA Enterprise Analytics account while continuing to collect UA data on our existing account.

Describe the solution you'd like

The existing DAP code in _includes/analytics.html should continue to appear within the document head, but this new GA4 snippet AND our existing UA snippet should now appear directly after the head.

DAP implementation guide (PDF) → GA4 Implementation guide (see "Set up data collection for websites") →

In the head:

<--! DAP -->
<script async type="text/javascript" id="_fed_an_ua_tag" src="https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=GSA&subagency=TTS"></script>

Directly after head:

<--! GA4 -->
<!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-HBYXWFP794"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-HBYXWFP794'); </script>

<--! UA -->
<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', '{{ site.google_analytics_ua }}', 'gsa.gov');
    ga('send', 'pageview');
    // anonymize user IPs (chops off the last IP triplet)
    ga('set', 'anonymizeIp', true);

    // forces SSL even if the page were somehow loaded over http://
    ga('set', 'forceSSL', true);

    ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>

Describe alternatives you've considered

No response

Additional context

No response

Code of Conduct

mahoneycm commented 6 months ago

After reviewing the documentation, some Q&A online, and testing, I believe the instructions mean after the opening head tag rather than after the head completely / closing tag. More information available in #2428