Skip to content
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

allow callback in time_event to modify tracked time #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 29 additions & 7 deletions build/mixpanel.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,7 @@ define(function () { 'use strict';
/** @const */ var ALIAS_ID_KEY = '__alias';
/** @const */ var CAMPAIGN_IDS_KEY = '__cmpns';
/** @const */ var EVENT_TIMERS_KEY = '__timers';
/** @const */ var EVENT_TIMERS_KEY_CALLBACK = '__timers_callback';
/** @const */ var RESERVED_PROPERTIES = [
SET_QUEUE_KEY,
SET_ONCE_QUEUE_KEY,
Expand All @@ -2741,7 +2742,8 @@ define(function () { 'use strict';
PEOPLE_DISTINCT_ID_KEY,
ALIAS_ID_KEY,
CAMPAIGN_IDS_KEY,
EVENT_TIMERS_KEY
EVENT_TIMERS_KEY,
EVENT_TIMERS_KEY_CALLBACK
];

/*
Expand Down Expand Up @@ -3354,10 +3356,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['props'][EVENT_TIMERS_KEY_CALLBACK] || {};
callbacks[event_name] = callback;
this['props'][EVENT_TIMERS_KEY_CALLBACK] = callbacks;
}
this.save();
};

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

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

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3812,10 +3828,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 +4102,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 +4112,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
36 changes: 29 additions & 7 deletions build/mixpanel.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,7 @@ var INIT_SNIPPET = 1;
/** @const */ var ALIAS_ID_KEY = '__alias';
/** @const */ var CAMPAIGN_IDS_KEY = '__cmpns';
/** @const */ var EVENT_TIMERS_KEY = '__timers';
/** @const */ var EVENT_TIMERS_KEY_CALLBACK = '__timers_callback';
/** @const */ var RESERVED_PROPERTIES = [
SET_QUEUE_KEY,
SET_ONCE_QUEUE_KEY,
Expand All @@ -2741,7 +2742,8 @@ var INIT_SNIPPET = 1;
PEOPLE_DISTINCT_ID_KEY,
ALIAS_ID_KEY,
CAMPAIGN_IDS_KEY,
EVENT_TIMERS_KEY
EVENT_TIMERS_KEY,
EVENT_TIMERS_KEY_CALLBACK
];

/*
Expand Down Expand Up @@ -3354,10 +3356,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['props'][EVENT_TIMERS_KEY_CALLBACK] || {};
callbacks[event_name] = callback;
this['props'][EVENT_TIMERS_KEY_CALLBACK] = callbacks;
}
this.save();
};

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

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

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3812,10 +3828,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 +4102,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 +4112,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
36 changes: 29 additions & 7 deletions build/mixpanel.globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,7 @@
/** @const */ var ALIAS_ID_KEY = '__alias';
/** @const */ var CAMPAIGN_IDS_KEY = '__cmpns';
/** @const */ var EVENT_TIMERS_KEY = '__timers';
/** @const */ var EVENT_TIMERS_KEY_CALLBACK = '__timers_callback';
/** @const */ var RESERVED_PROPERTIES = [
SET_QUEUE_KEY,
SET_ONCE_QUEUE_KEY,
Expand All @@ -2742,7 +2743,8 @@
PEOPLE_DISTINCT_ID_KEY,
ALIAS_ID_KEY,
CAMPAIGN_IDS_KEY,
EVENT_TIMERS_KEY
EVENT_TIMERS_KEY,
EVENT_TIMERS_KEY_CALLBACK
];

/*
Expand Down Expand Up @@ -3355,10 +3357,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['props'][EVENT_TIMERS_KEY_CALLBACK] || {};
callbacks[event_name] = callback;
this['props'][EVENT_TIMERS_KEY_CALLBACK] = callbacks;
}
this.save();
};

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

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

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3813,10 +3829,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 +4103,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 +4113,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
36 changes: 29 additions & 7 deletions build/mixpanel.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,7 @@
/** @const */ var ALIAS_ID_KEY = '__alias';
/** @const */ var CAMPAIGN_IDS_KEY = '__cmpns';
/** @const */ var EVENT_TIMERS_KEY = '__timers';
/** @const */ var EVENT_TIMERS_KEY_CALLBACK = '__timers_callback';
/** @const */ var RESERVED_PROPERTIES = [
SET_QUEUE_KEY,
SET_ONCE_QUEUE_KEY,
Expand All @@ -2745,7 +2746,8 @@
PEOPLE_DISTINCT_ID_KEY,
ALIAS_ID_KEY,
CAMPAIGN_IDS_KEY,
EVENT_TIMERS_KEY
EVENT_TIMERS_KEY,
EVENT_TIMERS_KEY_CALLBACK
];

/*
Expand Down Expand Up @@ -3358,10 +3360,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['props'][EVENT_TIMERS_KEY_CALLBACK] || {};
callbacks[event_name] = callback;
this['props'][EVENT_TIMERS_KEY_CALLBACK] = callbacks;
}
this.save();
};

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

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

return [timestamp, callback];
};

/**
Expand Down Expand Up @@ -3816,10 +3832,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 +4106,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 +4116,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
Loading