Hello,
I need to send events to Segment and I used this article as guidance Export Log Events with Rules
However, I encountered a few issues and need experts help.
This is my rule
function(user, context, callback) {
var Analytics = require('analytics-node');
var analytics = new Analytics(configuration.SEGMENT_WRITE_KEY, { flushAt: 1 });
// Note: Set { flushAt: 1 } and use analytics.flush to ensure
// the data is sent to Segment before the rule/Webtask terminates
// Identify your user
analytics.identify({
userId: user.user_id,
traits: {
//email: user.email,
signed_up: user.created_at,
login_count: context.stats.loginsCount
},
"context": {
"userAgent": context.request.UserAgent,
"ip": context.request.ip
}
});
var event = (context.stats.loginsCount > 1) ? 'Log In' : 'Sign Up';
analytics.track({
userId: user.user_id,
event: event,
properties: {
clientName: context.clientName,
clientID: context.clientID,
connection: context.connection
},
"context": {
"userAgent": context.request.UserAgent,
"ip": context.request.ip
}
});
analytics.flush(function(err, batch){
callback(null, user, context);
});
}
The first problem is that in documentation I found two ways of getting user logins count: context.stats.loginsCount and user.logins_count. What’s the difference? Why the user one does not work?
The second problem is that on each login/signup several events are sent. Seems that even logout sends events.
Appreciate your help.
Thanks.