@kntnt/engagement-metrics-matomo 1.0.0
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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/index.js +60 -0
- package/dist/kntnt-engagement-metrics-matomo.min.js +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kntnt Sweden AB
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @kntnt/engagement-metrics-matomo
|
|
2
|
+
|
|
3
|
+
Matomo Analytics add-on for [`@kntnt/engagement-metrics`](https://www.npmjs.com/package/@kntnt/engagement-metrics). Sends reading and scanning progress events to Matomo at configurable thresholds.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @kntnt/engagement-metrics @kntnt/engagement-metrics-matomo
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### ESM (bundler)
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { createMeasurer } from '@kntnt/engagement-metrics'
|
|
17
|
+
import { registerMatomo } from '@kntnt/engagement-metrics-matomo'
|
|
18
|
+
|
|
19
|
+
const measurer = createMeasurer({ selector: 'article p' })
|
|
20
|
+
registerMatomo(measurer, {
|
|
21
|
+
readingThresholds: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
|
|
22
|
+
scanningThresholds: [25, 50, 75, 100],
|
|
23
|
+
})
|
|
24
|
+
measurer.start()
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### IIFE (script tag)
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script src="https://unpkg.com/@kntnt/engagement-metrics/dist/kntnt-engagement-metrics.min.js"></script>
|
|
31
|
+
<script src="https://unpkg.com/@kntnt/engagement-metrics-matomo/dist/kntnt-engagement-metrics-matomo.min.js"></script>
|
|
32
|
+
<script>
|
|
33
|
+
KntntEngagementMetrics.measurer = KntntEngagementMetrics.start({ selector: 'article p' })
|
|
34
|
+
KntntEngagementMetrics.matomo.register()
|
|
35
|
+
</script>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Documentation
|
|
39
|
+
|
|
40
|
+
See the [main repository](https://github.com/Kntnt/kntnt-engagement-metrics) for full documentation, including how to set up Matomo custom dimensions and view engagement data in your Matomo dashboard.
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
[MIT](https://github.com/Kntnt/kntnt-engagement-metrics/blob/main/LICENSE) — Copyright (c) 2026 Kntnt Sweden AB
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/listener.ts
|
|
2
|
+
class MatomoListener {
|
|
3
|
+
#config;
|
|
4
|
+
#reportedReadingThresholds = new Set;
|
|
5
|
+
#reportedScanningThresholds = new Set;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.#config = config;
|
|
8
|
+
}
|
|
9
|
+
update(metrics) {
|
|
10
|
+
this.#checkReadingThresholds(metrics);
|
|
11
|
+
this.#checkScanningThresholds(metrics);
|
|
12
|
+
}
|
|
13
|
+
#checkReadingThresholds(metrics) {
|
|
14
|
+
const readingPct = metrics.readingRatio * 100;
|
|
15
|
+
for (const threshold of this.#config.readingThresholds) {
|
|
16
|
+
if (readingPct >= threshold && !this.#reportedReadingThresholds.has(threshold)) {
|
|
17
|
+
this.#reportedReadingThresholds.add(threshold);
|
|
18
|
+
this.#trackEvent("Engagement", "Reading", `${threshold}%`, Math.round(metrics.readingTime));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
#checkScanningThresholds(metrics) {
|
|
23
|
+
const scanningPct = metrics.scanningRatio * 100;
|
|
24
|
+
for (const threshold of this.#config.scanningThresholds) {
|
|
25
|
+
if (scanningPct >= threshold && !this.#reportedScanningThresholds.has(threshold)) {
|
|
26
|
+
this.#reportedScanningThresholds.add(threshold);
|
|
27
|
+
this.#trackEvent("Engagement", "Scanning", `${threshold}%`, Math.round(metrics.scanningDepth));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
#trackEvent(category, action, name, value) {
|
|
32
|
+
try {
|
|
33
|
+
const _paq = window._paq;
|
|
34
|
+
if (_paq) {
|
|
35
|
+
_paq.push(["trackEvent", category, action, name, value]);
|
|
36
|
+
} else {
|
|
37
|
+
console.warn("[kntnt-engagement-metrics-matomo] Matomo tracker (_paq) not found");
|
|
38
|
+
}
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.warn("[kntnt-engagement-metrics-matomo] Failed to send event:", e);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/index.ts
|
|
46
|
+
var DEFAULTS = {
|
|
47
|
+
bounceThreshold: 0.1,
|
|
48
|
+
readingThresholds: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
|
|
49
|
+
scanningThresholds: [25, 50, 75, 100],
|
|
50
|
+
trackerMode: "auto"
|
|
51
|
+
};
|
|
52
|
+
function registerMatomo(measurer, config) {
|
|
53
|
+
const mergedConfig = { ...DEFAULTS, ...config };
|
|
54
|
+
const listener = new MatomoListener(mergedConfig);
|
|
55
|
+
measurer.addListener(listener);
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
registerMatomo,
|
|
59
|
+
MatomoListener
|
|
60
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class y{#w;#n=new Set;#C=new Set;constructor(w){this.#w=w}update(w){this.#y(w),this.#I(w)}#y(w){let C=w.readingRatio*100;for(let n of this.#w.readingThresholds)if(C>=n&&!this.#n.has(n))this.#n.add(n),this.#k("Engagement","Reading",`${n}%`,Math.round(w.readingTime))}#I(w){let C=w.scanningRatio*100;for(let n of this.#w.scanningThresholds)if(C>=n&&!this.#C.has(n))this.#C.add(n),this.#k("Engagement","Scanning",`${n}%`,Math.round(w.scanningDepth))}#k(w,C,n,K){try{let k=window._paq;if(k)k.push(["trackEvent",w,C,n,K]);else console.warn("[kntnt-engagement-metrics-matomo] Matomo tracker (_paq) not found")}catch(k){console.warn("[kntnt-engagement-metrics-matomo] Failed to send event:",k)}}}var E={bounceThreshold:0.1,readingThresholds:[10,20,30,40,50,60,70,80,90,100],scanningThresholds:[25,50,75,100],trackerMode:"auto"};function b(w,C){let n={...E,...C},K=new y(n);w.addListener(K)}var I=window.KntntEngagementMetrics;if(I)if(I.matomo)console.warn("[kntnt-engagement-metrics-matomo] Already loaded, skipping");else{let w=window.kntntEngagementMetricsMatomoConfig??{};I.matomo={register:(C)=>b(I.measurer,C??w)}}else console.warn("[kntnt-engagement-metrics-matomo] Core library not loaded");
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kntnt/engagement-metrics-matomo",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Matomo Analytics add-on for @kntnt/engagement-metrics.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Thomas Barregren",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/Kntnt/kntnt-engagement-metrics.git",
|
|
10
|
+
"directory": "packages/matomo"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./iife": {
|
|
22
|
+
"default": "./dist/kntnt-engagement-metrics-matomo.min.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "bun build src/index.ts --outdir dist --target browser --format esm && bun build src/iife.ts --outfile dist/kntnt-engagement-metrics-matomo.min.js --target browser --minify",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"lint": "biome check src/",
|
|
34
|
+
"test": "bun test"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@kntnt/engagement-metrics": ">=1.0.0"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"engagement",
|
|
41
|
+
"metrics",
|
|
42
|
+
"matomo",
|
|
43
|
+
"analytics"
|
|
44
|
+
]
|
|
45
|
+
}
|