diff options
| -rw-r--r-- | assets/js/ga.js | 51 | ||||
| -rw-r--r-- | exampleSite/config.toml | 3 | ||||
| -rw-r--r-- | layouts/partials/data.html | 7 | ||||
| -rw-r--r-- | layouts/partials/head.html | 13 | ||||
| -rw-r--r-- | netlify.toml | 3 |
5 files changed, 75 insertions, 2 deletions
diff --git a/assets/js/ga.js b/assets/js/ga.js new file mode 100644 index 0000000..2f855c3 --- /dev/null +++ b/assets/js/ga.js @@ -0,0 +1,51 @@ +function isDoNotTrackEnabled() { + if (typeof window === 'undefined') return false + const {doNotTrack, navigator} = window + + // Do Not Track Settings across browsers + const dnt = (doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack) + + if (!dnt) return false + + return dnt === true || + dnt === 1 || + dnt === 'yes' || + (typeof dnt === 'string' && dnt.charAt(0) === '1'); +} + +if (isDoNotTrackEnabled()) { + // Skip analytics for users with Do Not Track enabled + console.info('[TRACKING]: Respecting DNT with respect to analytics...') +} else { + // Known DNT values not set, so we will assume it's off. + const data = JSON.parse(document.getElementById('ed-data').innerHTML) + + if (typeof data !== 'undefined' && data.analytics_code) { + (function () { + // New Google Site Tag (gtag.js) tagging/analytics framework + // See: https://developers.google.com/gtagjs + const base_url = 'https://www.googletagmanager.com' + let script = document.createElement("script"); + + script.src = base_url + "/gtag/js?id=" + data.analytics_code; + script.type = "text/javascript"; + script.async = true; + + document.getElementsByTagName("head")[0].appendChild(script); + }()) + + window.dataLayer = window.dataLayer || []; + + function gtag() { + dataLayer.push(arguments); + } + + gtag('js', new Date()); + + // Setup the project analytics code and send a pageview + gtag('config', data.analytics_code, { + 'anonymize_ip': true, + 'cookie_expires': 30 * 24 * 60 * 60 // 30 days + }) + } +} diff --git a/exampleSite/config.toml b/exampleSite/config.toml index d763fcd..dd59415 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -27,6 +27,9 @@ refLinksErrorLevel = 'WARNING' # kick out RSS, we'll use Atom and FEED bellow disableKinds = ['RSS'] +# Google Analytics Tracking id +googleAnalytics = '' + [minify] # Do not minify XML files to avoid CDATA escape issues disableXML = true diff --git a/layouts/partials/data.html b/layouts/partials/data.html new file mode 100644 index 0000000..4eedc7b --- /dev/null +++ b/layouts/partials/data.html @@ -0,0 +1,7 @@ +<script type="application/json" id="ed-data"> + { + "analytics_code": {{ site.GoogleAnalytics | default "" }}, + "site_name": {{ site.Title }}, + "language": {{ site.LanguageCode | default site.Language.Lang }} + } +</script> diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 0c9329d..e77e902 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -11,7 +11,8 @@ <meta name="author" content="{{ partial "author.html" . }}"> {{ hugo.Generator }} - {{- partial "styles.html" . }} + {{ partial "styles.html" . }} + {{ partial "data.html" . }} {{ block "custom-scripts" . }}{{ partial "custom-scripts.html" . }}{{ end }} {{- /* Favicons */}} @@ -29,6 +30,16 @@ <meta name="theme-color" content="#ffffff"> {{ block "custom-head" . }}{{ partial "custom-head.html" . }}{{ end }} + {{ with site.GoogleAnalytics }} + {{ $ga := resources.Get "js/ga.js" | minify | fingerprint }} + {{ if not site.Params.assets.disable_fingerprinting }} + {{ $ga = $ga | fingerprint }} + <script src="{{ $ga.Permalink }}" integrity="{{ $ga.Data.Integrity }}"></script> + {{ else }} + <script src="{{ $ga.Permalink }}"></script> + {{ end }} + {{ end }} + {{- /* Misc */}} {{ template "partials/templates/opengraph.html" . }} {{ template "partials/templates/twitter_cards.html" . }} diff --git a/netlify.toml b/netlify.toml index edd3d5e..b8eaddc 100644 --- a/netlify.toml +++ b/netlify.toml @@ -6,6 +6,7 @@ HUGO_VERSION = '0.100.2' HUGO_ENV = 'production' HUGO_ENABLEGITINFO = 'true' + HUGO_GOOGLEANALYTICS = 'G-DP9Q137C3X' [context.split1] command = 'hugo --source=exampleSite --gc --minify --enableGitInfo --destination ../public' @@ -67,7 +68,7 @@ # files received from those allowed domains, ignoring all other scripts # (including inline scripts and event-handling HTML attributes). # - Content-Security-Policy = "base-uri 'self'; style-src 'self' 'unsafe-inline'; font-src 'self'; object-src 'self'; media-src 'self'; worker-src 'self'; img-src 'self' data:; script-src 'self' *.netlify.app *.netlify.com; form-action 'self' submit-form.com; frame-ancestors 'none'; manifest-src 'self'; connect-src 'self';" + Content-Security-Policy = "base-uri 'self'; style-src 'self' 'unsafe-inline'; font-src 'self'; object-src 'self'; media-src 'self'; worker-src 'self'; img-src 'self' data:; script-src 'self' *.netlify.app *.netlify.com www.googletagmanager.com; form-action 'self' submit-form.com; frame-ancestors 'none'; manifest-src 'self'; connect-src 'self';" [[headers]] for = '/feeds/*.xml' |
