keanu-weblite/src/services/analytics.service.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

import cleaninsights from "./cleaninsights.service";
import matomo from "./matomo.service";
export default {
install(app) {
class AnalyticsServiceClass {
constructor() {
this.engines = [];
this.cachedEvents = [];
this.initialized = false;
app.$config.promise.then((config) => {
var analytics = config.analytics || {};
if (!Array.isArray(analytics)) {
analytics = [analytics];
}
for (const engineConfig of analytics) {
if (engineConfig.enabled) {
let type = engineConfig.type || "ci";
switch (type) {
case "ci":
{
2025-05-12 12:39:04 +02:00
let engine = cleaninsights.install(app, engineConfig.config);
this.engines.push(engine);
}
break;
case "matomo":
{
2025-05-12 12:39:04 +02:00
let engine = matomo.install(app, engineConfig.config);
this.engines.push(engine);
}
break;
}
}
}
this.initialized = true;
// Handle cached events
this.cachedEvents.forEach(([category, action]) => {
this.event(category, action);
});
this.cachedEvents = [];
});
}
event(category, action) {
if (!this.initialized) {
this.cachedEvents.push([category, action]);
return;
}
2025-05-06 09:27:53 +02:00
// Send the event to each configured analytics engine.
this.engines.forEach((engine) => {
engine.event(category, action);
});
}
}
const analyticsService = new AnalyticsServiceClass();
app.$analytics = analyticsService;
app.config.globalProperties.$analytics = analyticsService;
},
};