summaryrefslogtreecommitdiff
path: root/_includes/extensions/google-analytics.html
blob: 9ae896390e3a23b6dcf44f57ebcd31e8fd84ebed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!-- Google tag (gtag.js) -->
<script>
  function initGoogleAnalytics() {
    var doNotTrack = (window.doNotTrack === "1" || navigator.doNotTrack === "1" ||
      navigator.doNotTrack === "yes" || navigator.msDoNotTrack === "1");
    var enableDNT = "{{ site.enableDNT | default: true }}" == "true";

    if (!enableDNT || !doNotTrack) {
      var measurementId = '{{ site.google_analytics }}';

      (function(src) {
        var tag = document.createElement('script');
        tag.src = src;
        tag.async = true;
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      })('https://www.googletagmanager.com/gtag/js?id=' + measurementId);

      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', measurementId);
    }
  }
  window.addEventListener("load", initGoogleAnalytics);
</script>