Beginner
How to install Google Analytics tracking code on your website
Quick Answer
Install Google Analytics tracking code by copying the GA4 measurement ID from your Analytics property and adding the gtag code to your website's head section. The tracking code will then collect visitor data and send it to your Analytics dashboard.
Prerequisites
- A Google account
- Access to your website's HTML code
- Administrative access to Google Analytics property
1
Access your Google Analytics property
Sign in to your Google Analytics account at
analytics.google.com. Select the property where you want to get the tracking code. If you don't have a property yet, create one by clicking Create Property and following the setup wizard.Tip
Make sure you're using GA4 properties as Universal Analytics has been discontinued.
2
Navigate to Data Streams
In the left sidebar, click Admin (gear icon). Under the Property column, click Data Streams. You'll see a list of your configured data streams for web, iOS, and Android platforms.
3
Select or create a web stream
Click on your existing web stream, or click Add stream > Web if you haven't created one yet. For new streams, enter your website URL and stream name, then click Create stream.
Tip
Use your primary domain URL without any specific pages or parameters.
4
Copy the measurement ID
On the web stream details page, locate your Measurement ID (starts with G-). Copy this ID as you'll need it for the tracking code. The format looks like
G-XXXXXXXXXX.Tip
Keep this ID secure and don't share it publicly as it's unique to your property.
5
Get the tracking code
Scroll down to the Tagging Instructions section and click Global Site Tag (gtag.js). Copy the entire code snippet that appears. It should look like:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>Tip
This code snippet is ready to use and contains your unique measurement ID.
6
Add code to your website
Paste the tracking code into the
<head> section of every page you want to track, preferably right after the opening <head> tag. If using a CMS like WordPress, you can add it to your theme's header.php file or use a plugin.Tip
Place the code as high as possible in the head section for better tracking accuracy.
7
Verify installation
Save your changes and visit your website. Return to Google Analytics and go to Reports > Realtime. You should see your visit appear within a few minutes. You can also use the Google Tag Assistant browser extension to verify the code is firing correctly.
Tip
It may take 24-48 hours for full data to appear in standard reports, but Realtime reports show immediate activity.
Troubleshooting
Tracking code not showing data in reports
Check that the code is placed in the
<head> section, not the body. Verify the measurement ID matches exactly, and ensure there are no JavaScript errors on your page blocking the code execution.Multiple tracking codes detected
Remove any old Universal Analytics codes (starting with UA-) and ensure you only have one GA4 tracking code per page. Multiple codes can cause data duplication or conflicts.
Code appears as text on the website
Make sure you're pasting the code in the HTML editor, not a visual/WYSIWYG editor. The code should be added to template files, not page content areas.
Real-time reports not showing visitors
Clear your browser cache, disable ad blockers that might block Analytics, and ensure you're not filtering out internal traffic. Try visiting from a different device or network to test.
Ready to get started with Google Analytics?
Put this tutorial into practice. Visit Google Analytics and follow the steps above.
Visit Google Analytics →