If you're hosting a website on GitHub Pages and want to track how many people visit your site and what they do there, Google Analytics is the perfect tool. It provides valuable insights that can help you understand your audience and improve your site.
In this easy-to-follow guide, we'll show you how to add Google Analytics to your GitHub Pages site, even if you're new to web analytics!
What is Google Analytics?
Google Analytics is a free tool from Google that lets you track website traffic. By adding Google Analytics to your website, you can monitor how many visitors you have, what pages they visit, how long they stay, and much more. This data helps you make better decisions to improve your site.
Why Add Google Analytics to GitHub Pages?
GitHub Pages is a great way to host static websites, but it doesn't come with built-in tools for tracking visitor data. Google Analytics allows you to see how many people visit your site, which pages they check out, and how they interact with your content. This data is crucial for improving your site and increasing user engagement.
Steps to Add Google Analytics To GitHub Pages
Here’s how you can quickly set up Google Analytics on your GitHub Pages website:
Step 1: Create a Google Analytics Account
Go to the Google Analytics Website

Step 2: Fill in your account details
Fill in your account details, such as your account name and the website you want to track.

Step 3: Check the option boxes

Step 4: Fill this section as your Name or time zone
Choose your time zone and other settings, and click "Next".

Step 5: Accept the terms and condition

Step 6: Select your company size

Step 7: Select the box which you want to choose

Step 8: Accept the Terms and Condition

Step 9: Click Web options
Now you see the following window click Web options.

Step 10: Click the Create and Continue Button

Step 11: Now you successfully connect your GitHub to Google Analytics

Now you see my Github Analytics graph.

Pseudocode
<!-- 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>
<!-- End Google Analytics -->

<!-- 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>
Replace 'G-XXXXXXXXXX' with your actual Measurement ID
Step 12: Here is your measurement id just copy and replace in code

Step 13: Open Your GitHub account make a repository

Step 14: Click To Commit Changes

Here you see your Github traffic graph

Conclusion
You’ve successfully added Google Analytics to your GitHub Pages site. Now, you can track your visitors, monitor how they interact with your content, and make data-driven decisions to improve your site.
With Google Analytics, you’ll have access to valuable insights like:
- The number of visitors to your site
- What pages they visit
- How long they stay on your site