Remove segment support (main events package should be service-agnostic apart from Google Analytics)

This commit is contained in:
SachaG 2017-06-02 07:46:39 +09:00
parent 71fb896378
commit 0ce8051130
4 changed files with 16 additions and 134 deletions

View file

@ -1,53 +1,5 @@
import { addCallback } from 'meteor/vulcan:core';
import { sendGoogleAnalyticsRequest, requestAnalyticsAsync } from './helpers';
import { sendGoogleAnalyticsRequest } from './helpers';
// add client-side callback: log a ga request on page view
addCallback('router.onUpdate', sendGoogleAnalyticsRequest);
// generate callbacks on collection for each common mutation
['users', 'posts', 'comments', 'categories'].map(collection => {
return ['new', 'edit', 'remove'].map(mutation => {
const hook = `${collection}.${mutation}`;
addCallback(`${hook}.async`, function MutationAnalyticsTracking(...args) {
// a note on what's happenning below:
// the first argument is always the document we are interested in
// the second to last argument is always the current user
// on edit.async, the argument on index 1 is always the previous document
// see vulcan:lib/mutations.js for more informations
// remove unnecessary 'previousDocument' if operating on a collection.edit hook
if (hook.includes('edit')) {
args.splice(1,1);
}
const [document, currentUser, ...rest] = args; // eslint-disable-line no-unused-vars
return requestAnalyticsAsync(hook, document, currentUser);
});
// return the hook name, used for debug
return hook;
});
});
// generate callbacks on voting operations
['upvote', 'cancelUpvote', 'downvote', 'cancelDownvote'].map(operation => {
addCallback(`${operation}.async`, function OperationTracking(...args) {
const [document, currentUser, ...rest] = args; // eslint-disable-line no-unused-vars
return requestAnalyticsAsync(operation, document, currentUser);
});
});
// identify profile completion
addCallback(`users.profileCompleted.async`, function ProfileCompletedTracking(user) {
return requestAnalyticsAsync('users.profileCompleted', user);
});
addCallback('router.onUpdate', sendGoogleAnalyticsRequest);

View file

@ -1,4 +1,3 @@
import Analytics from 'analytics-node';
import { getSetting } from 'meteor/vulcan:core';
import Events from './collection.js';
/*
@ -42,72 +41,6 @@ export const initGoogleAnalytics = () => {
}
};
/*
We provide a special support for Segment, using analytics-node
See https://segment.com/docs/sources/server/node/
*/
export const requestAnalyticsAsync = (hook, document, user) => {
// get the segment write key from the settings
const useSegment = getSetting('useSegment');
const writeKey = getSetting('segmentWriteKey');
// the settings obviously tells to use segment
// and segment write key is defined & isn't the placeholder from sample_settings.json
if (useSegment && writeKey && writeKey !== '456bar') {
const analytics = new Analytics(writeKey);
if (hook.includes('users')) {
// if the mutation is related to users, use analytics.identify
// see https://segment.com/docs/sources/server/node/#identify
// note: on users.new.async, user is undefined
const userId = user ? user._id : document._id;
if (document.services) {
if(document.services.password) {
delete document.services.password;
}
if (document.services.resume) {
delete document.services.resume;
}
}
const data = {
userId,
traits: document,
};
// uncomment for debug
// console.log(`// dispatching identify on "${hook}" (user ${userId})`);
// console.log(data);
analytics.identify(data);
} else {
// else use analytics.track
// see https://segment.com/docs/sources/server/node/#track
const data = {
userId: user._id,
event: hook,
properties: document,
};
// uncomment for debug
// console.log(`// dispatching track on "${hook}"`);
// console.log(data);
analytics.track(data);
}
}
}
// collection based logging
Events.log = function (event) {

View file

@ -1,19 +1,18 @@
import { GraphQLSchema } from 'meteor/vulcan:core';
// import Events from './collection.js';
import { requestAnalyticsAsync } from './helpers.js';
// import { GraphQLSchema } from 'meteor/vulcan:core';
// // import Events from './collection.js';
// import { requestAnalyticsAsync } from './helpers.js';
GraphQLSchema.addMutation('eventTrack(eventName: String, properties: JSON): JSON');
// GraphQLSchema.addMutation('eventTrack(eventName: String, properties: JSON): JSON');
const resolvers = {
Mutation: {
eventTrack: (root, { eventName, properties }, context) => {
const user = context.currentUser || {_id: 'anonymous'};
// const resolvers = {
// Mutation: {
// eventTrack: (root, { eventName, properties }, context) => {
// const user = context.currentUser || {_id: 'anonymous'};
requestAnalyticsAsync(eventName, properties, user);
return properties;
},
},
};
// return properties;
// },
// },
// };
GraphQLSchema.addResolvers(resolvers);
// GraphQLSchema.addResolvers(resolvers);

View file

@ -1,7 +1,5 @@
import Events from './collection.js';
import { requestAnalyticsAsync } from './helpers';
import './callbacks.js';
import './mutations.js';
// import './mutations.js';
export default Events;
export { requestAnalyticsAsync };