@tansy/analytics-matomo 0.0.0-stage → 0.1.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/index.d.ts +10 -0
- package/index.js +85 -0
- package/package.json +26 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
What changed in each release of `@tansy/analytics-matomo`. Releases up to 0.1.0 went out with every other package
|
|
4
|
+
on one version and are described in [tansy's changelog](../../CHANGELOG.md).
|
|
5
|
+
|
|
6
|
+
## 0.1.1 — 2026-11-22
|
|
7
|
+
|
|
8
|
+
No changes of its own. Released beside `tansy` 0.1.1, from which every package moves on its own
|
|
9
|
+
version, and it now requires it: the `tansy` peer range is ^0.1.1.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mohamed Salah
|
|
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
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @tansy/analytics-matomo
|
|
4
|
+
|
|
5
|
+
Matomo for @tansy/analytics: the tracker on every page, and its numbers on the dashboard.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tansy/analytics-matomo
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Add it to `tansy.config.ts`. See [the analytics plugin documentation](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/plugins/analytics.md) for what it adds and the options it takes.
|
|
12
|
+
|
|
13
|
+
A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface MatomoOptions {
|
|
2
|
+
/** Your Matomo's address, self-hosted or in Matomo Cloud, such as https://example.matomo.cloud. */
|
|
3
|
+
url: string;
|
|
4
|
+
/** The site's id in Matomo, until one is saved in the settings. */
|
|
5
|
+
siteId?: string;
|
|
6
|
+
/** For tests: called instead of fetch. */
|
|
7
|
+
fetcher?: typeof fetch;
|
|
8
|
+
}
|
|
9
|
+
/** Matomo: open source, self-hosted or in its cloud, and without cookies if you choose. */
|
|
10
|
+
export declare const matomo: ({ url, siteId: defaultId, fetcher }: MatomoOptions) => import("@tansy/analytics/provider").AnalyticsProvider;
|
package/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { baseOf, breakdown, defineAnalyticsProvider, fillDays, num, requestJson, str } from "@tansy/analytics/provider";
|
|
2
|
+
const siteId = (settings) => str(settings.matomoSiteId);
|
|
3
|
+
const token = (settings) => str(settings.matomoToken);
|
|
4
|
+
/** Matomo: open source, self-hosted or in its cloud, and without cookies if you choose. */
|
|
5
|
+
export const matomo = ({ url, siteId: defaultId = "", fetcher }) => {
|
|
6
|
+
const base = baseOf(url, "Matomo's url");
|
|
7
|
+
const origin = new URL(base).origin;
|
|
8
|
+
/** One Reporting API call. The token goes in the body, never the address, so it stays out of logs. */
|
|
9
|
+
async function call(settings, method, range, period, extra = {}) {
|
|
10
|
+
const answer = await requestJson(`${base}/index.php`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
13
|
+
body: new URLSearchParams({ module: "API", format: "JSON", method, idSite: siteId(settings), period, date: `${range.from},${range.to}`, token_auth: token(settings), ...extra }),
|
|
14
|
+
}, fetcher);
|
|
15
|
+
if (answer && typeof answer === "object" && answer.result === "error")
|
|
16
|
+
throw new Error(`Matomo refused ${method}.`);
|
|
17
|
+
return answer;
|
|
18
|
+
}
|
|
19
|
+
const table = async (settings, range, method, count = "nb_visits", name = (row) => row.label) => {
|
|
20
|
+
const rows = await call(settings, method, range, "range", { filter_limit: "10", flat: "1", filter_sort_column: count, filter_sort_order: "desc" });
|
|
21
|
+
return breakdown((Array.isArray(rows) ? rows : []).map((row) => ({ name: name(row), count: row[count] })));
|
|
22
|
+
};
|
|
23
|
+
return defineAnalyticsProvider({
|
|
24
|
+
id: "matomo",
|
|
25
|
+
label: "Matomo",
|
|
26
|
+
messages: {
|
|
27
|
+
ar: {
|
|
28
|
+
settings: {
|
|
29
|
+
matomoSiteId: { label: "معرّف الموقع في Matomo", description: "رقم من Matomo ← المشرف ← المواقع ← الإدارة، مثل 1." },
|
|
30
|
+
matomoCookieless: { label: "دون ملفات تعريف الارتباط", description: "لا يضع Matomo ملفات تعريف ارتباط، فلا حاجة إلى موافقة الزوار في أغلب الحالات. يُعدّ الزائر العائد زائرًا جديدًا أحيانًا." },
|
|
31
|
+
matomoToken: { label: "رمز المصادقة في Matomo", description: "اختياري، لعرض الأرقام هنا. من Matomo ← الإعدادات الشخصية ← الأمان ← رموز المصادقة، لمستخدم له صلاحية العرض." },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
settings: () => [
|
|
36
|
+
{ name: "matomoSiteId", type: "text", default: defaultId, maxLength: 10, label: "Matomo site id", description: "A number, from Matomo → Administration → Websites → Manage, such as 1." },
|
|
37
|
+
{ name: "matomoCookieless", type: "boolean", default: true, label: "Without cookies", description: "Matomo sets no cookies, so most places need no consent from visitors. A returning visitor sometimes counts as new." },
|
|
38
|
+
{ name: "matomoToken", type: "secret", env: "MATOMO_TOKEN", label: "Matomo auth token", description: "Optional, to show the numbers here. From Matomo → Personal → Security → Auth tokens, for a user who may view the site." },
|
|
39
|
+
],
|
|
40
|
+
ready: (settings) => /^\d{1,10}$/.test(siteId(settings)),
|
|
41
|
+
reportReady: (settings) => Boolean(token(settings)),
|
|
42
|
+
tracker: (settings) => ({
|
|
43
|
+
script: `${base}/matomo.js`,
|
|
44
|
+
stubs: [{ type: "array", name: "_paq" }],
|
|
45
|
+
setup: [
|
|
46
|
+
{ fn: "_paq.push", args: [["setTrackerUrl", `${base}/matomo.php`]] },
|
|
47
|
+
{ fn: "_paq.push", args: [["setSiteId", siteId(settings)]] },
|
|
48
|
+
...(settings.matomoCookieless !== false ? [{ fn: "_paq.push", args: [["disableCookies"]] }] : []),
|
|
49
|
+
{ fn: "_paq.push", args: [["enableLinkTracking"]] },
|
|
50
|
+
],
|
|
51
|
+
pageview: [
|
|
52
|
+
{ fn: "_paq.push", args: [["setReferrerUrl", "$referrer"]] },
|
|
53
|
+
{ fn: "_paq.push", args: [["setCustomUrl", "$url"]] },
|
|
54
|
+
{ fn: "_paq.push", args: [["setDocumentTitle", "$title"]] },
|
|
55
|
+
{ fn: "_paq.push", args: [["trackPageView"]] },
|
|
56
|
+
],
|
|
57
|
+
}),
|
|
58
|
+
async report(range, settings) {
|
|
59
|
+
const [visits, actions, pages, referrers, countries, devices, browsers] = await Promise.all([
|
|
60
|
+
call(settings, "VisitsSummary.get", range, "day"),
|
|
61
|
+
call(settings, "Actions.get", range, "day"),
|
|
62
|
+
table(settings, range, "Actions.getPageUrls", "nb_hits"),
|
|
63
|
+
table(settings, range, "Referrers.getAll"),
|
|
64
|
+
table(settings, range, "UserCountry.getCountry", "nb_visits", (row) => String(row.code ?? row.label ?? "").toUpperCase()),
|
|
65
|
+
table(settings, range, "DevicesDetection.getType"),
|
|
66
|
+
table(settings, range, "DevicesDetection.getBrowsers"),
|
|
67
|
+
]);
|
|
68
|
+
// A day without visits is an empty list rather than an object.
|
|
69
|
+
const field = (day, name) => (day && !Array.isArray(day) ? num(day[name]) : 0);
|
|
70
|
+
const days = new Map(Object.keys({ ...visits, ...actions }).map((date) => [date, { views: field(actions[date], "nb_pageviews"), visitors: field(visits[date], "nb_uniq_visitors") || field(visits[date], "nb_visits") }]));
|
|
71
|
+
const series = fillDays(range, days);
|
|
72
|
+
return {
|
|
73
|
+
totals: { views: series.reduce((sum, day) => sum + day.views, 0), visitors: series.reduce((sum, day) => sum + day.visitors, 0) },
|
|
74
|
+
days: series,
|
|
75
|
+
pages,
|
|
76
|
+
referrers,
|
|
77
|
+
countries,
|
|
78
|
+
devices,
|
|
79
|
+
browsers,
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
dashboardUrl: (settings) => `${base}/index.php?module=CoreHome&action=index&idSite=${encodeURIComponent(siteId(settings))}&period=day&date=today`,
|
|
83
|
+
csp: { connect: [origin], img: [origin] },
|
|
84
|
+
});
|
|
85
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/analytics-matomo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Matomo for @tansy/analytics: the tracker on every page, and its numbers on the dashboard.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tansy",
|
|
7
|
+
"tansy-analytics-provider"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
|
|
11
|
+
"homepage": "https://github.com/mhmdsalahsebai/tansy",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/mhmdsalahsebai/tansy.git",
|
|
15
|
+
"directory": "plugins/analytics-matomo"
|
|
16
|
+
},
|
|
17
|
+
"bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./index.d.ts",
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@tansy/analytics": "^0.1.1",
|
|
26
|
+
"tansy": "^0.1.1"
|
|
27
|
+
}
|
|
28
|
+
}
|