Google Adsens
Published:
You can add both Google AdSense (to display advertisements and earn revenue) and Google Analytics 4 (GA4, to track website visitors and traffic) to your website hosted on GitHub Pages using the AcademicPages template.
The good news is that AcademicPages is built with Jekyll, so you generally don’t need to modify the template’s core code. You can add Google’s tracking and advertising scripts through the Jekyll layout files.
I’ll explain how to do both.
1. Add Google Analytics 4 (GA4)
GA4 / Google Analytics 4 Migration & Transition by Tinderpoint*
Google Analytics
Tracks visitors, page views, traffic sources, geographic statistics, and user engagement.
Free service
Step 1: Create a Google Analytics account
- Go to Google Analytics.
- Sign in with your Google account.
- Create an account for your website.
- Create a GA4 property.
- Add a Web data stream.
- Enter your website URL, for example:
https://yourusername.github.io - Google will provide a Measurement ID similar to:
G-XXXXXXXXXX
Keep this ID. You will need it in your website configuration.
Step 2: Find the correct AcademicPages file
In your GitHub repository, locate:
_config.yml
AcademicPages normally supports Google Analytics configuration through this file.
Look for a section similar to:
yaml
analytics:
provider: false
Change it to:
yaml
analytics:
provider: "google-gtag"
google:
tracking_id: "G-XXXXXXXXXX"
anonymize_ip: true
Replace G-XXXXXXXXXX with your actual Measurement ID.
Save and commit the changes.
Important: If your version of AcademicPages has a different analytics configuration, check its _includes/analytics/ directory and existing configuration before adding another tracking script. You don’t want Analytics installed twice.
Step 3: Verify that Analytics works
- Wait a few minutes after deploying your website.
- Open Google Analytics.
- Navigate to Reports → Realtime.
- Open your website in another browser tab.
You should see your visit in the Realtime report.
2. Add Google AdSense (to earn money from advertisements)
| Monetize Your Content with Google AdSense | Earn Revenue from Ads |
Google AdSense
Displays advertisements on your website and can generate revenue from eligible ad impressions and clicks.
Requires Google approval
Step 1: Register your website
- Visit Google AdSense.
- Sign in using your Google account.
- Add your website URL.
- Complete the registration process.
- Google will provide an AdSense verification code.
Your website will need to pass Google’s review before you can display ads. Review can take a few days or, in some cases, 2–4 weeks.
Google AdSense Help
+1
Step 2: Add the AdSense code to AcademicPages
In your GitHub repository, look for:
_includes/head/custom.html
If this file doesn’t exist, create it.
Paste the AdSense code provided by Google into this file. It will look similar to:
HTMLWrap linesCopy codePreview
<script async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
crossorigin="anonymous">
</script>
Replace the publisher ID with the one provided by Google.
AcademicPages includes custom head functionality, so this is a convenient place to add the code without changing the main theme files.
Step 3: Enable automatic advertisements
Once Google approves your website:
- Open your AdSense account.
- Go to Ads → By site.
- Select your website.
- Enable Auto ads.
- Save your settings.
Google can then determine suitable places to display advertisements.
Step 4: Add ads.txt
Google may ask you to publish an ads.txt file.
Create a plain-text file named:
ads.txt
Place it in the root directory of your GitHub repository, alongside files such as index.html and _config.yml.
It will contain a line similar to:
text
google.com, pub-XXXXXXXXXXXXXXXX, DIRECT, f08c47fec0942fa0
Use the exact line supplied by AdSense.
After deployment, it should be accessible at:
https://yourusername.github.io/ads.txt
3. Important: A correction for AcademicPages Analytics configuration
After checking the AcademicPages repository, I found that its configuration supports google-analytics-4 as the provider.
So, for the standard AcademicPages template, I recommend using this configuration in _config.yml instead of the google-gtag example I gave earlier:
yaml
analytics:
provider: "google-analytics-4"
google:
tracking_id: "G-XXXXXXXXXX"
This follows the template’s configuration structure.
GitHub
+1
You can also consult the AcademicPages Google Analytics guide.
4. Important considerations for a website hosted in Germany
Since your website may receive visitors from Germany and other EU countries, pay attention to privacy requirements.
- Add a privacy policy (Datenschutzerklärung) explaining your use of Google Analytics and AdSense.
- Configure appropriate cookie consent before activating non-essential tracking technologies.
- For personalized advertising to EEA visitors, Google requires a Google-certified consent management platform (CMP) integrated with the IAB Transparency and Consent Framework. Google AdSense Help +1
You can find Google’s information here: Google AdSense EU consent requirements.
5. Recommended GitHub repository structure
After integrating both services, your AcademicPages repository could look like this:
text
your-website/
│
├── _config.yml
│
├── _includes/
│ ├── head/
│ │ └── custom.html
│ └── analytics.html
│
├── _layouts/
│
├── _pages/
│
├── assets/
│
├── ads.txt
│
└── index.html
You don’t need to modify the main AcademicPages layout if its existing includes are working correctly.
