-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatomo.ts
More file actions
69 lines (65 loc) · 1.72 KB
/
Copy pathmatomo.ts
File metadata and controls
69 lines (65 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Matomo Tag Manager analytics tracking utilities
*/
declare global {
interface Window {
_mtm: Array<Record<string, unknown>>;
}
}
/**
* Track a custom event in Matomo Tag Manager
* @param category - The event category (e.g., "Game", "UI", "Navigation")
* @param action - The event action (e.g., "country_selected", "guess_submitted")
* @param name - Optional event name for additional context
* @param value - Optional numeric value associated with the event
*/
export function trackEvent(
category: string,
action: string,
name?: string,
value?: number
): void {
if (typeof window !== "undefined" && window._mtm) {
const eventData: Record<string, unknown> = {
event: "trackEvent",
eventCategory: category,
eventAction: action,
};
if (name !== undefined) {
eventData.eventName = name;
}
if (value !== undefined) {
eventData.eventValue = value;
}
window._mtm.push(eventData);
}
}
/**
* Track a page view in Matomo Tag Manager
* @param customTitle - Optional custom page title
*/
export function trackPageView(customTitle?: string): void {
if (typeof window !== "undefined" && window._mtm) {
const eventData: Record<string, unknown> = {
event: "trackPageView",
};
if (customTitle) {
eventData.documentTitle = customTitle;
}
window._mtm.push(eventData);
}
}
/**
* Set a custom dimension in Matomo Tag Manager
* @param id - The dimension ID
* @param value - The dimension value
*/
export function setCustomDimension(id: number, value: string): void {
if (typeof window !== "undefined" && window._mtm) {
window._mtm.push({
event: "setCustomDimension",
dimensionId: id,
dimensionValue: value,
});
}
}