You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Minima主题中更新Google Analytics代码片段?

Fixing Minima Theme's Outdated Google Analytics Code

Hey there, let's work through this issue step by step—since you're new to Jekyll and Ruby, I'll keep things straightforward and actionable.

1. Correctly Copy the Minima Google Analytics Template to Your Local Project

Your initial command copied all underscore-prefixed folders from the Minima gem, which might have caused conflicts or missed the exact file you need. Instead, let's target only the GA-specific include file:

  • First, confirm Minima's installation path (run this in your project folder):
    bundle info minima --path
    
    This will output something like /home/your-user/.rbenv/gems/ruby-3.2.2/gems/minima-3.0.1
  • Copy just the Google Analytics include file to your local _includes directory (create the folder if it doesn't exist):
    cp "$(bundle info minima --path)/_includes/google-analytics.html" ./_includes/
    
    This ensures you're overriding only the file responsible for loading GA, without messing with other theme components.

2. Update the Local Template with Google's New Code

Open the newly copied ./_includes/google-analytics.html file and replace its contents with the latest GA4 code snippet (Google's current standard). Here's the template you can use (the {{ site.google_analytics }} tag will pull your ID from _config.yml automatically):

<!-- 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>
  • Critical Check: Double-check your _config.yml file to make sure your google_analytics value is a GA4 ID (starts with G-, not the old UA- format). It should look like this:
    google_analytics: G-XXXXXXXXXX
    

3. Rebuild Your Site with a Clean Cache

Jekyll sometimes caches old files, which is why your first build didn't reflect changes. Follow these steps to ensure fresh output:

  • Clear the Jekyll cache to remove stale files:
    bundle exec jekyll clean
    
  • Rebuild and serve your site locally to test the changes:
    bundle exec jekyll serve
    
  • Visit localhost:4000 in your browser, right-click > "View Page Source", and search for your GA4 ID to confirm the new code is loading correctly.

4. Extra Troubleshooting Tips

  • If you still don't see the new code: Ensure your local _includes/google-analytics.html is the one being used—Jekyll prioritizes local files over theme gem files, so as long as this file exists in your project, it should override the theme's version.
  • Check layout references: Most Minima versions load the GA include in _layouts/default.html. If you copied the entire _layouts folder earlier, make sure that file still has {% include google-analytics.html %} in the <head> section.
  • Monitor the GitHub issue: Once Minima officially updates their GA template, you can delete your local _includes/google-analytics.html file and run bundle update minima to use the official fix.

内容的提问来源于stack exchange,提问作者Rotkiv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:12:29