Jekyll build ends up missing its Google Analytics

Hi guys,

I’m struggling lately with a key difference between my local deployment and what ends up on the netlify deploy. I’m using Jekyll and when I build my site locally I see that I have Google Analytics on every page. However when it’s deployed to Netlify the Google Analytics are missing.

I checked and sure that:

  • both builds are using the JEKYLL_ENV=production environment
  • ruby version is the same - I defined it in .ruby-version
  • I compared output and I see that Jekyll is also on the same version in both my local env and the deploy output on Netlify

Could someone explain this difference somehow? Is it possible that Google Analytics are filtered somehow from Netlify?

Thanks,
Kalin

That generally doesn’t happen.

Would you be willing to share your URL or repo?

My blog is the one being deployed: http://kstaykov.eu/

Sadly the live URL has no clue. A repo would be needed.

That’s the repo: Bitbucket

Sorry it took long. I had to install Jekyll as it was my first time trying it out, so excuse me if I ask something stupid.

I ran bundle exec jekyll serve and I don’t see any Google Analytics on your webpage. Are you sure it’s there when you run your website locally? Is there any special configuration needed?

I also found some article online on how you can add Analytics to Jekyll if you haven’t checked it out already: Google Analytics setup for Jekyll / Michael Lee

I used to have it but suddenly it stopped working. Maybe I’m doing it wrong somehow but when I build the Jekyll site like that:

JEKYLL_ENV=production bundle exec jekyll build -d new-directory

I can see that analytics urls are present:

bash-5.0# grep -r analytics new-directory/ | head
new-directory/404/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/404/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/blog/ACM/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/blog/Today-I-Choose/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/blog/codewars/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/blog/move-to-netlify/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/blog/new-workflow/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/categories/blog/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/categories/crypto/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
new-directory/blah/categories/dev/index.html:<script src="https://www.google-analytics.com/analytics.js" async></script>
bash-5.0#

Since I’m on Windows, I had to modify the command a little. I used this: set JEKYLL_ENV=production | jekyll build. In the generated folder’s index.html, I don’t see any Google Analytics tags.

I guess you reproduced the problem so it must be in the code. I cannot see that with the docker container I use for Jekyll - “jekyll/builder”. Building it with that container results in a site that has the analytics tags.

I’ll install Jekyll outside docker and give it another try. Thanks for the help, now at least I know that it’s a code problem.

Let us know your findings and if at all it’s a problem in the code, don’t forget to post the solution as it might help the next person.

1 Like

I solved the problem! I’ll explain what was happening in case someone else needs this fix.

Looks like the problem is with the “JEKYLL_ENV: production” environment variable not being set. It was missing on the Netlify build and as soon as I added it from the configuration UI the analytics came back.

Which means that you were probably not successful under Windows when you tried setting this variable.

What got me confused in the first place was that I was using jekyll/builder:latest docker image which has the JEKYLL_ENV set to production:

When I tried using the minimal image the problem finally reproduced because it doesn’t have the variable:

I hope someone finds this helpful :slight_smile:

Hey there @kstaykov ,

Welcome to the Netlify Forums! Thank you so much for sharing your solution. This will help future community members if they encounter a similar situation, so we greatly appreciate it.

Best,
Hillary