2017-02-02 10:39:33 +01:00
|
|
|
|
import { addCallback } from 'meteor/nova:core';
|
2017-02-02 09:52:47 +01:00
|
|
|
|
import Posts from 'meteor/nova:posts';
|
|
|
|
|
import Events from './collection.js';
|
|
|
|
|
import { sendGoogleAnalyticsRequest, requestAnalyticsAsync } from './helpers';
|
2017-02-01 23:11:41 +01:00
|
|
|
|
|
|
|
|
|
// add client-side callback: log a ga request on page view
|
|
|
|
|
addCallback('router.onUpdate', sendGoogleAnalyticsRequest);
|
|
|
|
|
|
|
|
|
|
|
2017-02-02 10:39:33 +01:00
|
|
|
|
// generate callbacks on collection for each common mutation
|
|
|
|
|
['users', 'posts', 'comments', 'categories'].map(collection => {
|
|
|
|
|
|
|
|
|
|
return ['new', 'edit', 'remove'].map(mutation => {
|
2017-02-02 09:52:47 +01:00
|
|
|
|
|
2017-02-02 10:39:33 +01:00
|
|
|
|
const hook = `${collection}.${mutation}`;
|
|
|
|
|
|
|
|
|
|
addCallback(`${hook}.async`, function MutationAnalyticsTracking(...args) {
|
2017-02-01 23:11:41 +01:00
|
|
|
|
|
2017-02-02 10:39:33 +01:00
|
|
|
|
// 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 nova:lib/mutations.js for more informations
|
2017-02-01 23:11:41 +01:00
|
|
|
|
|
2017-02-02 10:39:33 +01:00
|
|
|
|
// remove unnecessary 'previousDocument' if operating on a collection.edit hook
|
|
|
|
|
if (hook.includes('edit')) {
|
|
|
|
|
args.splice(1,1);
|
|
|
|
|
}
|
2017-02-02 09:52:47 +01:00
|
|
|
|
|
|
|
|
|
const [document, currentUser, ...rest] = args; // eslint-disable-line no-unused-vars
|
|
|
|
|
|
2017-02-02 10:39:33 +01:00
|
|
|
|
return requestAnalyticsAsync(hook, document, currentUser);
|
2017-02-02 09:52:47 +01:00
|
|
|
|
});
|
2017-02-02 10:39:33 +01:00
|
|
|
|
|
|
|
|
|
// return the hook name, used for debug
|
|
|
|
|
return hook;
|
2017-02-02 09:52:47 +01:00
|
|
|
|
});
|
2017-02-02 10:39:33 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// generate callbacks on voting operations
|
|
|
|
|
['upvote', 'cancelUpvote', 'downvote', 'cancelDownvote'].map(operation => {
|
2017-02-02 09:52:47 +01:00
|
|
|
|
|
2017-02-02 10:39:33 +01:00
|
|
|
|
addCallback(`${operation}.async`, function OperationTracking(...args) {
|
|
|
|
|
|
|
|
|
|
const [document, currentUser, ...rest] = args; // eslint-disable-line no-unused-vars
|
|
|
|
|
|
|
|
|
|
return requestAnalyticsAsync(operation, document, currentUser);
|
2017-02-02 09:52:47 +01:00
|
|
|
|
});
|
2017-02-02 10:39:33 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// identify profile completion
|
|
|
|
|
addCallback(`users.profileCompleted.async`, function ProfileCompletedTracking(user) {
|
|
|
|
|
return requestAnalyticsAsync('users.profileCompleted', user);
|
|
|
|
|
});
|
2017-02-02 09:52:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * @summary Increase the number of clicks on a post
|
|
|
|
|
// * @param {string} postId – the ID of the post being edited
|
|
|
|
|
// * @param {string} ip – the IP of the current user
|
|
|
|
|
// */
|
|
|
|
|
Posts.increaseClicks = (postId, ip) => {
|
|
|
|
|
const clickEvent = {
|
|
|
|
|
name: 'click',
|
|
|
|
|
properties: {
|
|
|
|
|
postId: postId,
|
|
|
|
|
ip: ip
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// make sure this IP hasn't previously clicked on this post
|
|
|
|
|
const existingClickEvent = Events.findOne({name: 'click', 'properties.postId': postId, 'properties.ip': ip});
|
|
|
|
|
|
|
|
|
|
if(!existingClickEvent) {
|
|
|
|
|
Events.log(clickEvent);
|
2017-02-11 13:12:17 +09:00
|
|
|
|
return Posts.update(postId, { $inc: { clickCount: 1 }}, {validate: false, bypassCollection2:true});
|
2017-02-02 09:52:47 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// track links clicked, locally in Events collection
|
|
|
|
|
// note: this event is not sent to segment cause we cannot access the current user
|
|
|
|
|
// in our server-side route /out -> sending an event would create a new anonymous
|
|
|
|
|
// user: the free limit of 1,000 unique users per month would be reached quickly
|
|
|
|
|
addCallback(`posts.click.async`, function PostsClickTracking(postId, ip) {
|
|
|
|
|
return Posts.increaseClicks(postId, ip);
|
|
|
|
|
});
|