Vulcan/packages/vulcan-events-segment/lib/server/segment-server.js

47 lines
1,022 B
JavaScript
Raw Normal View History

import Analytics from 'analytics-node';
2018-01-25 15:03:03 -06:00
import { getSetting } from 'meteor/vulcan:core';
import {
/* addPageFunction, addInitFunction, */
addIdentifyFunction,
addTrackFunction,
} from 'meteor/vulcan:events';
2017-12-20 09:43:13 +09:00
const segmentWriteKey = getSetting('segment.serverKey');
2017-12-20 09:43:13 +09:00
if (segmentWriteKey) {
2017-12-20 09:43:13 +09:00
const analytics = new Analytics(segmentWriteKey);
2017-12-20 09:43:13 +09:00
/*
Identify User
*/
2018-01-25 15:03:03 -06:00
// eslint-disable-next-line no-inner-declarations
2017-12-20 09:43:13 +09:00
function segmentIdentifyServer(currentUser) {
analytics.identify({
userId: currentUser._id,
traits: {
email: currentUser.email,
pageUrl: currentUser.pageUrl,
},
});
}
addIdentifyFunction(segmentIdentifyServer);
2017-12-20 09:43:13 +09:00
/*
2017-12-20 09:43:13 +09:00
Track Event
2017-12-20 09:43:13 +09:00
*/
2018-01-25 15:03:03 -06:00
// eslint-disable-next-line no-inner-declarations
2017-12-20 09:43:13 +09:00
function segmentTrackServer(eventName, eventProperties, currentUser) {
analytics.track({
event: eventName,
properties: eventProperties,
userId: currentUser && currentUser._id,
});
}
addTrackFunction(segmentTrackServer);
}