@kntnt/engagement-metrics-gtag 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 +69 -0
- package/dist/kntnt-engagement-metrics-gtag.min.js +1 -0
- package/package.json +46 -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-gtag
|
|
2
|
+
|
|
3
|
+
Google Analytics 4 (gtag.js) add-on for [`@kntnt/engagement-metrics`](https://www.npmjs.com/package/@kntnt/engagement-metrics). Sends reading and scanning progress events to GA4 at configurable thresholds.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @kntnt/engagement-metrics @kntnt/engagement-metrics-gtag
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### ESM (bundler)
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { createMeasurer } from '@kntnt/engagement-metrics'
|
|
17
|
+
import { registerGtag } from '@kntnt/engagement-metrics-gtag'
|
|
18
|
+
|
|
19
|
+
const measurer = createMeasurer({ selector: 'article p' })
|
|
20
|
+
registerGtag(measurer, {
|
|
21
|
+
readingThresholds: [10, 25, 50, 75, 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-gtag/dist/kntnt-engagement-metrics-gtag.min.js"></script>
|
|
32
|
+
<script>
|
|
33
|
+
KntntEngagementMetrics.measurer = KntntEngagementMetrics.start({ selector: 'article p' })
|
|
34
|
+
KntntEngagementMetrics.gtag.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 register custom dimensions in GA4 and view engagement data in your reports.
|
|
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,69 @@
|
|
|
1
|
+
// src/listener.ts
|
|
2
|
+
class GtagListener {
|
|
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.#sendEvent(this.#config.readingEventName, {
|
|
19
|
+
percentage: threshold,
|
|
20
|
+
reading_time: Math.round(metrics.readingTime),
|
|
21
|
+
reading_ratio: Math.round(metrics.readingRatio * 100)
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
#checkScanningThresholds(metrics) {
|
|
27
|
+
const scanningPct = metrics.scanningRatio * 100;
|
|
28
|
+
for (const threshold of this.#config.scanningThresholds) {
|
|
29
|
+
if (scanningPct >= threshold && !this.#reportedScanningThresholds.has(threshold)) {
|
|
30
|
+
this.#reportedScanningThresholds.add(threshold);
|
|
31
|
+
this.#sendEvent(this.#config.scanningEventName, {
|
|
32
|
+
percentage: threshold,
|
|
33
|
+
scanning_depth: Math.round(metrics.scanningDepth),
|
|
34
|
+
scanning_ratio: Math.round(metrics.scanningRatio * 100)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
#sendEvent(eventName, params) {
|
|
40
|
+
try {
|
|
41
|
+
const gtag = window.gtag;
|
|
42
|
+
if (typeof gtag === "function") {
|
|
43
|
+
gtag("event", eventName, params);
|
|
44
|
+
} else {
|
|
45
|
+
console.warn("[kntnt-engagement-metrics-gtag] gtag function not found");
|
|
46
|
+
}
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.warn("[kntnt-engagement-metrics-gtag] Failed to send event:", e);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/index.ts
|
|
54
|
+
var DEFAULTS = {
|
|
55
|
+
readingEventName: "engagement_reading",
|
|
56
|
+
scanningEventName: "engagement_scanning",
|
|
57
|
+
readingThresholds: [10, 25, 50, 75, 90, 100],
|
|
58
|
+
scanningThresholds: [25, 50, 75, 100],
|
|
59
|
+
trackerMode: "auto"
|
|
60
|
+
};
|
|
61
|
+
function registerGtag(measurer, config) {
|
|
62
|
+
const mergedConfig = { ...DEFAULTS, ...config };
|
|
63
|
+
const listener = new GtagListener(mergedConfig);
|
|
64
|
+
measurer.addListener(listener);
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
registerGtag,
|
|
68
|
+
GtagListener
|
|
69
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class C{#w;#k=new Set;#y=new Set;constructor(w){this.#w=w}update(w){this.#I(w),this.#K(w)}#I(w){let y=w.readingRatio*100;for(let k of this.#w.readingThresholds)if(y>=k&&!this.#k.has(k))this.#k.add(k),this.#C(this.#w.readingEventName,{percentage:k,reading_time:Math.round(w.readingTime),reading_ratio:Math.round(w.readingRatio*100)})}#K(w){let y=w.scanningRatio*100;for(let k of this.#w.scanningThresholds)if(y>=k&&!this.#y.has(k))this.#y.add(k),this.#C(this.#w.scanningEventName,{percentage:k,scanning_depth:Math.round(w.scanningDepth),scanning_ratio:Math.round(w.scanningRatio*100)})}#C(w,y){try{let k=window.gtag;if(typeof k==="function")k("event",w,y);else console.warn("[kntnt-engagement-metrics-gtag] gtag function not found")}catch(k){console.warn("[kntnt-engagement-metrics-gtag] Failed to send event:",k)}}}var E={readingEventName:"engagement_reading",scanningEventName:"engagement_scanning",readingThresholds:[10,25,50,75,90,100],scanningThresholds:[25,50,75,100],trackerMode:"auto"};function K(w,y){let k={...E,...y},b=new C(k);w.addListener(b)}var I=window.KntntEngagementMetrics;if(I)if(I.gtag)console.warn("[kntnt-engagement-metrics-gtag] Already loaded, skipping");else{let w=window.kntntEngagementMetricsGtagConfig??{};I.gtag={register:(y)=>K(I.measurer,y??w)}}else console.warn("[kntnt-engagement-metrics-gtag] Core library not loaded");
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kntnt/engagement-metrics-gtag",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Google Analytics 4 (gtag.js) 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/gtag"
|
|
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-gtag.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-gtag.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
|
+
"google-analytics",
|
|
43
|
+
"gtag",
|
|
44
|
+
"ga4"
|
|
45
|
+
]
|
|
46
|
+
}
|