How to install tracking code on HubSpot

beginner 8 min read Updated 2026-03-18
Quick Answer

Installing HubSpot tracking code involves copying the tracking snippet from your HubSpot account settings and adding it to your website's HTML before the closing tag. The code automatically tracks visitor behavior and enables HubSpot's analytics and marketing features.

Prerequisites

  • Active HubSpot account
  • Website administrator access
  • Basic understanding of HTML/JavaScript

Step-by-Step Instructions

1

Access your HubSpot tracking code

Log into your HubSpot account and navigate to Settings (gear icon) in the main navigation. Click on Tracking & Analytics in the left sidebar, then select Tracking Code.
Bookmark this page for easy access when managing multiple websites.
2

Copy the tracking code snippet

In the tracking code section, you'll see a JavaScript code snippet in a gray box. Click the Copy button to copy the entire tracking code to your clipboard. The code will look similar to:
<!-- Start of HubSpot Embed Code -->
<script type="text/javascript" id="hs-script-loader" async defer src="//js.hs-scripts.com/XXXXXXX.js"></script>
<!-- End of HubSpot Embed Code -->
Each HubSpot portal has a unique Hub ID number in the tracking code - never share this with unauthorized users.
3

Access your website's HTML code

Open your website's source code using your content management system, code editor, or website builder. If using WordPress, go to Appearance > Theme Editor or use a plugin. For other platforms, access the HTML template files or header section.
Always create a backup of your website files before making any code changes.
4

Locate the closing </head> tag

Find the </head> tag in your website's HTML code. This is typically located in the header template file (header.php for WordPress) or the main HTML template. The tracking code must be placed immediately before this closing tag.
Use Ctrl+F (or Cmd+F on Mac) to quickly search for '</head>' in your code editor.
5

Paste the tracking code

Paste the HubSpot tracking code you copied earlier directly above the </head> tag. Ensure there's a line break between the tracking code and the closing head tag for clean formatting:
<!-- Start of HubSpot Embed Code -->
<script type="text/javascript" id="hs-script-loader" async defer src="//js.hs-scripts.com/XXXXXXX.js"></script>
<!-- End of HubSpot Embed Code -->
</head>
6

Save and publish changes

Save your HTML file changes and publish or upload them to your live website. If using a CMS like WordPress, click Update File or Save Changes. For other platforms, follow their specific publishing process to make the changes live.
Clear your website cache after publishing to ensure the tracking code loads properly.
7

Verify tracking code installation

Return to HubSpot and go back to Settings > Tracking & Analytics > Tracking Code. The page should show a green checkmark with "Installed and working" status within 10-15 minutes. You can also check by viewing your website's source code to confirm the script is present.
Use browser developer tools (F12) to check the Network tab and verify the HubSpot script is loading without errors.

Common Issues & Troubleshooting

Tracking code shows as 'Not Installed' after 24 hours

Verify the code is placed before the </head> tag, not in the <body>. Check that your website cache has been cleared and that no security plugins are blocking the script. Also ensure you haven't modified the tracking code snippet.

Website loading slowly after installing tracking code

The HubSpot tracking code is designed to load asynchronously and shouldn't affect page speed. Check for conflicting scripts or plugins. Consider placing the code just before </body> instead of </head> if speed is critical, though this may delay data collection.

Tracking code not working on all pages

Ensure you've added the code to your main template file that's used across all pages, not just individual page files. For WordPress, add it to header.php of your active theme. For other CMS platforms, add it to the global header template.

Console errors related to HubSpot script

Check that the Hub ID in your tracking code matches your HubSpot account. Verify there are no syntax errors in the code placement and that the script URL is accessible. Try regenerating the tracking code from HubSpot settings if the issue persists.

Prices mentioned in this guide are pulled from current plan data and may change. Always verify on the official HubSpot website before purchasing.