Skip to content
Steven Roland

How to Use Partytown.js to Load Common Scripts Like Google Tag Manager or Google Analytics

Integrating third-party scripts such as Google Tag Manager (GTM) and Google Analytics can significantly impact your website's performance. Partytown.js offers a solution by offloading these scripts to a web worker, freeing up the main thread for more critical tasks. Below is a guide on how to use Partytown.js to load these common scripts effectively.

What is Partytown.js?

Partytown.js is a library designed to relocate resource-intensive scripts from the main thread to a web worker. This approach helps improve website performance by minimizing the impact of third-party scripts on the main thread. Partytown.js is particularly useful for scripts that do not need to be part of the critical rendering path, such as analytics and tracking scripts.

Setting Up Partytown.js

1. Installation:

  • You can install Partytown.js using npm or yarn:

    npm install @builder.io/partytown

    or

    yarn add @builder.io/partytown
  • Copy the Partytown library files to your public directory:

    npx partytown copylib public/~partytown

2. Include Partytown Script:

  • Add the Partytown script to your HTML file to initialize the library:

    <script src="~partytown/partytown.js"></script>

Loading Google Tag Manager with Partytown.js

1. Modify Script Tags:

  • Change the type attribute of your GTM script tags to text/partytown:

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

2. Forward Events:

  • Configure Partytown to forward specific function calls to the web worker. For GTM, you need to forward dataLayer.push:

    <script>
        window.partytown = { forward: ['dataLayer.push'] };
    </script>

Loading Google Analytics with Partytown.js

1. Script Configuration:

  • Similar to GTM, set the type attribute for Google Analytics scripts:

    <script type="text/partytown" src="https://www.google-analytics.com/analytics.js"></script>
    <script type="text/partytown">
        window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};
        ga('create', 'YOUR-GA-ID', 'auto');
        ga('send', 'pageview');
    </script>

2. Forwarding Functions:

  • Ensure that any functions used by Google Analytics are forwarded:

    <script>
        window.partytown = { forward: ['ga'] };
    </script>

Considerations and Caveats

  • Compatibility: Not all third-party scripts are compatible with Partytown. It's important to test each script individually to ensure it functions correctly when offloaded to a web worker.

  • Incremental Adoption: Partytown allows incremental adoption. You can start by offloading a few scripts and gradually move more as you confirm compatibility and performance improvements.

  • Debugging: Enable debugging in development to monitor how scripts are being processed by Partytown:

    window.partytown = { debug: true };

By using Partytown.js, you can significantly improve your website's performance by offloading heavy third-party scripts like Google Tag Manager and Google Analytics to a web worker, thus freeing up the main thread for more critical tasks.

Support My Work

If you enjoy my content, consider supporting me through Buy Me a Coffee or GitHub Sponsors.

Buy Me A Coffee
or

More posts

Simplifying Laravel Development with Laravel Sail

Laravel Sail is a lightweight CLI for managing Laravel's Docker development environment. It simplifies running Artisan commands, PHP scripts, tests, and database operations. Key uses include local development, CI/CD pipelines, team collaboration, and multi-version PHP testing. Best practices involve using aliases, customizing services, and regular updates.

The Myth of Geographical Cures: Wisdom from Neil Gaiman

Neil Gaiman's quote from "The Graveyard Book" challenges the idea that changing locations can solve our problems. It emphasizes that happiness is internal and that personal growth, not geographical change, is key to contentment.

Simplifying Social Authentication with Laravel Socialite

Laravel Socialite simplifies OAuth authentication with social media platforms in Laravel apps. It offers easy setup and integration with providers like GitHub, Google, and Facebook. Key features include user detail retrieval and stateless authentication. Suggested uses include one-click registration, multi-provider auth, and social sharing. Best practices involve proper error handling, security measures, and token management.