@ampless/plugin-plausible 0.1.1-alpha.8 → 0.2.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +43 -0
- package/README.md +43 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +53 -1
- package/package.json +2 -2
package/README.ja.md
CHANGED
|
@@ -43,6 +43,49 @@ export default defineConfig({
|
|
|
43
43
|
| `domain` | `''` | 初期 Plausible サイトドメイン。例: `example.com`。Plausible 管理画面に登録した値と完全一致させる必要がある — ミスマッチだとページビューが silently drop される。空文字でプラグインを無効化したまま ship できる。 |
|
|
44
44
|
| `scriptUrl` | `'https://plausible.io/js/script.js'` | Plausible スクリプトの URL。self-hosted Plausible を使う場合に上書き(例: `'https://analytics.example.com/js/script.js'`)。admin field は **required** なので値を消すことはできない。plausible.io デフォルトに戻すには admin form の **デフォルトに戻す** を使う。 |
|
|
45
45
|
| `instanceId` | `'plausible'` | script 要素 id と設定保存キーの namespace。同サイトに複数の Plausible site を入れる場合に分ける。 |
|
|
46
|
+
| `consentCategory` | `''` | オプションの同意カテゴリ slug。設定すると `window.amplessConsent.has(<この値>)` が true になるまで Plausible loader を発火しない。詳細は[同意ゲーティング](#同意ゲーティング)を参照。 |
|
|
47
|
+
|
|
48
|
+
## 同意ゲーティング
|
|
49
|
+
|
|
50
|
+
Plausible はプライバシー重視・cookie 不使用の解析サービスであり、多くの導入では同意ゲーティングは不要です。ただし、サイトの法的要件やプライバシーポリシーで解析スクリプト読み込み前の明示的同意が必要な場合は gated mode を有効にできます:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { defineConfig } from 'ampless'
|
|
54
|
+
import cookieConsent from '@ampless/plugin-cookie-consent'
|
|
55
|
+
import plausiblePlugin from '@ampless/plugin-plausible'
|
|
56
|
+
|
|
57
|
+
export default defineConfig({
|
|
58
|
+
plugins: [
|
|
59
|
+
// cookie-consent は Plausible plugin より前に置く
|
|
60
|
+
cookieConsent({
|
|
61
|
+
categories: [{ id: 'analytics', label: 'アナリティクス', defaultEnabled: false }],
|
|
62
|
+
}),
|
|
63
|
+
plausiblePlugin({
|
|
64
|
+
domain: 'example.com',
|
|
65
|
+
consentCategory: 'analytics',
|
|
66
|
+
}),
|
|
67
|
+
],
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`consentCategory` を設定すると plugin は **gated mode** に切り替わります: 標準の `<script>` descriptor の代わりに単一のインライン script を emit し、以下の動作をします:
|
|
72
|
+
|
|
73
|
+
1. 直ちに `window.amplessConsent.has('analytics')` を確認(`localStorage` から復元した同意に対応)。
|
|
74
|
+
2. false なら `window.amplessConsent.on('analytics', ...)` で同意イベントを購読して待機。
|
|
75
|
+
3. Plausible plugin が cookie-consent plugin より先にロードされたケースに備え、`ampless:consent-ready` イベントも購読。
|
|
76
|
+
|
|
77
|
+
**Fail-closed 契約:** `consentCategory` を設定したまま `@ampless/plugin-cookie-consent` を登録しなかった場合、`window.amplessConsent` は install されません。Plausible は**永久に発火しない**まま 5 秒後に `console.warn` が出力されます:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
[ampless:plausible] consentCategory is set but window.amplessConsent never installed.
|
|
81
|
+
Did you forget to register @ampless/plugin-cookie-consent?
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
この warning は本番環境でも発火します。設定ミスを早期に検出するためのものであり、抑制する仕組みはありません。
|
|
85
|
+
|
|
86
|
+
**プラグインの順序:** `plugins` 配列内で `@ampless/plugin-cookie-consent` を Plausible plugin より前に置いてください。
|
|
87
|
+
|
|
88
|
+
Consent Convention および `window.amplessConsent` API の詳細は [docs/architecture/08-plugin-architecture.md](https://github.com/heavymoons/ampless/blob/main/docs/architecture/08-plugin-architecture.md) を参照してください。
|
|
46
89
|
|
|
47
90
|
## 管理画面から設定を編集する
|
|
48
91
|
|
package/README.md
CHANGED
|
@@ -43,6 +43,49 @@ export default defineConfig({
|
|
|
43
43
|
| `domain` | `''` | Initial Plausible site domain, e.g. `example.com`. Must match the value registered in the Plausible dashboard exactly — a mismatch silently drops every pageview. Set to `''` to ship the plugin disabled. |
|
|
44
44
|
| `scriptUrl` | `'https://plausible.io/js/script.js'` | URL of the Plausible script. Override for self-hosted Plausible (e.g. `'https://analytics.example.com/js/script.js'`). The admin field is **required**, so the value can't be cleared — to switch back to the hosted plausible.io URL, use **Reset to default** in the admin form. |
|
|
45
45
|
| `instanceId` | `'plausible'` | Namespace used for the script element id and settings storage key. Set distinct values when registering multiple Plausible sites on the same deployment. |
|
|
46
|
+
| `consentCategory` | `''` | Optional consent category slug. When set, the Plausible loader fires only after `window.amplessConsent.has(<this>)` returns true. See [Consent gating](#consent-gating) below. |
|
|
47
|
+
|
|
48
|
+
## Consent gating
|
|
49
|
+
|
|
50
|
+
Plausible is a privacy-focused, cookie-free analytics service — most deployments do not need consent gating at all. However, if your site's legal requirements or privacy policy demand explicit consent before any analytics script loads, you can enable gated mode:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { defineConfig } from 'ampless'
|
|
54
|
+
import cookieConsent from '@ampless/plugin-cookie-consent'
|
|
55
|
+
import plausiblePlugin from '@ampless/plugin-plausible'
|
|
56
|
+
|
|
57
|
+
export default defineConfig({
|
|
58
|
+
plugins: [
|
|
59
|
+
// cookie-consent must appear before the analytics plugin
|
|
60
|
+
cookieConsent({
|
|
61
|
+
categories: [{ id: 'analytics', label: 'Analytics', defaultEnabled: false }],
|
|
62
|
+
}),
|
|
63
|
+
plausiblePlugin({
|
|
64
|
+
domain: 'example.com',
|
|
65
|
+
consentCategory: 'analytics',
|
|
66
|
+
}),
|
|
67
|
+
],
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
When `consentCategory` is set the plugin switches to **gated mode**: instead of the standard `<script>` descriptor, it emits a single inline script that:
|
|
72
|
+
|
|
73
|
+
1. Checks `window.amplessConsent.has('analytics')` immediately (covers consent restored from `localStorage`).
|
|
74
|
+
2. Otherwise subscribes to the consent event via `window.amplessConsent.on('analytics', ...)` and waits.
|
|
75
|
+
3. Also listens for `ampless:consent-ready` in case the Plausible plugin loads before the cookie-consent plugin has installed its global API.
|
|
76
|
+
|
|
77
|
+
**Fail-closed contract:** if `consentCategory` is set but `@ampless/plugin-cookie-consent` is never registered, `window.amplessConsent` is never installed. Plausible will **never fire**, and after 5 seconds a `console.warn` appears:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
[ampless:plausible] consentCategory is set but window.amplessConsent never installed.
|
|
81
|
+
Did you forget to register @ampless/plugin-cookie-consent?
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This warning fires in production too — it is intended to help operators catch misconfiguration quickly. There is no mechanism to suppress it.
|
|
85
|
+
|
|
86
|
+
**Plugin ordering:** register `@ampless/plugin-cookie-consent` before the Plausible plugin in the `plugins` array.
|
|
87
|
+
|
|
88
|
+
For full details on the Consent Convention and the `window.amplessConsent` API see [docs/architecture/08-plugin-architecture.md](https://github.com/heavymoons/ampless/blob/main/docs/architecture/08-plugin-architecture.md).
|
|
46
89
|
|
|
47
90
|
## Editing settings from the admin UI
|
|
48
91
|
|
package/dist/index.d.ts
CHANGED
|
@@ -33,6 +33,13 @@ interface PlausibleOptions {
|
|
|
33
33
|
* deployment).
|
|
34
34
|
*/
|
|
35
35
|
instanceId?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Optional consent category. When set, the analytics loader does
|
|
38
|
+
* not run until window.amplessConsent.has(consentCategory) returns
|
|
39
|
+
* true. See @ampless/plugin-cookie-consent and the Consent
|
|
40
|
+
* Convention in docs/architecture/08-plugin-architecture.md.
|
|
41
|
+
*/
|
|
42
|
+
consentCategory?: string;
|
|
36
43
|
}
|
|
37
44
|
/**
|
|
38
45
|
* Factory for the Plausible plugin. Emits a single `<script>`
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,8 @@ function plausiblePlugin(options = {}) {
|
|
|
5
5
|
const {
|
|
6
6
|
domain = "",
|
|
7
7
|
scriptUrl = DEFAULT_SCRIPT_URL,
|
|
8
|
-
instanceId = "plausible"
|
|
8
|
+
instanceId = "plausible",
|
|
9
|
+
consentCategory = ""
|
|
9
10
|
} = options;
|
|
10
11
|
return definePlugin({
|
|
11
12
|
name: "plausible",
|
|
@@ -56,6 +57,19 @@ function plausiblePlugin(options = {}) {
|
|
|
56
57
|
allowRelative: false,
|
|
57
58
|
placeholder: DEFAULT_SCRIPT_URL,
|
|
58
59
|
default: scriptUrl
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
type: "text",
|
|
63
|
+
key: "consentCategory",
|
|
64
|
+
label: { en: "Consent category", ja: "\u540C\u610F\u30AB\u30C6\u30B4\u30EA" },
|
|
65
|
+
description: {
|
|
66
|
+
en: "Optional. When set, the analytics loader fires only after `window.amplessConsent.has(<this>)` returns true. Requires `@ampless/plugin-cookie-consent` to also be registered \u2014 fail-closed otherwise (no tracking, console warning after 5s). See the Consent Convention in the plugin author guide.",
|
|
67
|
+
ja: "\u30AA\u30D7\u30B7\u30E7\u30F3\u3002\u8A2D\u5B9A\u3059\u308B\u3068 `window.amplessConsent.has(<value>)` \u304C true \u306B\u306A\u308B\u307E\u3067 analytics loader \u3092\u767A\u706B\u3057\u307E\u305B\u3093\u3002`@ampless/plugin-cookie-consent` \u306E\u4F75\u7528\u304C\u5FC5\u9808 \u2014 \u672A\u5C0E\u5165\u6642\u306F\u5B8C\u5168\u306B\u767A\u706B\u305B\u305A 5 \u79D2\u5F8C\u306B console warning (fail-closed)\u3002\u8A73\u7D30\u306F plugin author guide \u306E Consent Convention \u7BC0\u3092\u53C2\u7167\u3002"
|
|
68
|
+
},
|
|
69
|
+
pattern: "^$|^[a-z][a-z0-9_-]*$",
|
|
70
|
+
maxLength: 32,
|
|
71
|
+
placeholder: "analytics",
|
|
72
|
+
default: consentCategory
|
|
59
73
|
}
|
|
60
74
|
]
|
|
61
75
|
},
|
|
@@ -63,6 +77,44 @@ function plausiblePlugin(options = {}) {
|
|
|
63
77
|
const resolvedDomain = (ctx.setting("domain") ?? "").trim();
|
|
64
78
|
const resolvedScriptUrl = (ctx.setting("scriptUrl") ?? "").trim();
|
|
65
79
|
if (!resolvedDomain || !resolvedScriptUrl) return [];
|
|
80
|
+
const category = (ctx.setting("consentCategory") ?? "").trim();
|
|
81
|
+
if (category) {
|
|
82
|
+
return [
|
|
83
|
+
{
|
|
84
|
+
type: "inlineScript",
|
|
85
|
+
id: `plausible-gated-${instanceId}`,
|
|
86
|
+
strategy: "afterInteractive",
|
|
87
|
+
body: [
|
|
88
|
+
"(function () {",
|
|
89
|
+
" var initialized = false",
|
|
90
|
+
" function init() {",
|
|
91
|
+
" if (initialized) return",
|
|
92
|
+
" initialized = true",
|
|
93
|
+
" var s = document.createElement('script')",
|
|
94
|
+
` s.src = ${JSON.stringify(resolvedScriptUrl)}`,
|
|
95
|
+
" s.defer = true",
|
|
96
|
+
` s.setAttribute('data-domain', ${JSON.stringify(resolvedDomain)})`,
|
|
97
|
+
" document.head.appendChild(s)",
|
|
98
|
+
" }",
|
|
99
|
+
" function wait() {",
|
|
100
|
+
` if (window.amplessConsent.has(${JSON.stringify(category)})) init()`,
|
|
101
|
+
` else window.amplessConsent.on(${JSON.stringify(category)}, init)`,
|
|
102
|
+
" }",
|
|
103
|
+
" if (window.amplessConsent) {",
|
|
104
|
+
" wait()",
|
|
105
|
+
" } else {",
|
|
106
|
+
" window.addEventListener('ampless:consent-ready', wait, { once: true })",
|
|
107
|
+
" setTimeout(function () {",
|
|
108
|
+
" if (!window.amplessConsent) {",
|
|
109
|
+
" console.warn('[ampless:plausible] consentCategory is set but window.amplessConsent never installed. Did you forget to register @ampless/plugin-cookie-consent?')",
|
|
110
|
+
" }",
|
|
111
|
+
" }, 5000)",
|
|
112
|
+
" }",
|
|
113
|
+
"})()"
|
|
114
|
+
].join("\n")
|
|
115
|
+
}
|
|
116
|
+
];
|
|
117
|
+
}
|
|
66
118
|
return [
|
|
67
119
|
{
|
|
68
120
|
type: "script",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ampless/plugin-plausible",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-alpha.11",
|
|
4
4
|
"description": "Plausible Analytics plugin for ampless",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"homepage": "https://github.com/heavymoons/ampless/tree/main/packages/plugin-plausible#readme",
|
|
27
27
|
"bugs": "https://github.com/heavymoons/ampless/issues",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"ampless": "1.0.0-alpha.
|
|
29
|
+
"ampless": "1.0.0-alpha.28"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
32
|
"ampless",
|