divinerites/plausible-hugo
概要
Dead simple integration between plausible.io and gohugo.io
データ・分析計測したスター増加
- 24時間
- 0
- 7日間
- 0
- 15日間
- 0
- 1か月
- 0
期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。
README.md
オリジナルplausible-hugo : A theme component & Hugo module
According to their web site, "Plausible Analytics, is a simple, open-source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics"
And Hugo is The world’s fastest framework for building websites.
This Hugo theme component & module is a dead simple integration between plausible.io and gohugo.io
Want deep knowledge about plausible-hugo ? :
Please, consider leaving a star on Github if you like it.
Usage is simple as 1-2-3
- Add this
plausible-hugocomponent as a theme in your theme section inconfig.toml. - Add a
[params.plausible]section in yourconfig.tomlfile. - Call the partial
plausible_head.htmlin your own<head>section.
That's it ! It works. Nothing more is needed
Mininum config.toml file
Two modes. The mode is chosen by script_id : without it, nothing changes from v1 (same output, same parameters). With it, the module writes the new Plausible script.
Mode v1 (legacy) : the script script.js, with domain. Existing sites need no change.
theme = ["plausible-hugo"] # Add this theme to your already existing other themes
[params.plausible]
enable = true # Whether to enable plausible tracking
domain = "example.com" # Plausible "domain" name/id in your dashboard
Mode v2 (new Plausible script) : add script_id.
theme = ["plausible-hugo"]
[params.plausible]
enable = true
script_id = "pa-XXXXXXXXXXXXXXXXXXXXX" # Script id given by Plausible (see below)
Example <head> html section
<head>
...
{{ partial "plausible_head.html" . }}
...
</head>
You can have a look at the end of this file for a minimal working example as a hugo module.
Hugo Module
You also can use it as a versioned Hugo Module if you prefer and know how to do it. Requires Hugo 0.126.3 or newer. Since v2.0.0 the module path ends with /v2 (a Go requirement for major versions). This import change is the only action required to upgrade from v1. Nothing else changes in your config or in the generated HTML.
We strongly advise for this method, as it will be simple and transparent to keep up with our version updates.
In this case, instead of declaring this as a theme, just add it as a hugo module.
[module]
[[module.imports]]
path = "github.com/divinerites/plausible-hugo/v2"
Where is my script_id ? (new script, optional)
In Plausible : Site settings > Site installation. The snippet looks like this :
<script async src="https://plausible.io/js/pa-XXXXXXXXXXXXXXXXXXXXX.js"></script>
<script>window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};plausible.init()</script>
Your script_id is the pa-XXXXXXXXXXXXXXXXXXXXX part. With script_id the module writes this snippet for you in <head>.
Upgrading from v1 to v2
The only required action is the import path. Go requires the /v2 suffix for a major version. In your config.toml (or hugo.toml) :
[module]
[[module.imports]]
path = "github.com/divinerites/plausible-hugo/v2" # was ".../plausible-hugo"
Then update, and re-vendor if you use _vendor :
hugo mod get -u github.com/divinerites/plausible-hugo/v2
hugo mod tidy
hugo mod vendor # only if you use _vendor
If you use it as a theme (theme = ["plausible-hugo"]), there is no import path to change.
Without script_id, the generated HTML, _redirects and CSP are identical to v1.22.3, with one accepted difference : an empty plausible_js.html no longer writes <script></script>. Every v1 parameter still works : domain, outbound_link, file_downloads, file_downloads_types, and the global options (hash_based_routing, proxy_netlify, selfhosted_domain, debug, …). Hugo versions before 0.120 are still handled by the module (the minimum is 0.126.3 since v1.22.3).
Migration to the new Plausible script (optional)
v2.0.0 can use the new Plausible script : one script per site (pa-<id>.js) and a plausible.init({...}) call. It needs Plausible Cloud, or Plausible CE 3.1.0 or later for self hosting. To switch, add script_id. To go back, remove it.
What changes once script_id is set (see the parameter tables below) :
| Parameter | With script_id |
|---|---|
domain |
Not required any more, except for public_dashboard. |
outbound_link, file_downloads, file_downloads_types |
Ignored. Set them in your Plausible dashboard (Site settings > Tracking). You can remove them. |
hash_based_routing |
Sets the hashBasedRouting option of plausible.init, no more .hash in the script name. |
debug |
In hugo server, loads the script and sets the captureOnLocalhost option of plausible.init. |
proxy_netlify |
The page loads /misc/js/script.js (generic name from the module config). Events go to /misc/api/event, and init receives endpoint. The pa- id is only in _redirects. |
selfhosted_domain |
Used as the script and event host (also in _redirects). |
enable, public_dashboard, dash_*, gitstar |
Unchanged. |
plausible_do_not_track, plausible_custom_goal (front matter) |
Unchanged. |
If you use proxy_netlify, the plausible_redirects_netlify.html partial generates the rule for you. The page requests the generic /misc/js/script.js ; the pa- id appears only in _redirects. If you wrote the 2 lines by hand, use :
/misc/js/script.js https://plausible.io/js/pa-XXXXXXXXXXXXXXXXXXXXX.js 200
/misc/api/event https://plausible.io/api/event 200
Other behaviours with script_id :
plausible_js.htmlis still your extension point for custom goals. An emptyplausible_js.htmldoes not write an empty<script></script>(also in v1 mode, the only accepted difference with v1.22.3).plausible_csp.htmland the CSP comment are the same as in v1 ('unsafe-inline' https://<host>). The inlineplausible.initsnippet needs'unsafe-inline', like the v1 inline goals script. Withproxy_netlifyyou only need'self'.- In
hugo server, the script is not loaded (unlessdebug = true), but the smallwindow.plausiblestub is still written, so youronclick="plausible(...)"never fails.
You can try both modes with the exampleSite/ of this repository : cd exampleSite && hugo --minify (add --environment proxy for the Netlify proxy, --environment legacy for the v1 script).
--- Parameters
The parameters below follow the layout of the module config.toml.
--- v1 script only
-----
Used when script_id is not set. The module writes the legacy script.<ext>.js snippet, with data-domain.
| Parameter | Role |
|---|---|
domain |
Required. Your Plausible site name, as in the dashboard. Also required for public_dashboard in v1 and in v2. |
outbound_link |
Track outbound link clicks (adds .outbound-links to the script name). |
file_downloads |
Track file downloads (adds .file-downloads to the script name). |
file_downloads_types |
Optional list of file extensions, for example "sh,run". Overrides the default list. |
--- v2 script only
-----
Used when script_id is set. The module writes the new pa-<id>.js script and plausible.init({...}).
| Parameter | Role |
|---|---|
script_id |
The pa-… id given by Plausible (Site settings > Site installation). This is what switches the module to the v2 script. |
--- Global options (v1 and v2)
-----
| Parameter | Role |
|---|---|
enable |
Turns tracking on or off. |
proxy_netlify |
Serve the script and events through a Netlify proxy (/misc/js/… and /misc/api/event). |
hash_based_routing |
Track hash-based URLs. In v1 it adds .hash to the script name ; in v2 it sets hashBasedRouting in plausible.init. |
selfhosted_domain |
Your self-hosted Plausible host, instead of plausible.io. |
public_dashboard |
Write the public dashboard URL in the HTML source. Needs domain in v1 and v2. |
dash_link, dash_theme, dash_bgcolor, dash_disable |
Embedded dashboard (the plausible_dashboard.html partial). |
debug |
In hugo server, load the script anyway (v2 also sets captureOnLocalhost). |
gitstar |
Set to false to hide the “leave a star on GitHub” reminder. |
Front matter, in both modes : plausible_do_not_track (skip the page) and plausible_custom_goal (send an event when the page is opened).
--- Deprecated
-----
custom_js_domain is fully deprecated. Use proxy_netlify instead.
In v1 the parameter still works, for compatibility, and Hugo prints the same console warning as in v1.22.3. In v2 it is ignored.
Using Plausible goals
1 - Simple custom goals
If you want to use some custom goals, for each goal, you just have to add a snipplet in a partial named plausible_js.html that you have to create in your site /partials directory
Then, add a straightforward call to those plausible function/goal where you need it in your gohugo code.
This is an example, if you want a plausible custom goal named ClickOnTelephoneNumber.
Snipplet example for plausible_js.html
function ClickOnTelephoneNumber() {
plausible('ClickOnTelephoneNumber');
}
Example of integration in your html code
<a href="tel:+331234567890"
onclick="ClickOnTelephoneNumber()">
+331234567890
</a>
2 - Custom goals when entering a page
You can have a use case where you want a custom goal setup when you enter a certain page. So you can have more granularity than the classic "page goals".
Just add a front matter parameter plausible_custom_goal in this page.
Each time you enter the page, your custom goal is reached.
---
plausible_custom_goal : "MySpecialCustomGoal"
---
3 - Variable custom goals
Using variables for your Goal names
For more flexibility, do not forget that you can use any {{ $variable }} or {{ .variable }} for your Goal name.
For example, generate your goals using /data/about.yml or similar frontmatter, you get the idea.
Then you’ll get 2 goals : telephoneMobileAbout & telephoneHomeAbout
A - Example : /data/about.yml
about:
enable : true
title : My title
about_item :
- title : An other item title
plausible : Mobile
phone : "+33 1 23 45 67 89"
content : lorem ipsum is better than nothing.
- title : An other item title 2
plausible : Home
phone : "+33 9 87 65 43 21"
content : lorem ipsum is better than nothing 2
B - Generate the calls in your /partial/about.html example
{{- $data := index .Site.Data .Site.Language.Lang }}
{{- if $data.about.about.enable }}
{{- range $data.about.about.about_item }}
<!-- sanitize phone number for tel: function -->
{{ $phone_ok := (replaceRE "(\\s)" "" .phone ) }}
<a href="tel:{{ .phone_ok }}"
onclick="telephone{{ .plausible | safeJS }}About()">
{{ .phone }}
</a>
{{- end }}
{{- end }}
C - Sniplet for plausible_js.html
{{- $data := index .Site.Data .Site.Language.Lang }}
{{- if $data.about.about.enable }}
{{- range $data.about.about.about_item }}
function telephone{{ .plausible | safeJS }}About() {
plausible('telephone{{ .plausible | safeJS }}About');
}
{{- end }}
{{- end }}
4 - 404 custom goal
We take care of the 404 pages for you.
You only have to configure the goal in your Plausible settings for them to show up on your dashboard : Select Custom event as the goal trigger and enter this exact name: 404.
Other options
1 - Optional measurements (outbound links, file downloads, forms)
With script_id (new script) : they are set in your Plausible dashboard (Site settings > Tracking), no change in your site is needed. See Plausible documentation.
Without script_id (v1 script) : use the parameters below, as before.
v1 script - Outbound Link custom goal
If you want to use the Outbound Link custom goal,
just add the parameter outbound_link to your params.plausible section.
[params.plausible]
outbound_link = true
v1 script - File downloads tracking custom goal
If you want to use the File downloads tracking custom goal,
just add the parameter file_downloads to your params.plausible section.
[params.plausible]
file_downloads = true
Track a different file type
If you want to track different file types like .sh and .run, just add the parameter file_downloads_types to your params.plausible section.
[params.plausible]
file_downloads_types = "sh,run"
Using the file_downloads_types parameter will override the default list and only your custom file type downloads will be tracked.
2 - Hash-based routing
If you want Hash-based routing, just add the parameter hash_based_routing to your params.plausible section. With the v1 script it adds .hash to the script name. With script_id it sets hashBasedRouting: true in plausible.init.
[params.plausible]
hash_based_routing = true
3 - Do not track certain pages
You can prevent certain pages from being tracked by adding plausible_do_not_track: true in the page Front Matter
---
plausible_do_not_track: true
---
4 - Proxy for analytics and manage Adblockers
Some adblockers/browsers block every tracking script, even privacy-first analytics like plausible.io
You can deal with this by proxying the script.
If you are hosting on Netlify, we have done the main setup for you.
1 - Set proxy_netlify on your config.toml.
[params.plausible]
proxy_netlify = true
With script_id, the page loads /misc/js/script.js (the same public name as v1, from the module config) and calls plausible.init({"endpoint":"/misc/api/event"}). The pa- id appears only in _redirects. Without script_id, it loads /misc/js/script.js with data-api, as in v1.
2 - Add the partial plausible_redirects_netlify.html in your index.redir file. So your final generated _redirects will take care of this proxying.
# Netlify redirects from aliases
{{ range $p := site.Pages -}}
{{ range .Aliases }}
{{ . | printf "%-35s" }} {{ $p.RelPermalink }} 301!
{{ end -}}
{{- end -}}
{{ partial "plausible_redirects_netlify.html" . }}
3 - If you do not already use the index.redir system, you just can create a _redirects file in your /static folder and add those 2 lines below in this file (v1 script).
/misc/js/script.js https://plausible.io/js/script.js 200
/misc/api/event https://plausible.io/api/event 200
If you want outbound-links, use this line instead:
/misc/js/script.js https://plausible.io/js/script.outbound-links.js 200
file_downloads and hash_based_routing are part of the path the page requests (/misc/js/script.file-downloads.hash.js, for example). outbound_link is not: the page still requests the path without .outbound-links, and the redirect target adds it. With all three options on, the lines are:
/misc/js/script.file-downloads.hash.js https://plausible.io/js/script.file-downloads.outbound-links.hash.js 200
/misc/api/event https://plausible.io/api/event 200
With script_id, use these 2 lines instead (the public path is generic ; put your own id only on the right-hand side) :
/misc/js/script.js https://plausible.io/js/pa-XXXXXXXXXXXXXXXXXXXXX.js 200
/misc/api/event https://plausible.io/api/event 200
The plausible_redirects_netlify.html partial builds the right lines for the mode you use, and for every v1 script combination. Prefer it to a hand-written _redirects.
5 - Embed your Plausible.io dashboard in your Hugo site
You can embed your Plausible stats dashboard into on your hugo website using an iFrame. This is useful in case you want to showcase your stats.
First :
- you need to generate a shared link in your own Plausible dashboard.
- Do not give a theme or background color. You can do this with Hugo params.
- For example it will generate
https://plausible.io/share/yourdomain?auth=AZE1234RTYUOP67.
Then, just put this value in a .plausible.dash_link parameter.
[params.plausible]
dash_link = "https://plausible.io/share/yourdomain?auth=AZE1234RTYUOP67"
You also can add 2 parameter if you want, but this is optional :
- the theme you want (
light/dark/system). Default islight. - the custom background color.
- Tip : You can set the background color as
transparent.
- Tip : You can set the background color as
[params.plausible]
dash_theme = "dark"
dash_bgcolor = "#880011"
Then just add this partial where you want your embeded Plausible dashboard.
...
{{ partial "plausible_dashboard.html" .}}
...
Activating the embed dashboard
This embed dashboard feature do not rely on .plausible.enable variable.
So it will be shown if you simply add the plausible_dashboard.html template. Whatever is plausible.enable
In this case, for manually disabling the dashboard you can set dash_disable variable to true.
[params.plausible]
dash_disable = true
6 - Write public dashboard information in Web page source
If you made your dashboard public, you may want to write this url in your web page source, so people can find it more easily.
Just add public_dashboard = true in your config.toml plausible section. By default this option is set to false, so nothing is written by default.
[params.plausible]
public_dashboard = true
And this will be written in your HTML source code. It also works for self hosting.
<!-- Plausible Analytics public dashboard URL : https://plausible.io/example.com -->
7 - Self Hosting
You can define your self hosted domain address in config.toml. The new script (script_id) needs Plausible CE 3.1.0 or later.
v1 and v2 scripts are not tested by the developer in self-hosted mode.
This is optional, and plausible.io is used if this parameter is unset.
[params.plausible]
selfhosted_domain = "myplausible.example.com" # Self-hosted plausible domain
8 - Plausible and Content-Security-Policy
Be careful if you have some CSP in your headers, do not forget to allow plausible domains you use.
You can add directly the requested domain(s) to your existing Content-Security-Policy.
Or use this plausible_csp.html partial in your /layouts/index.headersto generate for you the correct _headers, used by Netlify:
CSP example for /layouts/index.headers
Content-Security-Policy: [... existing stuff ...] {{ partial "plausible_csp.html" . }}
The partial and the comment are the same with the v1 script and with script_id. 'unsafe-inline' is needed by the inline plausible.init snippet (and by the v1 goals script).
In any case, in the HTML source code, you'll find a comment with the correct domain to add to your CSP.
Developping locally with plausible-hugo
1 - Mode server
When you're in hugo mode server, the call to plausible.io javascript is disable, so you can dev without bloating your statistics.
2 - Debug mode
When in mode server you could really need to test your code in real situation.
In this case, just add debug = true in your [params.plausible].
The call to plausible.io javascript will be enable (with the captureOnLocalhost option), so you can have everything working like in production.
[params.plausible]
debug = true # debug mode
Various
1 - Check variables & module versions
We perform an automatic check if the right mandatory variables are fitting the right version.
If we found a problem, we abort your build with a clear message.
You will also receive warning message if you are using deprecated features.
So it helps you updating your config.toml in case we introduced some breaking changes in variables names between versions. It is rare, but it happened already.
2 - Giving a star on github.com
By default we put a console friendly reminder to consider giving a star on github if you like this module.
Obviously you can disable this message, just by adding gitstar = false in your params.plausible.
[params.plausible]
gitstar = false
3 - Credits
- Hugo : www.gohugo.io
- Plausible : www.plausible.io
- Plausible documentation : docs.plausible.io
Configuration example (hugo module option)
config.toml
[module]
[[module.imports]]
path = "github.com/divinerites/plausible-hugo/v2"
[params.plausible]
enable = true
# --- v1 script only
# ---------------------------------------------------
domain = "my-domain-id"
outbound_link = true
file_downloads = true
# --- v2 script only (replaces the v1 block above)
# ---------------------------------------------------
# script_id = "pa-XXXXXXXXXXXXXXXXXXXXX"
# --- Global options
# ---------------------------------------------------
debug = false
gitstar = false
index.html
<!DOCTYPE html>
<html>
<head>
...
{{ partial "plausible_head.html" . }}
...
</head>
</html>
関連プロジェクトを探す
読み込み中…