113 lines
2.7 KiB
JavaScript
113 lines
2.7 KiB
JavaScript
/**
|
|
*
|
|
* @author Nicolas Stein
|
|
* @version 1.0
|
|
* @category MyEic
|
|
* @subcategory Libraries
|
|
*/
|
|
class Matomo4Sparc {
|
|
|
|
/**
|
|
* @typedef Matomo4Sparc~options
|
|
* @property {string} trackUrl
|
|
* @property {string} idsite
|
|
* @property {string} method
|
|
* @property {object} baseLogObj
|
|
* @property {number} baseLogObj.rec
|
|
* @property {number} baseLogObj.apiv
|
|
* @property {number} baseLogObj.new_visit
|
|
*/
|
|
|
|
/** @type {Matomo4Sparc~options} */
|
|
options = {
|
|
trackUrl: '',
|
|
idsite:'1',
|
|
method: 'POST',
|
|
baseLogObj : {
|
|
rec:1,
|
|
apiv:1,
|
|
new_visit: 0,
|
|
},
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {Matomo4Sparc~options} options
|
|
*/
|
|
constructor(options) {
|
|
this.mergeOptions(this.options, options);
|
|
this.options.baseLogObj.idsite = this.options.idsite
|
|
//console.log('Matomo4Sparc started...')
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {Object} target
|
|
* @param {Object} source
|
|
*/
|
|
mergeOptions(target, source) {
|
|
for (let option in source) {
|
|
if(typeof source[option] === 'object' && !Array.isArray(source[option]) && (option in target) && !(target[option] instanceof(Element))) {
|
|
this.mergeOptions(target[option], source[option]);
|
|
} else {
|
|
target[option] = source[option];
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @typedef {Object} Matomo4Sparc~UserInfo
|
|
* @property {string} res
|
|
* @property {string} ua
|
|
* @property {string} uid
|
|
* @property {string} _var
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @returns {Matomo4Sparc~UserInfo}
|
|
|
|
*/
|
|
userInfos() {
|
|
return({
|
|
res: window.screen.availWidth+'x'+window.screen.availHeight,
|
|
ua: window.navigator.userAgent,
|
|
uid: app.User.identity.uuid,
|
|
_cvar: JSON.stringify({ userRoles: app.User.roles })
|
|
})
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {*} action
|
|
* @param {*} callOptions
|
|
*/
|
|
logAction(action, callOptions) {
|
|
let logObj = {
|
|
...this.options.baseLogObj,
|
|
...this.userInfos(),
|
|
...{
|
|
rand: crypto.randomUUID(),
|
|
action_name: action,
|
|
url: document.location.href,
|
|
},
|
|
...callOptions
|
|
}
|
|
|
|
let fd = new FormData()
|
|
for(let key in logObj) fd.set(key, logObj[key])
|
|
fetch(
|
|
this.options.trackUrl,{
|
|
method: this.options.method,
|
|
body: fd
|
|
})
|
|
}
|
|
}
|
|
|
|
app.registerClass('Matomo4Sparc', Matomo4Sparc)
|
|
|
|
if(app.config.matomo4sparc && app.config.matomo4sparc.enabled) {
|
|
app.matomo = new Matomo4Sparc(app.config.matomo4sparc)
|
|
app.matomo.logAction('Launched Myeic App', { new_visit: 1 })
|
|
} |