pages-themes / midnight

Midnight is a Jekyll theme for GitHub Pages
https://pages-themes.github.io/midnight/
Creative Commons Zero v1.0 Universal
305 stars 566 forks source link

support for new Google Analytics #26

Open bradipao opened 3 years ago

bradipao commented 3 years ago

I tried to enable Google Analytics using the proper parameter (google_analytics) in _config.yml and the intended code is actually injected in the page, but apparently it is valid only for old Analytics tracking IDs (UA-xxxxxxx). New measurement IDs (G-xxxxxxxx) seems to need a different script code.

I modified _default.html removing current code and I added new code just after HEAD tag as per Google Analytics documentation ( https://developers.google.com/analytics/devguides/collection/gtagjs ). After a few try it seems working now.

It is still compatible with the "google_analytics" parameter in _config.yml, the difference is just in the script code. But in a more general approach probably a dedicated parameter would be better, to have both scripts in the layout.

<head>

  {% if site.google_analytics %}
    <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>
  {% endif %}
parkr commented 3 years ago

In #30, we are introducing a new include for custom headers. I'd propose that we put this old code there (for backwards compatibility) and then allow folks who want to use v4 to put that custom data into a custom include for Google Analytics.

parkr commented 3 years ago

To be clearer:

  1. _layouts/default.html contains {% include head-custom.html %} or similar.
  2. _includes/head-custom.html (maybe make it underscore-delimited) includes {% include google_analytics.html %}
  3. _includes/google_analytics.html contains pre-existing v3 code. User replaces this include inside their site with the code they want to be included instead.
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ynikitenko commented 2 years ago

Thank you! It would be great if you could add some information to the main page. I installed GA4, and only then realized that I need the other tracking number.