Set Google Analytics in Jekyll Site
This article is is about adding a google tag for Google Analytics to a Jekyll website.
I’m using the Minima theme for the Jekyll website.
Reference
Environment
- Jekyll 4.3.3
- Minima 2.5.1
Workflow to set Google Analytics and Verification
1. Add the JavaScript for Google tag
Create or update a google-analytics.html
file in the _includes
directory with the following content.
There may be the default file but it’s not compatible with Google Analytics 4. So you need to change it.
I used this website as reference.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ site.google_analytics }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ site.google_analytics }}');
</script>
2. Update the Config
Add the following line to the _config.yml
file, replacing the placeholder with your google tag.
You can obtain the google tag from Google Analytics.
The code for Google Analytics starts from G-
.
google_analytics: YOUR_GOOGLE_TAG
You need to specify a production environment (JEKYLL_ENV=production
) in the Jekyll build command to make the code work.