How to Set Up Google Analytics: Step-by-Step Guide
To set up
Google Analytics, create a Google Analytics account, add your website as a property, and then install the provided tracking code on your website's pages. This code collects data about visitors and sends it to your Analytics dashboard for analysis.Syntax
Setting up Google Analytics involves adding a tracking code snippet to your website's HTML. This snippet looks like a JavaScript code block that collects visitor data.
The main parts are:
- Tracking ID: A unique identifier for your website property, e.g.,
G-XXXXXXXXXX. - Global Site Tag (gtag.js): The JavaScript code that sends data to Google Analytics.
html
<!-- Global site tag (gtag.js) - Google Analytics --> <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>
Example
This example shows how to add Google Analytics tracking to a simple HTML page. Replace G-XXXXXXXXXX with your actual tracking ID from your Google Analytics account.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sample Page with Google Analytics</title> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-1234567890"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-1234567890'); </script> </head> <body> <h1>Welcome to My Website</h1> <p>This page is tracked by Google Analytics.</p> </body> </html>
Output
When this page loads, Google Analytics starts collecting visitor data and sends it to your Analytics dashboard under property ID G-1234567890.
Common Pitfalls
Some common mistakes when setting up Google Analytics include:
- Not replacing the placeholder tracking ID
G-XXXXXXXXXXwith your actual ID. - Placing the tracking code incorrectly, such as outside the
<head>or before the<body>tag. - Using multiple tracking codes on the same page, which can cause data conflicts.
- Not verifying the setup by checking real-time reports in Google Analytics.
Always test your setup after installation.
html
<!-- Wrong: Missing actual tracking ID --> <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> <!-- Right: Use your real tracking ID --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-1234567890"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-1234567890'); </script>
Quick Reference
Follow these quick steps to set up Google Analytics:
- Create a Google Analytics account at analytics.google.com.
- Add your website as a new property.
- Copy the Global Site Tag (gtag.js) tracking code provided.
- Paste the tracking code into every page of your website, ideally inside the
<head>tag. - Verify data collection by checking the real-time reports in your Analytics dashboard.
Key Takeaways
Create a Google Analytics account and add your website as a property to get a unique tracking ID.
Install the Global Site Tag (gtag.js) with your tracking ID inside the section of your website pages.
Always replace placeholder IDs with your actual tracking ID to ensure data is collected correctly.
Verify your setup by viewing real-time data in the Google Analytics dashboard after installation.
Avoid adding multiple tracking codes on the same page to prevent data conflicts.