@igo2/core
Version:
95 lines (91 loc) • 3.65 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import * as i1 from '@igo2/core/config';
class AnalyticsService {
config;
options;
get paq() {
return (window._paq = window._paq || []);
}
constructor(config) {
this.config = config;
this.options = this.config.getConfig('analytics') || {};
if (this.options.provider === 'matomo') {
this.initMatomo();
}
}
initMatomo() {
if (!this.options.url || !this.options.id) {
return;
}
const url = this.options.url.substr(-1) === '/'
? this.options.url + 'matomo'
: this.options.url;
// this.paq.push(['trackPageView']);
// this.paq.push(['enableLinkTracking']);
(() => {
this.paq.push(['setTrackerUrl', url + '.php']);
this.paq.push(['setSiteId', this.options.id]);
this.paq.push(['setSecureCookie', location.protocol === 'https:']);
const g = document.createElement('script');
const s = document.getElementsByTagName('script')[0];
g.type = 'text/javascript';
g.async = true;
g.defer = true;
g.src = url + '.js';
s.parentNode.insertBefore(g, s);
})();
}
/**
* Pass `null` to unset the user.
*/
setUser(user, profils) {
if (this.options.provider === 'matomo') {
if (!user) {
this.paq.push(['resetUserId']);
this.paq.push(['deleteCustomVariable', 1, 'user']);
this.paq.push(['deleteCustomVariable', 2, 'name']);
this.paq.push(['deleteCustomVariable', 3, 'profils']);
}
else {
this.paq.push(['setUserId', user.id]);
const name = `${user.firstName} ${user.lastName}`;
this.paq.push(['setCustomVariable', 1, 'user', user.sourceId, 'visit']);
this.paq.push(['setCustomVariable', 2, 'name', name, 'visit']);
this.paq.push(['setCustomVariable', 3, 'profils', profils, 'visit']);
}
this.paq.push(['trackPageView']);
this.paq.push(['enableLinkTracking']);
}
}
trackSearch(term, nbResults) {
if (this.options.provider === 'matomo') {
this.paq.push(['trackSiteSearch', term, false, nbResults]);
}
}
trackEvent(category, action, name) {
if (this.options.provider === 'matomo') {
this.paq.push(['trackEvent', category, action, name]);
}
}
/**
* Function that tracks layers added to the map
*/
trackLayer(category, action, parameters) {
if (this.options.provider === 'matomo')
this.paq.push(['trackEvent', category, action, parameters]);
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: AnalyticsService, deps: [{ token: i1.ConfigService }], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: AnalyticsService, providedIn: 'root' });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.15", ngImport: i0, type: AnalyticsService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: () => [{ type: i1.ConfigService }] });
/**
* Generated bundle index. Do not edit.
*/
export { AnalyticsService };
//# sourceMappingURL=igo2-core-analytics.mjs.map