parent
c57cc93fd6
commit
213963671d
@ -0,0 +1,49 @@ |
||||
Template.body.onRendered(() => { |
||||
Tracker.autorun((c) => { |
||||
let url = RocketChat.settings.get('PiwikAnalytics_url'); |
||||
let 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)]); |
||||
let d = document; |
||||
let g = d.createElement('script'); |
||||
let 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); |
||||
})(); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
//Track logins and associate user ids with piwik
|
||||
(() => { |
||||
let oldUserId = null; |
||||
|
||||
Meteor.autorun(() => { |
||||
let newUserId = Meteor.userId(); |
||||
if (oldUserId === null && newUserId) { |
||||
if (window._paq) { |
||||
window._paq.push(['trackEvent', 'User', 'Login', newUserId ]); |
||||
window._paq.push(['setUserId', newUserId]); |
||||
} |
||||
} else if (newUserId === null && oldUserId) { |
||||
if (window._paq) { |
||||
window._paq.push(['trackEvent', 'User', 'Logout', oldUserId ]); |
||||
} |
||||
} |
||||
oldUserId = Meteor.userId(); |
||||
}); |
||||
})(); |
@ -0,0 +1,24 @@ |
||||
//Trigger the trackPageView manually as the page views don't seem to be tracked
|
||||
FlowRouter.triggers.enter([function updatePiwik(route) { |
||||
if (window._paq) { |
||||
let http = location.protocol; |
||||
let slashes = http.concat('//'); |
||||
let host = slashes.concat(window.location.hostname); |
||||
|
||||
window._paq.push(['setCustomUrl', host + route.path]); |
||||
window._paq.push(['trackPageView']); |
||||
} |
||||
}]); |
||||
|
||||
//Custom events
|
||||
RocketChat.callbacks.add('afterSaveMessage', (message) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_messages')) { |
||||
window._paq.push(['trackEvent', 'Message', 'Send', ChatRoom.findOne({ _id: message.rid }).name ]); |
||||
} |
||||
}, 2000); |
||||
|
||||
RocketChat.callbacks.add('afterCreateChannel', (channel) => { |
||||
if (window._paq && RocketChat.settings.get('PiwikAnalytics_features_rooms')) { |
||||
window._paq.push(['trackEvent', 'Room', 'Create', channel.name]); |
||||
} |
||||
}); |
@ -0,0 +1,16 @@ |
||||
Package.describe({ |
||||
name: 'rocketchat:piwik', |
||||
version: '0.0.1', |
||||
summary: 'Piwik integeration for Rocket.Chat', |
||||
git: '' |
||||
}); |
||||
|
||||
Package.onUse(function(api) { |
||||
api.versionsFrom('1.0'); |
||||
|
||||
api.use([ 'ecmascript', 'rocketchat:lib' ]); |
||||
api.use([ 'templating', 'kadira:flow-router'], 'client'); |
||||
|
||||
api.addFiles([ 'client/loadScript.js', 'client/trackEvents.js' ], 'client'); |
||||
api.addFiles([ 'server/settings.js' ], 'server'); |
||||
}); |
@ -0,0 +1,27 @@ |
||||
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' |
||||
}); |
||||
}); |
||||
}); |
Loading…
Reference in new issue