mirror of https://github.com/luizdepra/hugo-coder.git

Viktar Patotski
10.34.2021 963dc43f0ff1de6ad97ab0fa69058fc299899b89
Add abbility to use Google Tag Manager for analytics (#514)

* Add ability to embed custom code to <head>

* add myself to CONTRIBUTORS.md

* implement dedicated template for Google Tag Manager

* move google tag manager to bottom of the body tag
3 files modified
1 files added
18 ■■■■■ changed files
CONTRIBUTORS.md 1 ●●●● patch | view | raw | blame | history
exampleSite/config.toml 4 ●●●● patch | view | raw | blame | history
layouts/_default/baseof.html 4 ●●●● patch | view | raw | blame | history
layouts/partials/analytics/googletagmanager.html 9 ●●●●● patch | view | raw | blame | history
CONTRIBUTORS.md
@@ -94,3 +94,4 @@
- [Alex Miranda](https://ammiranda.com)
- [Alphonse Mariya](https://github.com/alfunx)
- [Ziwei Pan](https://github.com/PanZiwei/)
- [Viktar Patotski](https://github.com/xp-vit)
exampleSite/config.toml
@@ -87,6 +87,10 @@
    # For cloud-hosting, use provided URL, e.g. example.matomo.cloud
    serverURL = "analytics.example.com"
# If you want to use Google Tag Manager(https://analytics.google.com/) for analytics, add this section
[params.googleTagManager]
    id = "gid"
# If you want to implement a Content-Security-Policy, add this section
[params.csp]
      childsrc = ["'self'"]
layouts/_default/baseof.html
@@ -147,6 +147,10 @@
    {{ if and .Site.Params.matomo .Site.Params.matomo.serverURL }}
      {{- partial "analytics/matomo" . -}}
    {{ end }}
    {{ if and .Site.Params.googleTagManager .Site.Params.googleTagManager.id }}
      {{- partial "analytics/googletagmanager" . -}}
    {{ end }}
  </body>
</html>
layouts/partials/analytics/googletagmanager.html
New file
@@ -0,0 +1,9 @@
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ $.Site.Params.googleTagManager.id }}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', '{{ $.Site.Params.googleTagManager.id }}');
</script>