@eox/pages-theme-esa 0.8.1 → 0.8.2
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/package.json +1 -1
- package/src/Layout.vue +24 -7
- package/src/helpers.js +131 -0
- package/src/helpers.test.js +57 -0
package/package.json
CHANGED
package/src/Layout.vue
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
import { onMounted, watch } from "vue";
|
|
3
3
|
import { useData, useRouter, withBase } from "vitepress";
|
|
4
4
|
import DefaultTheme from "vitepress/theme";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
applyShadowDomEventListener,
|
|
7
|
+
normalizeTrackingUrl,
|
|
8
|
+
trackEvent,
|
|
9
|
+
trackPageView,
|
|
10
|
+
} from "./helpers";
|
|
6
11
|
|
|
7
12
|
const { Layout } = DefaultTheme;
|
|
8
13
|
const { site, theme, frontmatter } = useData();
|
|
@@ -43,10 +48,19 @@ onMounted(async () => {
|
|
|
43
48
|
100: false,
|
|
44
49
|
};
|
|
45
50
|
document.addEventListener("scroll", (e) => {
|
|
51
|
+
const currentPath = normalizeTrackingUrl(router.route.path);
|
|
46
52
|
Object.keys(scrollTargets).forEach((target) => {
|
|
47
|
-
if (
|
|
53
|
+
if (
|
|
54
|
+
getScrollPercent() >= parseInt(target, 10) &&
|
|
55
|
+
!scrollTargets[target]
|
|
56
|
+
) {
|
|
48
57
|
scrollTargets[target] = true;
|
|
49
|
-
|
|
58
|
+
trackEvent([
|
|
59
|
+
"Interaction",
|
|
60
|
+
"Scroll Depth",
|
|
61
|
+
currentPath,
|
|
62
|
+
parseInt(target, 10),
|
|
63
|
+
]);
|
|
50
64
|
}
|
|
51
65
|
});
|
|
52
66
|
});
|
|
@@ -56,8 +70,11 @@ onMounted(async () => {
|
|
|
56
70
|
const routerEventHandler = (event, anchor) => {
|
|
57
71
|
const href = anchor.getAttribute("href");
|
|
58
72
|
const target = anchor.getAttribute("target");
|
|
73
|
+
if (!href) return;
|
|
59
74
|
if (href.startsWith("http") || target === "_blank") {
|
|
60
75
|
window.open(href, target).focus();
|
|
76
|
+
} else if (href.startsWith("mailto:") || href.startsWith("tel:")) {
|
|
77
|
+
window.location.href = href;
|
|
61
78
|
} else {
|
|
62
79
|
router.go(!href.startsWith(site.value.base) ? withBase(href) : href);
|
|
63
80
|
}
|
|
@@ -67,10 +84,10 @@ onMounted(async () => {
|
|
|
67
84
|
() => router.route.data.relativePath,
|
|
68
85
|
() => {
|
|
69
86
|
if (!window._paq) return;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
trackPageView({
|
|
88
|
+
path: router.route.path,
|
|
89
|
+
title: router.route.data.title,
|
|
90
|
+
});
|
|
74
91
|
},
|
|
75
92
|
{ immediate: true },
|
|
76
93
|
);
|
package/src/helpers.js
CHANGED
|
@@ -30,3 +30,134 @@ export const applyShadowDomEventListener = (
|
|
|
30
30
|
});
|
|
31
31
|
});
|
|
32
32
|
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Normalizes a URL / path for tracking
|
|
36
|
+
*
|
|
37
|
+
* @param {string} inputUrl - URL or path
|
|
38
|
+
* @param {Object} [options] - Options
|
|
39
|
+
* @returns {string} Normalized URL / path
|
|
40
|
+
*/
|
|
41
|
+
export const normalizeTrackingUrl = (inputUrl, options = {}) => {
|
|
42
|
+
if (!inputUrl || typeof inputUrl !== "string") return inputUrl;
|
|
43
|
+
const { ignoredParams = ["x", "y", "z"], stripTrailingSlash = true } =
|
|
44
|
+
options;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const base =
|
|
48
|
+
typeof window !== "undefined" && window.location?.href
|
|
49
|
+
? window.location.href
|
|
50
|
+
: "https://eox.at";
|
|
51
|
+
const url = new URL(inputUrl, base);
|
|
52
|
+
let pathname = url.pathname;
|
|
53
|
+
|
|
54
|
+
if (stripTrailingSlash && pathname.length > 1 && pathname.endsWith("/")) {
|
|
55
|
+
pathname = pathname.replace(/\/+$/, "");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const searchParams = new URLSearchParams(url.search);
|
|
59
|
+
for (const key of ignoredParams) {
|
|
60
|
+
searchParams.delete(key);
|
|
61
|
+
}
|
|
62
|
+
searchParams.sort();
|
|
63
|
+
|
|
64
|
+
const search = searchParams.toString();
|
|
65
|
+
const isRelativeInput = !/^https?:\/\//i.test(inputUrl);
|
|
66
|
+
|
|
67
|
+
if (isRelativeInput) {
|
|
68
|
+
return pathname + (search ? `?${search}` : "");
|
|
69
|
+
}
|
|
70
|
+
url.pathname = pathname;
|
|
71
|
+
url.search = search ? `?${search}` : "";
|
|
72
|
+
return url.toString();
|
|
73
|
+
} catch {
|
|
74
|
+
if (stripTrailingSlash && inputUrl.length > 1 && inputUrl.endsWith("/")) {
|
|
75
|
+
return inputUrl.replace(/\/+$/, "");
|
|
76
|
+
}
|
|
77
|
+
return inputUrl;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Track Matomo event with normalized path parameters.
|
|
83
|
+
*
|
|
84
|
+
* @param {Array<string|number>|string} eventDetails - Array of details or Category string
|
|
85
|
+
* @param {...*} rest - Remaining event details
|
|
86
|
+
*/
|
|
87
|
+
export const trackEvent = (eventDetails, ...rest) => {
|
|
88
|
+
if (typeof window === "undefined") return;
|
|
89
|
+
const _paq = (window._paq = window._paq || []);
|
|
90
|
+
|
|
91
|
+
const details = Array.isArray(eventDetails)
|
|
92
|
+
? [...eventDetails]
|
|
93
|
+
: [eventDetails, ...rest];
|
|
94
|
+
|
|
95
|
+
if (typeof details[1] === "string" && details[1].startsWith("/")) {
|
|
96
|
+
details[1] = normalizeTrackingUrl(details[1]);
|
|
97
|
+
}
|
|
98
|
+
if (typeof details[2] === "string" && details[2].startsWith("/")) {
|
|
99
|
+
details[2] = normalizeTrackingUrl(details[2]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_paq.push(["trackEvent", ...details]);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Track page view with normalized URL and standardized document title.
|
|
107
|
+
*
|
|
108
|
+
* @param {Object|string} [options] - Router instance, options object, or path string
|
|
109
|
+
*/
|
|
110
|
+
export const trackPageView = (options = {}) => {
|
|
111
|
+
if (typeof window === "undefined") return;
|
|
112
|
+
const _paq = (window._paq = window._paq || []);
|
|
113
|
+
|
|
114
|
+
let rawPath = "";
|
|
115
|
+
let rawTitle = "";
|
|
116
|
+
|
|
117
|
+
if (typeof options === "string") {
|
|
118
|
+
rawPath = options;
|
|
119
|
+
} else if (options && typeof options === "object") {
|
|
120
|
+
if (options.route) {
|
|
121
|
+
rawPath = options.route.path;
|
|
122
|
+
rawTitle = options.route.data?.title;
|
|
123
|
+
} else {
|
|
124
|
+
rawPath =
|
|
125
|
+
options.path ||
|
|
126
|
+
options.customUrl ||
|
|
127
|
+
(typeof window !== "undefined"
|
|
128
|
+
? window.location.pathname + window.location.search
|
|
129
|
+
: "");
|
|
130
|
+
rawTitle =
|
|
131
|
+
options.title ||
|
|
132
|
+
options.customTitle ||
|
|
133
|
+
(typeof document !== "undefined" ? document.title : "");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const normalizedUrl = normalizeTrackingUrl(rawPath);
|
|
138
|
+
if (normalizedUrl) {
|
|
139
|
+
_paq.push(["setCustomUrl", normalizedUrl]);
|
|
140
|
+
}
|
|
141
|
+
if (rawTitle) {
|
|
142
|
+
_paq.push(["setDocumentTitle", rawTitle.trim()]);
|
|
143
|
+
}
|
|
144
|
+
_paq.push(["trackPageView"]);
|
|
145
|
+
_paq.push(["enableLinkTracking"]);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Track site search keyword query in Matomo.
|
|
150
|
+
*
|
|
151
|
+
* @param {string} keyword - Searched term
|
|
152
|
+
* @param {string|boolean} [category=false] - Search category
|
|
153
|
+
* @param {number} [resultsCount=0] - Result count
|
|
154
|
+
*/
|
|
155
|
+
export const trackSiteSearch = (
|
|
156
|
+
keyword,
|
|
157
|
+
category = false,
|
|
158
|
+
resultsCount = 0,
|
|
159
|
+
) => {
|
|
160
|
+
if (typeof window === "undefined") return;
|
|
161
|
+
const _paq = (window._paq = window._paq || []);
|
|
162
|
+
_paq.push(["trackSiteSearch", keyword, category, resultsCount]);
|
|
163
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { describe, it, beforeEach } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import {
|
|
4
|
+
normalizeTrackingUrl,
|
|
5
|
+
trackPageView,
|
|
6
|
+
trackEvent,
|
|
7
|
+
trackSiteSearch,
|
|
8
|
+
} from "./helpers.js";
|
|
9
|
+
|
|
10
|
+
describe("ESA helpers - normalizeTrackingUrl", () => {
|
|
11
|
+
it("strips trailing slashes from path", () => {
|
|
12
|
+
assert.equal(normalizeTrackingUrl("/explore/"), "/explore");
|
|
13
|
+
assert.equal(normalizeTrackingUrl("/explore"), "/explore");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("removes map coordinates x, y, z while keeping template", () => {
|
|
17
|
+
assert.equal(
|
|
18
|
+
normalizeTrackingUrl("/explore/?x=15&y=48&z=4&template=expert"),
|
|
19
|
+
"/explore?template=expert",
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("ESA helpers - trackEvent", () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
globalThis._paq = [];
|
|
27
|
+
globalThis.window = globalThis;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("pushes event and normalizes path in payload", () => {
|
|
31
|
+
trackEvent(["Interaction", "Scroll Depth", "/explore/", 25]);
|
|
32
|
+
assert.deepEqual(globalThis._paq, [
|
|
33
|
+
["trackEvent", "Interaction", "Scroll Depth", "/explore", 25],
|
|
34
|
+
]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("ESA helpers - trackPageView", () => {
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
globalThis._paq = [];
|
|
41
|
+
globalThis.window = globalThis;
|
|
42
|
+
globalThis.document = { title: "Test Title" };
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("tracks normalized pageview", () => {
|
|
46
|
+
trackPageView({
|
|
47
|
+
path: "/story/",
|
|
48
|
+
title: "Story Page",
|
|
49
|
+
});
|
|
50
|
+
assert.deepEqual(globalThis._paq, [
|
|
51
|
+
["setCustomUrl", "/story"],
|
|
52
|
+
["setDocumentTitle", "Story Page"],
|
|
53
|
+
["trackPageView"],
|
|
54
|
+
["enableLinkTracking"],
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
});
|