1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-09 07:45:22 +02:00

feat(analytics): add apis for event tracking (#5298)

* feat(analytics): add apis for event tracking

feat(api): fetch instanceID

feat(state): set instance id and version on matomo

refactor(state): export validation of app state

feat(analytics): update dimensions

refactor(analytics): move matomo to module

feat(analytics): disable analytics on non production

feat(analytics): track event metadata

refactor(analytics): clean push function

refactor(analytics): rename init function

feat(analytics): track user role

feat(analytics): track user global role

fix(stacks): remove event tracking for stack create

* style(analytics): remove TODO

* feat(build): add testing env
This commit is contained in:
Chaim Lev-Ari 2021-08-11 01:45:53 +03:00 committed by GitHub
parent 11d555bbd6
commit 9be0b89aff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 251 additions and 282 deletions

View file

@ -2,6 +2,8 @@ export function StatusViewModel(data) {
this.Authentication = data.Authentication;
this.Snapshot = data.Snapshot;
this.Version = data.Version;
this.Edition = data.Edition;
this.InstanceID = data.InstanceID;
}
export function StatusVersionViewModel(data) {

View file

@ -2,29 +2,16 @@ import moment from 'moment';
angular.module('portainer.app').factory('StateManager', [
'$q',
'$async',
'SystemService',
'InfoHelper',
'EndpointProvider',
'LocalStorage',
'SettingsService',
'StatusService',
'APPLICATION_CACHE_VALIDITY',
'AgentPingService',
'$analytics',
function StateManagerFactory(
$q,
SystemService,
InfoHelper,
EndpointProvider,
LocalStorage,
SettingsService,
StatusService,
APPLICATION_CACHE_VALIDITY,
AgentPingService,
$analytics
) {
'use strict';
function StateManagerFactory($q, $async, SystemService, InfoHelper, LocalStorage, SettingsService, StatusService, APPLICATION_CACHE_VALIDITY, AgentPingService, $analytics) {
var manager = {};
var state = {
@ -85,6 +72,9 @@ angular.module('portainer.app').factory('StateManager', [
function assignStateFromStatusAndSettings(status, settings) {
state.application.version = status.Version;
state.application.edition = status.Edition;
state.application.instanceId = status.InstanceID;
state.application.enableTelemetry = settings.EnableTelemetry;
state.application.logo = settings.LogoURL;
state.application.snapshotInterval = settings.SnapshotInterval;
@ -103,55 +93,51 @@ angular.module('portainer.app').factory('StateManager', [
var status = data.status;
var settings = data.settings;
assignStateFromStatusAndSettings(status, settings);
$analytics.setOptOut(!settings.EnableTelemetry);
LocalStorage.storeApplicationState(state.application);
deferred.resolve(state);
})
.catch(function error(err) {
deferred.reject({ msg: 'Unable to retrieve server settings and status', err: err });
})
.finally(function final() {
state.loading = false;
});
return deferred.promise;
}
manager.initialize = function () {
var deferred = $q.defer();
var UIState = LocalStorage.getUIState();
if (UIState) {
state.UI = UIState;
}
var endpointState = LocalStorage.getEndpointState();
if (endpointState) {
state.endpoint = endpointState;
}
var applicationState = LocalStorage.getApplicationState();
if (applicationState && applicationState.validity) {
var now = moment().unix();
var cacheValidity = now - applicationState.validity;
if (cacheValidity > APPLICATION_CACHE_VALIDITY) {
loadApplicationState()
.then(() => deferred.resolve(state))
.catch((err) => deferred.reject(err));
} else {
state.application = applicationState;
state.loading = false;
$analytics.setOptOut(!state.application.enableTelemetry);
deferred.resolve(state);
manager.initialize = initialize;
async function initialize() {
return $async(async () => {
const UIState = LocalStorage.getUIState();
if (UIState) {
state.UI = UIState;
}
} else {
loadApplicationState()
.then(() => deferred.resolve(state))
.catch((err) => deferred.reject(err));
}
return deferred.promise;
};
const endpointState = LocalStorage.getEndpointState();
if (endpointState) {
state.endpoint = endpointState;
}
const applicationState = LocalStorage.getApplicationState();
if (isAppStateValid(applicationState)) {
state.application = applicationState;
} else {
await loadApplicationState();
}
state.loading = false;
$analytics.setPortainerStatus(state.application.instanceId, state.application.version);
$analytics.setOptOut(!state.application.enableTelemetry);
return state;
});
}
function isAppStateValid(appState) {
if (!appState || !appState.validity) {
return false;
}
const now = moment().unix();
const cacheValidity = now - appState.validity;
return cacheValidity < APPLICATION_CACHE_VALIDITY;
}
function assignExtensions(endpointExtensions) {
var extensions = [];

View file

@ -5,6 +5,7 @@ class AuthenticationController {
/* @ngInject */
constructor(
$async,
$analytics,
$scope,
$state,
$stateParams,
@ -20,6 +21,7 @@ class AuthenticationController {
StatusService
) {
this.$async = $async;
this.$analytics = $analytics;
this.$scope = $scope;
this.$state = $state;
this.$stateParams = $stateParams;
@ -150,6 +152,10 @@ class AuthenticationController {
async postLoginSteps() {
await this.StateManager.initialize();
const isAdmin = this.Authentication.isAdmin();
this.$analytics.setUserRole(isAdmin ? 'admin' : 'standard-user');
await this.checkForEndpointsAsync();
await this.checkForLatestVersionAsync();
}