Integrating HubSpot live chat widget into React/Gatsby application

I need help implementing HubSpot’s live chat feature in my React application built with Gatsby. Our team received instructions to place this script tag directly in the HTML body:

<script type="text/javascript" id="hubspot-chat-loader" async defer src="//js.hs-scripts.com/yournumberhere"></script>

The problem is our deployment process doesn’t include the public directory where the main HTML file lives. Since we can’t modify the index.html directly, I’m wondering if there’s a way to load this chat script dynamically through a React component instead. What would be the best approach to integrate this third-party chat widget without touching the static HTML file? I’m fairly new to handling external scripts in React so any guidance would be appreciated.

Just dealt with this on a Gatsby project. Skip modifying the static index.html - create a custom React hook instead. Use useEffect to inject the HubSpot script when your component mounts. Make sure you check if the script’s already there so you don’t load duplicates. I wrapped mine in a component since it’s way more flexible and you can drop it anywhere in your app. Heads up though - if you’re using client-side routing, you’ll probably need to manually trigger HubSpot’s initialization when routes change or it’ll act weird.