2017-05-28 03:47:09 +08:00
|
|
|
/*
|
|
|
|
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
import { getCurrentLanguage } from './languageHandler';
|
2017-05-28 03:47:09 +08:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2017-05-30 02:04:37 +08:00
|
|
|
import PlatformPeg from './PlatformPeg';
|
2017-05-29 21:26:29 +08:00
|
|
|
import SdkConfig from './SdkConfig';
|
2017-05-28 03:47:09 +08:00
|
|
|
|
|
|
|
function redact(str) {
|
|
|
|
return str.replace(/#\/(room|user)\/(.+)/, "#/$1/<redacted>");
|
|
|
|
}
|
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
const customVariables = {
|
|
|
|
'App Platform': 1,
|
|
|
|
'App Version': 2,
|
|
|
|
'User Type': 3,
|
|
|
|
'Chosen Language': 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-28 03:47:09 +08:00
|
|
|
class Analytics {
|
|
|
|
constructor() {
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq = null;
|
2017-05-29 21:26:29 +08:00
|
|
|
this.disabled = true;
|
2017-05-30 00:26:48 +08:00
|
|
|
this.firstPage = true;
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 21:26:29 +08:00
|
|
|
/**
|
|
|
|
* Enable Analytics if initialized but disabled
|
|
|
|
* otherwise try and initalize, no-op if piwik config missing
|
|
|
|
*/
|
|
|
|
enable() {
|
2017-05-30 00:26:48 +08:00
|
|
|
if (this._paq || this._init()) {
|
|
|
|
this.disabled = false;
|
|
|
|
}
|
2017-05-29 21:26:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable Analytics calls, will not fully unload Piwik until a refresh,
|
|
|
|
* but this is second best, Piwik should not pull anything implicitly.
|
|
|
|
*/
|
|
|
|
disable() {
|
|
|
|
this.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_init() {
|
|
|
|
const config = SdkConfig.get();
|
|
|
|
if (!config || !config.piwik || !config.piwik.url || !config.piwik.siteId) return;
|
|
|
|
|
|
|
|
const url = config.piwik.url;
|
|
|
|
const siteId = config.piwik.siteId;
|
|
|
|
const self = this;
|
2017-05-28 03:47:09 +08:00
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
window._paq = this._paq = window._paq || [];
|
|
|
|
|
|
|
|
this._paq.push(['setTrackerUrl', url+'piwik.php']);
|
|
|
|
this._paq.push(['setSiteId', siteId]);
|
2017-05-30 02:04:37 +08:00
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['trackAllContentImpressions']);
|
|
|
|
this._paq.push(['discardHashTag', false]);
|
|
|
|
this._paq.push(['enableHeartBeatTimer']);
|
|
|
|
this._paq.push(['enableLinkTracking', true]);
|
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
const platform = PlatformPeg.get();
|
2017-05-30 02:52:00 +08:00
|
|
|
this._setVisitVariable('App Platform', platform.getHumanReadableName());
|
2017-05-30 02:04:37 +08:00
|
|
|
platform.getAppVersion().then((version) => {
|
|
|
|
this._setVisitVariable('App Version', version);
|
|
|
|
}).catch(() => {
|
|
|
|
this._setVisitVariable('App Version', 'unknown');
|
|
|
|
});
|
|
|
|
|
|
|
|
this._setVisitVariable('Chosen Language', getCurrentLanguage());
|
|
|
|
|
2017-05-29 21:26:29 +08:00
|
|
|
(function() {
|
|
|
|
const g = document.createElement('script');
|
|
|
|
const s = document.getElementsByTagName('script')[0];
|
|
|
|
g.type='text/javascript'; g.async=true; g.defer=true; g.src=url+'piwik.js';
|
|
|
|
|
|
|
|
g.onload = function() {
|
|
|
|
console.log('Initialised anonymous analytics');
|
2017-05-30 00:26:48 +08:00
|
|
|
self._paq = window._paq;
|
2017-05-29 21:26:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
s.parentNode.insertBefore(g, s);
|
|
|
|
})();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
trackPageChange() {
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled) return;
|
2017-05-30 00:26:48 +08:00
|
|
|
if (this.firstPage) {
|
|
|
|
// De-duplicate first page
|
|
|
|
// router seems to hit the fn twice
|
|
|
|
this.firstPage = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._paq.push(['setCustomUrl', redact(window.location.href)]);
|
|
|
|
this._paq.push(['trackPageView']);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
trackEvent(category, action, name) {
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled) return;
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['trackEvent', category, action, name]);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
logout() {
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled) return;
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['deleteCookies']);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
login() { // not used currently
|
2017-05-28 03:47:09 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
2017-05-29 21:26:29 +08:00
|
|
|
if (this.disabled || !cli) return;
|
2017-05-28 03:47:09 +08:00
|
|
|
|
2017-05-30 00:26:48 +08:00
|
|
|
this._paq.push(['setUserId', `@${cli.getUserIdLocalpart()}:${cli.getDomain()}`]);
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 02:04:37 +08:00
|
|
|
_setVisitVariable(key, value) {
|
|
|
|
this._paq.push(['setCustomVariable', customVariables[key], key, value, 'visit']);
|
|
|
|
}
|
|
|
|
|
|
|
|
setGuest(guest) {
|
|
|
|
if (this.disabled) return;
|
|
|
|
this._setVisitVariable('User Type', guest ? 'Guest' : 'Logged In');
|
|
|
|
}
|
2017-05-28 03:47:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.mxAnalytics) {
|
|
|
|
global.mxAnalytics = new Analytics();
|
|
|
|
}
|
|
|
|
module.exports = global.mxAnalytics;
|