Adding Google Analytics to your Jekyll site is a powerful way to gain insights into your visitors and understand how they interact with your content. Although Jekyll is a static site generator, integrating Google Analytics is straightforward and can be done with a few simple steps.
Step 1: Create a Google Analytics Account
First, visit the Google Analytics website and sign in with your Google account. Click on "Start for free" to set up a new account. Follow the prompts to create an account, property, and view for your website.
Step 2: Get Your Tracking ID
Once your property is set up, navigate to the Admin panel. Under the property column, click on "Tracking Info" and then "Tracking Code." You will see a Tracking ID that looks like UA-XXXXXXXXX-X. Copy this ID, as you'll need it for the next step.
Step 3: Add the Tracking Code to Your Jekyll Site
Open your Jekyll site's source code. Locate the _layouts folder and open the default layout file, usually named default.html or similar. Insert the Google Analytics tracking script just before the closing </head> tag:
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'YOUR_TRACKING_ID');</script>
Replace YOUR_TRACKING_ID with the ID you copied earlier.
Step 4: Deploy Your Site and Verify
After saving your changes, deploy your Jekyll site. Visit your website in a browser and go back to Google Analytics. Use the "Real-Time" report to verify that your visits are being tracked. If you see your activity, the setup was successful!
Additional Tips
- Use environment variables to manage your Tracking ID securely.
- Customize your Google Analytics dashboard to track specific goals and events.
- Ensure your site uses HTTPS to comply with Google’s tracking policies.
By following these steps, you'll be able to monitor your Jekyll site's traffic effectively. Google Analytics provides valuable data to help improve your content and grow your audience.