Skip to content

allow callback in time_event to modify tracked time #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions build/mixpanel.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3354,10 +3354,16 @@ define(function () { 'use strict';
return this['props'][key] || (this['props'][key] = default_val);
};

MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp) {
MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp, callback) {
var timers = this['props'][EVENT_TIMERS_KEY] || {};
timers[event_name] = timestamp;
this['props'][EVENT_TIMERS_KEY] = timers;

if (callback && (typeof(callback) === 'function')) {
var callbacks = this['event_timer_callbacks'] || {};
callbacks[event_name] = callback;
this['event_timer_callbacks'] = callbacks;
}
this.save();
};

Expand All @@ -3368,7 +3374,15 @@ define(function () { 'use strict';
delete this['props'][EVENT_TIMERS_KEY][event_name];
this.save();
}
return timestamp;

var callbacks = this['event_timer_callbacks'] || {};
var callback = callbacks[event_name];
if (!_.isUndefined(callback)) {
delete this['event_timer_callbacks'];
this.save;
}

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3812,10 +3826,16 @@ define(function () { 'use strict';
properties['token'] = this.get_config('token');

// set $duration if time_event was previously called for this event
var start_timestamp = this['persistence'].remove_event_timer(event_name);
var start_timestamp_with_callback = this['persistence'].remove_event_timer(event_name);
var start_timestamp = start_timestamp_with_callback[0];
var timer_callback = start_timestamp_with_callback[1];
if (!_.isUndefined(start_timestamp)) {
var duration_in_ms = new Date().getTime() - start_timestamp;
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3));
var end_timestamp = parseFloat((duration_in_ms / 1000).toFixed(3));
if (!_.isUndefined(timer_callback)) {
end_timestamp = timer_callback(end_timestamp);
}
properties['$duration'] = end_timestamp;
}

// update persistence
Expand Down Expand Up @@ -4080,7 +4100,7 @@ define(function () { 'use strict';
*
* @param {String} event_name The name of the event.
*/
MixpanelLib.prototype.time_event = function(event_name) {
MixpanelLib.prototype.time_event = function(event_name, callback) {
if (_.isUndefined(event_name)) {
console$1.error('No event name provided to mixpanel.time_event');
return;
Expand All @@ -4090,7 +4110,7 @@ define(function () { 'use strict';
return;
}

this['persistence'].set_event_timer(event_name, new Date().getTime());
this['persistence'].set_event_timer(event_name, new Date().getTime(), callback);
};

/**
Expand Down
32 changes: 26 additions & 6 deletions build/mixpanel.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3354,10 +3354,16 @@ MixpanelPersistence.prototype._get_or_create_queue = function(queue, default_val
return this['props'][key] || (this['props'][key] = default_val);
};

MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp) {
MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp, callback) {
var timers = this['props'][EVENT_TIMERS_KEY] || {};
timers[event_name] = timestamp;
this['props'][EVENT_TIMERS_KEY] = timers;

if (callback && (typeof(callback) === 'function')) {
var callbacks = this['event_timer_callbacks'] || {};
callbacks[event_name] = callback;
this['event_timer_callbacks'] = callbacks;
}
this.save();
};

Expand All @@ -3368,7 +3374,15 @@ MixpanelPersistence.prototype.remove_event_timer = function(event_name) {
delete this['props'][EVENT_TIMERS_KEY][event_name];
this.save();
}
return timestamp;

var callbacks = this['event_timer_callbacks'] || {};
var callback = callbacks[event_name];
if (!_.isUndefined(callback)) {
delete this['event_timer_callbacks'];
this.save;
}

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3812,10 +3826,16 @@ MixpanelLib.prototype.track = addOptOutCheckMixpanelLib(function(event_name, pro
properties['token'] = this.get_config('token');

// set $duration if time_event was previously called for this event
var start_timestamp = this['persistence'].remove_event_timer(event_name);
var start_timestamp_with_callback = this['persistence'].remove_event_timer(event_name);
var start_timestamp = start_timestamp_with_callback[0];
var timer_callback = start_timestamp_with_callback[1];
if (!_.isUndefined(start_timestamp)) {
var duration_in_ms = new Date().getTime() - start_timestamp;
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3));
var end_timestamp = parseFloat((duration_in_ms / 1000).toFixed(3));
if (!_.isUndefined(timer_callback)) {
end_timestamp = timer_callback(end_timestamp);
}
properties['$duration'] = end_timestamp;
}

// update persistence
Expand Down Expand Up @@ -4080,7 +4100,7 @@ MixpanelLib.prototype.track_forms = function() {
*
* @param {String} event_name The name of the event.
*/
MixpanelLib.prototype.time_event = function(event_name) {
MixpanelLib.prototype.time_event = function(event_name, callback) {
if (_.isUndefined(event_name)) {
console$1.error('No event name provided to mixpanel.time_event');
return;
Expand All @@ -4090,7 +4110,7 @@ MixpanelLib.prototype.time_event = function(event_name) {
return;
}

this['persistence'].set_event_timer(event_name, new Date().getTime());
this['persistence'].set_event_timer(event_name, new Date().getTime(), callback);
};

/**
Expand Down
32 changes: 26 additions & 6 deletions build/mixpanel.globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3355,10 +3355,16 @@
return this['props'][key] || (this['props'][key] = default_val);
};

MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp) {
MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp, callback) {
var timers = this['props'][EVENT_TIMERS_KEY] || {};
timers[event_name] = timestamp;
this['props'][EVENT_TIMERS_KEY] = timers;

if (callback && (typeof(callback) === 'function')) {
var callbacks = this['event_timer_callbacks'] || {};
callbacks[event_name] = callback;
this['event_timer_callbacks'] = callbacks;
}
this.save();
};

Expand All @@ -3369,7 +3375,15 @@
delete this['props'][EVENT_TIMERS_KEY][event_name];
this.save();
}
return timestamp;

var callbacks = this['event_timer_callbacks'] || {};
var callback = callbacks[event_name];
if (!_.isUndefined(callback)) {
delete this['event_timer_callbacks'];
this.save;
}

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3813,10 +3827,16 @@
properties['token'] = this.get_config('token');

// set $duration if time_event was previously called for this event
var start_timestamp = this['persistence'].remove_event_timer(event_name);
var start_timestamp_with_callback = this['persistence'].remove_event_timer(event_name);
var start_timestamp = start_timestamp_with_callback[0];
var timer_callback = start_timestamp_with_callback[1];
if (!_.isUndefined(start_timestamp)) {
var duration_in_ms = new Date().getTime() - start_timestamp;
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3));
var end_timestamp = parseFloat((duration_in_ms / 1000).toFixed(3));
if (!_.isUndefined(timer_callback)) {
end_timestamp = timer_callback(end_timestamp);
}
properties['$duration'] = end_timestamp;
}

// update persistence
Expand Down Expand Up @@ -4081,7 +4101,7 @@
*
* @param {String} event_name The name of the event.
*/
MixpanelLib.prototype.time_event = function(event_name) {
MixpanelLib.prototype.time_event = function(event_name, callback) {
if (_.isUndefined(event_name)) {
console$1.error('No event name provided to mixpanel.time_event');
return;
Expand All @@ -4091,7 +4111,7 @@
return;
}

this['persistence'].set_event_timer(event_name, new Date().getTime());
this['persistence'].set_event_timer(event_name, new Date().getTime(), callback);
};

/**
Expand Down
32 changes: 26 additions & 6 deletions build/mixpanel.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3358,10 +3358,16 @@
return this['props'][key] || (this['props'][key] = default_val);
};

MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp) {
MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp, callback) {
var timers = this['props'][EVENT_TIMERS_KEY] || {};
timers[event_name] = timestamp;
this['props'][EVENT_TIMERS_KEY] = timers;

if (callback && (typeof(callback) === 'function')) {
var callbacks = this['event_timer_callbacks'] || {};
callbacks[event_name] = callback;
this['event_timer_callbacks'] = callbacks;
}
this.save();
};

Expand All @@ -3372,7 +3378,15 @@
delete this['props'][EVENT_TIMERS_KEY][event_name];
this.save();
}
return timestamp;

var callbacks = this['event_timer_callbacks'] || {};
var callback = callbacks[event_name];
if (!_.isUndefined(callback)) {
delete this['event_timer_callbacks'];
this.save;
}

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3816,10 +3830,16 @@
properties['token'] = this.get_config('token');

// set $duration if time_event was previously called for this event
var start_timestamp = this['persistence'].remove_event_timer(event_name);
var start_timestamp_with_callback = this['persistence'].remove_event_timer(event_name);
var start_timestamp = start_timestamp_with_callback[0];
var timer_callback = start_timestamp_with_callback[1];
if (!_.isUndefined(start_timestamp)) {
var duration_in_ms = new Date().getTime() - start_timestamp;
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3));
var end_timestamp = parseFloat((duration_in_ms / 1000).toFixed(3));
if (!_.isUndefined(timer_callback)) {
end_timestamp = timer_callback(end_timestamp);
}
properties['$duration'] = end_timestamp;
}

// update persistence
Expand Down Expand Up @@ -4084,7 +4104,7 @@
*
* @param {String} event_name The name of the event.
*/
MixpanelLib.prototype.time_event = function(event_name) {
MixpanelLib.prototype.time_event = function(event_name, callback) {
if (_.isUndefined(event_name)) {
console$1.error('No event name provided to mixpanel.time_event');
return;
Expand All @@ -4094,7 +4114,7 @@
return;
}

this['persistence'].set_event_timer(event_name, new Date().getTime());
this['persistence'].set_event_timer(event_name, new Date().getTime(), callback);
};

/**
Expand Down
32 changes: 26 additions & 6 deletions examples/commonjs-browserify/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3355,10 +3355,16 @@ MixpanelPersistence.prototype._get_or_create_queue = function(queue, default_val
return this['props'][key] || (this['props'][key] = default_val);
};

MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp) {
MixpanelPersistence.prototype.set_event_timer = function(event_name, timestamp, callback) {
var timers = this['props'][EVENT_TIMERS_KEY] || {};
timers[event_name] = timestamp;
this['props'][EVENT_TIMERS_KEY] = timers;

if (callback && (typeof(callback) === 'function')) {
var callbacks = this['event_timer_callbacks'] || {};
callbacks[event_name] = callback;
this['event_timer_callbacks'] = callbacks;
}
this.save();
};

Expand All @@ -3369,7 +3375,15 @@ MixpanelPersistence.prototype.remove_event_timer = function(event_name) {
delete this['props'][EVENT_TIMERS_KEY][event_name];
this.save();
}
return timestamp;

var callbacks = this['event_timer_callbacks'] || {};
var callback = callbacks[event_name];
if (!_.isUndefined(callback)) {
delete this['event_timer_callbacks'];
this.save;
}

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3813,10 +3827,16 @@ MixpanelLib.prototype.track = addOptOutCheckMixpanelLib(function(event_name, pro
properties['token'] = this.get_config('token');

// set $duration if time_event was previously called for this event
var start_timestamp = this['persistence'].remove_event_timer(event_name);
var start_timestamp_with_callback = this['persistence'].remove_event_timer(event_name);
var start_timestamp = start_timestamp_with_callback[0];
var timer_callback = start_timestamp_with_callback[1];
if (!_.isUndefined(start_timestamp)) {
var duration_in_ms = new Date().getTime() - start_timestamp;
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3));
var end_timestamp = parseFloat((duration_in_ms / 1000).toFixed(3));
if (!_.isUndefined(timer_callback)) {
end_timestamp = timer_callback(end_timestamp);
}
properties['$duration'] = end_timestamp;
}

// update persistence
Expand Down Expand Up @@ -4081,7 +4101,7 @@ MixpanelLib.prototype.track_forms = function() {
*
* @param {String} event_name The name of the event.
*/
MixpanelLib.prototype.time_event = function(event_name) {
MixpanelLib.prototype.time_event = function(event_name, callback) {
if (_.isUndefined(event_name)) {
console$1.error('No event name provided to mixpanel.time_event');
return;
Expand All @@ -4091,7 +4111,7 @@ MixpanelLib.prototype.time_event = function(event_name) {
return;
}

this['persistence'].set_event_timer(event_name, new Date().getTime());
this['persistence'].set_event_timer(event_name, new Date().getTime(), callback);
};

/**
Expand Down
Loading