Преглед на файлове

Integration of matomo with env vars

guillaume преди 7 години
родител
ревизия
ec59af3777
променени са 5 файла, в които са добавени 92 реда и са изтрити 1 реда
  1. 45 0
      client/lib/utils.js
  2. 6 0
      config/router.js
  3. 17 0
      models/settings.js
  4. 9 0
      server/policy.js
  5. 15 1
      snap-src/bin/config

+ 45 - 0
client/lib/utils.js

@@ -144,6 +144,51 @@ Utils = {
       }
     });
   },
+
+  setMatomo(data){
+    window._paq = window._paq || [];
+    window._paq.push(['setDoNotTrack', data.doNotTrack]);
+    if (data.withUserName){
+      window._paq.push(['setUserId', Meteor.user().username]);
+    }
+    window._paq.push(['trackPageView']);
+    window._paq.push(['enableLinkTracking']);
+
+    (function() {
+      window._paq.push(['setTrackerUrl', `${data.address}piwik.php`]);
+      window._paq.push(['setSiteId', data.siteId]);
+
+      const script = document.createElement('script');
+      Object.assign(script, {
+        id: 'scriptMatomo',
+        type: 'text/javascript',
+        async: 'true',
+        defer: 'true',
+        src: `${data.address}piwik.js`,
+      });
+
+      const s = document.getElementsByTagName('script')[0];
+      s.parentNode.insertBefore(script, s);
+    })();
+
+    Session.set('matomo', true);
+  },
+
+  manageMatomo() {
+    const matomo = Session.get('matomo');
+    if (matomo === undefined){
+      Meteor.call('getMatomoConf', (err, data) => {
+        if (err && err.error[0] === 'var-not-exist'){
+          Session.set('matomo', false); // siteId || address server not defined
+        }
+        if (!err){
+          Utils.setMatomo(data);
+        }
+      });
+    } else if (matomo) {
+      window._paq.push(['trackPageView']);
+    }
+  },
 };
 
 // A simple tracker dependency that we invalidate every time the window is

+ 6 - 0
config/router.js

@@ -14,6 +14,8 @@ FlowRouter.route('/', {
     Filter.reset();
     EscapeActions.executeAll();
 
+    Utils.manageMatomo();
+
     BlazeLayout.render('defaultLayout', {
       headerBar: 'boardListHeaderBar',
       content: 'boardList',
@@ -38,6 +40,8 @@ FlowRouter.route('/b/:id/:slug', {
       EscapeActions.executeUpTo('popup-close');
     }
 
+    Utils.manageMatomo();
+
     BlazeLayout.render('defaultLayout', {
       headerBar: 'boardHeaderBar',
       content: 'board',
@@ -53,6 +57,8 @@ FlowRouter.route('/b/:boardId/:slug/:cardId', {
     Session.set('currentBoard', params.boardId);
     Session.set('currentCard', params.cardId);
 
+    Utils.manageMatomo();
+
     BlazeLayout.render('defaultLayout', {
       headerBar: 'boardHeaderBar',
       content: 'board',

+ 17 - 0
models/settings.js

@@ -96,6 +96,14 @@ if (Meteor.isServer) {
     return (min + Math.round(rand * range));
   }
 
+  function getEnvVar(name){
+    const value = process.env[name];
+    if (value){
+      return value;
+    }
+    throw new Meteor.Error(['var-not-exist', `The environment variable ${name} does not exist`]);
+  }
+
   function sendInvitationEmail (_id){
     const icode = InvitationCodes.findOne(_id);
     const author = Users.findOne(Meteor.userId());
@@ -180,5 +188,14 @@ if (Meteor.isServer) {
         email: user.emails[0].address,
       };
     },
+
+    getMatomoConf(){
+      return {
+        address: getEnvVar('MATOMO_ADDRESS'),
+        siteId: getEnvVar('MATOMO_SITE_ID'),
+        doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
+        withUserName: process.env.MATOMO_WITH_USERNAME || false,
+      };
+    },
   });
 }

+ 9 - 0
server/policy.js

@@ -0,0 +1,9 @@
+import { BrowserPolicy } from 'meteor/browser-policy-common';
+
+Meteor.startup(() => {
+  const matomoUrl = process.env.MATOMO_ADDRESS;
+  if (matomoUrl){
+    BrowserPolicy.content.allowScriptOrigin(matomoUrl);
+    BrowserPolicy.content.allowImageOrigin(matomoUrl);
+  }
+});

+ 15 - 1
snap-src/bin/config

@@ -3,7 +3,7 @@
 # All supported keys are defined here together with descriptions and default values
 
 # list of supported keys
-keys="MONGODB_BIND_UNIX_SOCKET MONGODB_BIND_IP MONGODB_PORT MAIL_URL MAIL_FROM ROOT_URL PORT DISABLE_MONGODB CADDY_ENABLED CADDY_BIND_PORT WITH_API"
+keys="MONGODB_BIND_UNIX_SOCKET MONGODB_BIND_IP MONGODB_PORT MAIL_URL MAIL_FROM ROOT_URL PORT DISABLE_MONGODB CADDY_ENABLED CADDY_BIND_PORT WITH_API MATOMO_ADDRESS MATOMO_SITE_ID MATOMO_DO_NOT_TRACK MATOMO_WITH_USERNAME"
 
 # default values
 DESCRIPTION_MONGODB_BIND_UNIX_SOCKET="mongodb binding unix socket:\n"\
@@ -51,3 +51,17 @@ KEY_CADDY_BIND_PORT="caddy-bind-port"
 DESCRIPTION_WITH_API="Enable/disable the api of wekan"
 DEFAULT_WITH_API="false"
 KEY_WITH_API="with-api"
+
+DESCRIPTION_MATOMO_ADDRESS="The address of the server where matomo is hosted"
+KEY_MATOMO_ADDRESS="matomo-address"
+
+DESCRIPTION_MATOMO_SITE_ID="The value of the site ID given in matomo server for wekan"
+KEY_MATOMO_SITE_ID="matomo-site-id"
+
+DESCRIPTION_MATOMO_DO_NOT_TRACK="The option do not track which enables users to not be tracked by matomo"
+DEFAULT_CADDY_BIND_PORT="false"
+KEY_MATOMO_DO_NOT_TRACK="matomo-do-not-track"
+
+DESCRIPTION_MATOMO_WITH_USERNAME="The option that allows matomo to retrieve the username"
+DEFAULT_CADDY_BIND_PORT="false"
+KEY_MATOMO_WITH_USERNAME="matomo-with-username"