diff options
| -rw-r--r-- | CHANGELOG.md | 8 | ||||
| -rw-r--r-- | assets/js/ga.js | 18 | ||||
| -rw-r--r-- | exampleSite/config/_default/config.yaml | 2 | ||||
| -rw-r--r-- | exampleSite/config/_default/params.yaml | 20 | ||||
| -rw-r--r-- | layouts/partials/data.html | 7 | ||||
| -rw-r--r-- | layouts/partials/head.html | 3 | ||||
| -rw-r--r-- | layouts/partials/scripts.html | 11 | ||||
| -rw-r--r-- | layouts/partials/seo/ga.html | 18 |
8 files changed, 48 insertions, 39 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b9e9c7..f21a467 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,13 +14,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Rename template to render JSON Feed from `list.json.json` to - `list.jsonfeed.json` -- Load `ga.js` only on production mode +- Rename template to render JSON Feed from `list.json.json` to `list.jsonfeed.json` +- Rename partial for Google Site Tag tagging/analytics + framework from `layouts/partials/scripts.html` to `layouts/partials/seo/ga.html` +- Use Google Site Tag tagging/analytics framework on production mode only ### Removed - Remove no longer needed jQuery library +- Remove no longer used `layouts/partials/data.html` partial ## [v0.4.0](https://github.com/sergeyklay/gohugo-theme-ed/compare/v0.3.0...v0.4.0) diff --git a/assets/js/ga.js b/assets/js/ga.js index 83d1b6e..b23ff8c 100644 --- a/assets/js/ga.js +++ b/assets/js/ga.js @@ -1,3 +1,5 @@ +import {analyticsCode, anonymizeIp} from '@params'; + function isDoNotTrackEnabled() { if (typeof window === 'undefined') { return false; @@ -22,16 +24,17 @@ if (isDoNotTrackEnabled()) { console.info('[TRACKING]: Respecting DNT with respect to analytics...'); // eslint-disable-line no-console } 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) { + if (analyticsCode) { (function () { // New Google Site Tag (gtag.js) tagging/analytics framework // See: https://developers.google.com/gtagjs const baseUrl = 'https://www.googletagmanager.com'; - let script = document.createElement('script'); + const params = new URLSearchParams({ + id: analyticsCode + }); - script.src = baseUrl + '/gtag/js?id=' + data.analytics_code; + let script = document.createElement('script'); + script.src = baseUrl + '/gtag/js?' + params.toString(); script.type = 'text/javascript'; script.async = true; @@ -88,9 +91,8 @@ if (isDoNotTrackEnabled()) { const month = 30 * 24 * 60 * 60; // 30 days, in seconds // Setup the project analytics code and send a pageview - gtag('config', data.analytics_code, { - 'page_title': data.page_title, - 'anonymize_ip': true, + gtag('config', analyticsCode, { + 'anonymize_ip': anonymizeIp, 'cookie_expires': month }); diff --git a/exampleSite/config/_default/config.yaml b/exampleSite/config/_default/config.yaml index 375ecc2..b610ee8 100644 --- a/exampleSite/config/_default/config.yaml +++ b/exampleSite/config/_default/config.yaml @@ -33,7 +33,7 @@ disableKinds: # # Set `HUGO_ENV` environment variable or `site.Params.env` configuration # parameter to "production" to use Google Analytics. -googleAnalytics: '' +googleAnalytics: 'xxx' minify: # Do not minify XML files to avoid CDATA escape issues diff --git a/exampleSite/config/_default/params.yaml b/exampleSite/config/_default/params.yaml index 42685e5..9a306b3 100644 --- a/exampleSite/config/_default/params.yaml +++ b/exampleSite/config/_default/params.yaml @@ -17,7 +17,7 @@ keywords: # "©" and "{year}" will be replaced by © and the current year. copyright: 'Copyright © 2019-{year} John Doe' -# Color scheme. Options: red, orange, magenta, cyan, blue, brown +# Colour scheme. Options: red, orange, magenta, cyan, blue, brown colorScheme: '' # Used in site header @@ -77,20 +77,26 @@ assets: # Logo image, relative to ./assets logo: /img/open-graph-logo.png disable_fingerprinting: false + seo: # Will be used in schema.org/Organization. # The site.Title will be used if empty. publisher: Serghei Iakovlev + # To anonymize the IP addresses of hits sent to Google Analytics + # set this param to true. Please note, to enable Google Analytics + # you have to set `googleAnalytics` param in config.yaml file. + anonymizeIp: true + social: -# Facebook Page Admin ID for Domain Insights -facebook_admin: '' + # Facebook Page Admin ID for Domain Insights + facebook_admin: '' -# Facebook Page ID -facebook_id: '' + # Facebook Page ID + facebook_id: '' -# Twitter username for the website -twitter: john_doe + # Twitter username for the website + twitter: john_doe # Configure search engine search: diff --git a/layouts/partials/data.html b/layouts/partials/data.html deleted file mode 100644 index afacc83..0000000 --- a/layouts/partials/data.html +++ /dev/null @@ -1,7 +0,0 @@ -<script type="application/json" id="ed-data"> - { - "analytics_code": {{ site.GoogleAnalytics | default "" }}, - "page_title": {{- partial "title.html" . -}}, - "language": {{ site.LanguageCode | default site.Language.Lang }} - } -</script> diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 587becb..7f5bd77 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -28,8 +28,7 @@ <meta name="theme-color" content="#ffffff"> {{ block "custom-head" . }}{{ partial "custom-head.html" . }}{{ end }} - {{ partial "data.html" . }} - {{ partial "scripts.html" . }} + {{ partial "partials/seo/ga.html" . }} {{ block "custom-scripts" . }}{{ partial "custom-scripts.html" . }}{{ end }} {{- /* Misc */}} diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html deleted file mode 100644 index 1f5983a..0000000 --- a/layouts/partials/scripts.html +++ /dev/null @@ -1,11 +0,0 @@ -{{- if or (eq (getenv "HUGO_ENV") "production") (eq site.Params.env "production") }} - {{- with site.GoogleAnalytics -}} - {{- $ga := resources.Get "js/ga.js" | minify -}} - {{- if not site.Params.assets.disable_fingerprinting -}} - {{- $ga = $ga | fingerprint -}} - <script src="{{ $ga.RelPermalink }}" integrity="{{ $ga.Data.Integrity }}"></script> - {{- else -}} - <script src="{{ $ga.RelPermalink }}"></script> - {{- end -}} - {{- end -}} -{{- end -}} diff --git a/layouts/partials/seo/ga.html b/layouts/partials/seo/ga.html new file mode 100644 index 0000000..1a3566e --- /dev/null +++ b/layouts/partials/seo/ga.html @@ -0,0 +1,18 @@ +{{- if (or (eq (getenv "HUGO_ENV") "production") (eq site.Params.env "production")) -}} + {{- with site.GoogleAnalytics -}} + {{- $gaParams := dict "analyticsCode" (site.GoogleAnalytics | default "") "anonymizeIp" (site.Params.anonymizeIp | default true) -}} + + {{- $gaScript := slice -}} + {{- $gaScript = $gaScript | append (resources.Get "js/ga.js") -}} + {{- $gaScript = $gaScript | resources.Concat "js/analytics-bundle.js" -}} + + {{- $gaScript = $gaScript | js.Build (dict "format" "iife" "params" $gaParams) -}} + + {{- if site.Params.assets.disable_fingerprinting }} + <script src="{{ $gaScript.RelPermalink }}"></script> + {{- else -}} + {{- $gaScript = $gaScript | fingerprint }} + <script src="{{ $gaScript.RelPermalink }}" integrity="{{ $gaScript.Data.Integrity }}"></script> + {{- end -}} + {{- end -}} +{{- end -}} |
