parent
225624bd8d
commit
946ec4e2ae
@ -1,23 +1,29 @@ |
||||
# Piwik Analytics Tracking |
||||
# Analytics Tracking |
||||
|
||||
## Google Analytics |
||||
|
||||
### Settings |
||||
* **Tracking ID**: Google Analytics tracking id found on analytics admin page - looks like UA-xxxxxxxx-x |
||||
|
||||
## Piwik |
||||
Rocket.Chat supports adding Piwik url and site id to track the analytics of your |
||||
server. Through this you will be able to see details analytics of per user, |
||||
including how many messages a session they send via custom events in Piwik and |
||||
how many channels they interact with. |
||||
|
||||
## Piwik & Google Chrome |
||||
### Piwik & Google Chrome |
||||
Google Chrome has a setting which sends a Do Not Track with each request and by |
||||
default Piwik respects that and you have to manually disable that feature inside |
||||
of Piwik. [Piwik has great documentation on how to disable this feature.](http://piwik.org/docs/privacy/#step-4-respect-donottrack-preference) |
||||
|
||||
## Piwik Settings in Rocket.Chat |
||||
Settings -> Piwik |
||||
|
||||
### General |
||||
### Settings |
||||
* **URL**: The url where your piwik is located. This is used for generating the tracking code and is required. Recommended format is: `//rocketchat.piwikpro.com/` |
||||
* **Client ID**: The client id which this website is. This is a number without any decimals, example: `1` |
||||
|
||||
### Features Enabled |
||||
## Settings in Rocket.Chat |
||||
Settings -> Analytics |
||||
|
||||
## Features Enabled |
||||
* **Messages**: `true/false` determines whether to use custom events to track how many times a user does something with a message. This ranges from sending messages, editing messages, reacting to messages, pinning, starring, and etc. |
||||
* **Rooms**: `true/false` determines whether to use custom events to track how many times a user does actions related to a room (channel, direct message, group). This ranges from creating, leaving, archiving, renaming, and etc. |
||||
* **Users**: `true/false` determines whether to use custom events to track how many times a user does actions related to users. This ranges from resetting passwords, creating new users, updating profile pictures, etc. |
||||
@ -0,0 +1,44 @@ |
||||
Template.body.onRendered(() => { |
||||
Tracker.autorun((c) => { |
||||
const piwikUrl = RocketChat.settings.get('PiwikAnalytics_enabled') && RocketChat.settings.get('PiwikAnalytics_url'); |
||||
const piwikSiteId = piwikUrl && RocketChat.settings.get('PiwikAnalytics_siteId'); |
||||
const googleId = RocketChat.settings.get('GoogleAnalytics_enabled') && RocketChat.settings.get('GoogleAnalytics_ID'); |
||||
if (piwikSiteId || googleId) { |
||||
c.stop(); |
||||
|
||||
if (piwikSiteId) { |
||||
window._paq = window._paq || []; |
||||
if (Meteor.userId()) { |
||||
window._paq.push(['setUserId', Meteor.userId()]); |
||||
} |
||||
|
||||
window._paq.push(['trackPageView']); |
||||
window._paq.push(['enableLinkTracking']); |
||||
(() => { |
||||
window._paq.push(['setTrackerUrl', piwikUrl + 'piwik.php']); |
||||
window._paq.push(['setSiteId', Number.parseInt(piwikSiteId)]); |
||||
const d = document; |
||||
const g = d.createElement('script'); |
||||
const s = d.getElementsByTagName('script')[0]; |
||||
g.type = 'text/javascript'; |
||||
g.async = true; |
||||
g.defer = true; |
||||
g.src = piwikUrl + 'piwik.js'; |
||||
s.parentNode.insertBefore(g, s); |
||||
})(); |
||||
} |
||||
|
||||
if (googleId) { |
||||
/*eslint-disable */ |
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
||||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); |
||||
|
||||
ga('create', googleId, 'auto'); |
||||
ga('send', 'pageview'); |
||||
/*eslint-enable */ |
||||
} |
||||
} |
||||
}); |
||||
}); |
||||
@ -0,0 +1,137 @@ |
||||
function trackEvent(category, action, label) { |
||||
if (window._paq) { |
||||
window._paq.push(['trackEvent', category, action, label]); |
||||
} |
||||
if (window.ga) { |
||||
window.ga('send', 'event', category, action, label); |
||||
} |
||||
} |
||||
|
||||
if (!window._paq || window.ga) { |
||||
//Trigger the trackPageView manually as the page views don't seem to be tracked
|
||||
FlowRouter.triggers.enter([(route) => { |
||||
if (window._paq) { |
||||
const http = location.protocol; |
||||
const slashes = http.concat('//'); |
||||
const host = slashes.concat(window.location.hostname); |
||||
window._paq.push(['setCustomUrl', host + route.path]); |
||||
window._paq.push(['trackPageView']); |
||||
} |
||||
if (window.ga) { |
||||
window.ga('send', 'pageview', route.path); |
||||
} |
||||
}]); |
||||
|
||||
//Login page has manual switches
|
||||
RocketChat.callbacks.add('loginPageStateChange', (state) => { |
||||
trackEvent('Navigation', 'Login Page State Change', state); |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-login-state-change'); |
||||
|
||||
//Messsages
|
||||
RocketChat.callbacks.add('afterSaveMessage', (message) => { |
||||
if ((window._paq || window.ga) && RocketChat.settings.get('Analytics_features_messages')) { |
||||
const room = ChatRoom.findOne({ _id: message.rid }); |
||||
trackEvent('Message', 'Send', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, 2000, 'trackEvents'); |
||||
|
||||
//Rooms
|
||||
RocketChat.callbacks.add('afterCreateChannel', (owner, room) => { |
||||
if (RocketChat.settings.get('Analytics_features_rooms')) { |
||||
trackEvent('Room', 'Create', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-after-create-channel'); |
||||
|
||||
RocketChat.callbacks.add('roomNameChanged', (room) => { |
||||
if (RocketChat.settings.get('Analytics_features_rooms')) { |
||||
trackEvent('Room', 'Changed Name', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-room-name-changed'); |
||||
|
||||
RocketChat.callbacks.add('roomTopicChanged', (room) => { |
||||
if (RocketChat.settings.get('Analytics_features_rooms')) { |
||||
trackEvent('Room', 'Changed Topic', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-room-topic-changed'); |
||||
|
||||
RocketChat.callbacks.add('roomTypeChanged', (room) => { |
||||
if (RocketChat.settings.get('Analytics_features_rooms')) { |
||||
trackEvent('Room', 'Changed Room Type', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-room-type-changed'); |
||||
|
||||
RocketChat.callbacks.add('archiveRoom', (room) => { |
||||
if (RocketChat.settings.get('Analytics_features_rooms')) { |
||||
trackEvent('Room', 'Archived', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-archive-room'); |
||||
|
||||
RocketChat.callbacks.add('unarchiveRoom', (room) => { |
||||
if (RocketChat.settings.get('Analytics_features_rooms')) { |
||||
trackEvent('Room', 'Unarchived', room.name + ' (' + room._id + ')'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-unarchive-room'); |
||||
|
||||
//Users
|
||||
//Track logins and associate user ids with piwik
|
||||
(() => { |
||||
let oldUserId = null; |
||||
|
||||
Meteor.autorun(() => { |
||||
const newUserId = Meteor.userId(); |
||||
if (oldUserId === null && newUserId) { |
||||
if (window._paq && RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Login', newUserId); |
||||
window._paq.push(['setUserId', newUserId]); |
||||
} |
||||
} else if (newUserId === null && oldUserId) { |
||||
if (window._paq && RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Logout', oldUserId); |
||||
} |
||||
} |
||||
oldUserId = Meteor.userId(); |
||||
}); |
||||
})(); |
||||
|
||||
RocketChat.callbacks.add('userRegistered', () => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Registered'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-resitered'); |
||||
|
||||
RocketChat.callbacks.add('usernameSet', () => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Username Set'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piweik-username-set'); |
||||
|
||||
RocketChat.callbacks.add('userPasswordReset', () => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Reset Password'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-password-reset'); |
||||
|
||||
RocketChat.callbacks.add('userConfirmationEmailRequested', () => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Confirmation Email Requested'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-confirmation-email-requested'); |
||||
|
||||
RocketChat.callbacks.add('userForgotPasswordEmailRequested', () => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Forgot Password Email Requested'); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-forgot-password-email-requested'); |
||||
|
||||
RocketChat.callbacks.add('userStatusManuallySet', (status) => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Status Manually Changed', status); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-user-status-manually-set'); |
||||
|
||||
RocketChat.callbacks.add('userAvatarSet', (service) => { |
||||
if (RocketChat.settings.get('Analytics_features_users')) { |
||||
trackEvent('User', 'Avatar Changed', service); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'analytics-user-avatar-set'); |
||||
} |
||||
@ -1,7 +1,7 @@ |
||||
Package.describe({ |
||||
name: 'rocketchat:piwik', |
||||
version: '0.0.1', |
||||
summary: 'Piwik integeration for Rocket.Chat', |
||||
name: 'rocketchat:analytics', |
||||
version: '0.0.2', |
||||
summary: 'Analytics integeration for Rocket.Chat', |
||||
git: '' |
||||
}); |
||||
|
||||
@ -0,0 +1,59 @@ |
||||
RocketChat.settings.addGroup('Analytics', function addSettings() { |
||||
this.section('Piwik', function() { |
||||
const enableQuery = {_id: 'PiwikAnalytics_enabled', value: true}; |
||||
this.add('PiwikAnalytics_enabled', false, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Enable' |
||||
}); |
||||
this.add('PiwikAnalytics_url', '', { |
||||
type: 'string', |
||||
public: true, |
||||
i18nLabel: 'URL', |
||||
enableQuery |
||||
}); |
||||
this.add('PiwikAnalytics_siteId', '', { |
||||
type: 'string', |
||||
public: true, |
||||
i18nLabel: 'Client_ID', |
||||
enableQuery |
||||
}); |
||||
}); |
||||
|
||||
this.section('Analytics_Google', function() { |
||||
const enableQuery = {_id: 'GoogleAnalytics_enabled', value: true}; |
||||
this.add('GoogleAnalytics_enabled', false, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Enable' |
||||
}); |
||||
|
||||
this.add('GoogleAnalytics_ID', '', { |
||||
type: 'string', |
||||
public: true, |
||||
i18nLabel: 'Analytics_Google_id', |
||||
enableQuery |
||||
}); |
||||
}); |
||||
|
||||
this.section('Analytics_features_enabled', function addFeaturesEnabledSettings() { |
||||
this.add('Analytics_features_messages', true, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Messages', |
||||
i18nDescription: 'Analytics_features_messages_Description' |
||||
}); |
||||
this.add('Analytics_features_rooms', true, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Rooms', |
||||
i18nDescription: 'Analytics_features_rooms_Description' |
||||
}); |
||||
this.add('Analytics_features_users', true, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Users', |
||||
i18nDescription: 'Analytics_features_users_Description' |
||||
}); |
||||
}); |
||||
}); |
||||
@ -1,29 +0,0 @@ |
||||
Template.body.onRendered(() => { |
||||
Tracker.autorun((c) => { |
||||
const url = RocketChat.settings.get('PiwikAnalytics_url'); |
||||
const siteId = RocketChat.settings.get('PiwikAnalytics_siteId'); |
||||
|
||||
if (Match.test(url, String) && url.trim() !== '' && Match.test(siteId, String) && siteId.trim() !== '') { |
||||
c.stop(); |
||||
window._paq = window._paq || []; |
||||
if (Meteor.userId()) { |
||||
window._paq.push(['setUserId', Meteor.userId()]); |
||||
} |
||||
|
||||
window._paq.push(['trackPageView']); |
||||
window._paq.push(['enableLinkTracking']); |
||||
(() => { |
||||
window._paq.push(['setTrackerUrl', url + 'piwik.php']); |
||||
window._paq.push(['setSiteId', Number.parseInt(siteId)]); |
||||
const d = document; |
||||
const g = d.createElement('script'); |
||||
const s = d.getElementsByTagName('script')[0]; |
||||
g.type = 'text/javascript'; |
||||
g.async = true; |
||||
g.defer = true; |
||||
g.src = url + 'piwik.js'; |
||||
s.parentNode.insertBefore(g, s); |
||||
})(); |
||||
} |
||||
}); |
||||
}); |
||||
@ -1,126 +0,0 @@ |
||||
//Trigger the trackPageView manually as the page views don't seem to be tracked
|
||||
FlowRouter.triggers.enter([(route) => { |
||||
if (window._paq) { |
||||
const http = location.protocol; |
||||
const slashes = http.concat('//'); |
||||
const host = slashes.concat(window.location.hostname); |
||||
|
||||
window._paq.push(['setCustomUrl', host + route.path]); |
||||
window._paq.push(['trackPageView']); |
||||
} |
||||
}]); |
||||
|
||||
//Login page has manual switches
|
||||
RocketChat.callbacks.add('loginPageStateChange', (state) => { |
||||
if (window._paq) { |
||||
window._paq.push(['trackEvent', 'Navigation', 'Login Page State Change', state]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-login-state-change'); |
||||
|
||||
//Messsages
|
||||
RocketChat.callbacks.add('afterSaveMessage', (message) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_messages')) { |
||||
const room = ChatRoom.findOne({ _id: message.rid }); |
||||
window._paq.push(['trackEvent', 'Message', 'Send', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, 2000, 'trackEvents'); |
||||
|
||||
//Rooms
|
||||
RocketChat.callbacks.add('afterCreateChannel', (owner, room) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Create', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-after-create-channel'); |
||||
|
||||
RocketChat.callbacks.add('roomNameChanged', (room) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Changed Name', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-room-name-changed'); |
||||
|
||||
RocketChat.callbacks.add('roomTopicChanged', (room) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Changed Topic', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-room-topic-changed'); |
||||
|
||||
RocketChat.callbacks.add('roomTypeChanged', (room) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Changed Room Type', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-room-type-changed'); |
||||
|
||||
RocketChat.callbacks.add('archiveRoom', (room) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Archived', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-archive-room'); |
||||
|
||||
RocketChat.callbacks.add('unarchiveRoom', (room) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Unarchived', room.name + ' (' + room._id + ')' ]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-unarchive-room'); |
||||
|
||||
//Users
|
||||
//Track logins and associate user ids with piwik
|
||||
(() => { |
||||
let oldUserId = null; |
||||
|
||||
Meteor.autorun(() => { |
||||
const newUserId = Meteor.userId(); |
||||
if (oldUserId === null && newUserId) { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Login', newUserId ]); |
||||
window._paq.push(['setUserId', newUserId]); |
||||
} |
||||
} else if (newUserId === null && oldUserId) { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Logout', oldUserId ]); |
||||
} |
||||
} |
||||
oldUserId = Meteor.userId(); |
||||
}); |
||||
})(); |
||||
|
||||
RocketChat.callbacks.add('userRegistered', () => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Registered']); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-resitered'); |
||||
|
||||
RocketChat.callbacks.add('usernameSet', () => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Username Set']); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piweik-username-set'); |
||||
|
||||
RocketChat.callbacks.add('userPasswordReset', () => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Reset Password']); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-password-reset'); |
||||
|
||||
RocketChat.callbacks.add('userConfirmationEmailRequested', () => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Confirmation Email Requested']); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-confirmation-email-requested'); |
||||
|
||||
RocketChat.callbacks.add('userForgotPasswordEmailRequested', () => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Forgot Password Email Requested']); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-forgot-password-email-requested'); |
||||
|
||||
RocketChat.callbacks.add('userStatusManuallySet', (status) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Status Manually Changed', status]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-status-manually-set'); |
||||
|
||||
RocketChat.callbacks.add('userAvatarSet', (service) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_users')) { |
||||
window._paq.push(['trackEvent', 'User', 'Avatar Changed', service]); |
||||
} |
||||
}, RocketChat.callbacks.priority.MEDIUM, 'piwik-user-avatar-set'); |
||||
@ -1,33 +0,0 @@ |
||||
RocketChat.settings.addGroup('Piwik', function addSettings() { |
||||
this.add('PiwikAnalytics_url', '', { |
||||
type: 'string', |
||||
public: true, |
||||
i18nLabel: 'URL' |
||||
}); |
||||
this.add('PiwikAnalytics_siteId', '', { |
||||
type: 'string', |
||||
public: true, |
||||
i18nLabel: 'Client_ID' |
||||
}); |
||||
|
||||
this.section('Analytics_features_enabled', function addFeaturesEnabledSettings() { |
||||
this.add('PiwikAnalytics_features_messages', true, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Messages', |
||||
i18nDescription: 'Analytics_features_messages_Description' |
||||
}); |
||||
this.add('PiwikAnalytics_features_rooms', true, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Rooms', |
||||
i18nDescription: 'Analytics_features_rooms_Description' |
||||
}); |
||||
this.add('PiwikAnalytics_features_users', true, { |
||||
type: 'boolean', |
||||
public: true, |
||||
i18nLabel: 'Users', |
||||
i18nDescription: 'Analytics_features_users_Description' |
||||
}); |
||||
}); |
||||
}); |
||||
Loading…
Reference in new issue