Google Tag Manager

Add the Google Tag Manager Script

Partytown Script

Set the script element's type attribute to text/partytown. For example:

<script
  type="text/partytown"
  src="https://www.googletagmanager.com/gtag/js?id=YOUR-ID-HERE"
></script>
<script type="text/partytown">
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
 
  gtag('config', 'YOUR-ID-HERE');
</script>

Google Analytics 4 (GA4)

GA4 responses come with the correct CORS headers, so you will not have to proxy the requests to www.google-analytics.com. Older versions of Google Analytics required proxying, but have been sunset by Google and are no longer supported.

Note that the automatic user_engagement event GA4 sends when the user leaves the page is not reliable from a web worker. Please see page exit events for more information.

Forward Events

Google Tag Manager uses the dataLayer array to send events. In order for Partytown to forward the calls to window.dataLayer.push({..}), the forward config should add "dataLayer.push". Please see forwarding events and triggers for more information.

Example Config

{
  "forward": ["dataLayer.push"]
}

Please see the integration docs for framework specific configuration.