Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

* Add support for passing callback functions to logEvent

## 2.3.0 (September 2, 2015)

* Add option to batch events into a single request.

## 2.2.1 (Aug 13, 2015)
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,20 @@ User IDs are automatically generated and stored in cookies if not specified.
Device IDs are generated randomly, although you can define a custom device ID setting it as a configuration option or by calling:

amplitude.setDeviceId("CUSTOM_DEVICE_ID");

You can pass a callback function to logEvent, which will get called after receiving a response from the server:

amplitude.logEvent("EVENT_IDENTIFIER_HERE", null, callback_function);

The status and response from the server are passed to the callback function, which you might find useful. An example of a callback function which redirects the browser to another site after a response:

```javascript
var callback_function = function(status, response) {
if (status === 200 && response === 'success') {
// do something here
}
window.location.replace('URL_OF_OTHER_SITE');
};
```

In the case that `optOut` is true, then no event will be logged, but the callback will be called. In the case that `batchEvents` is true, if the batch requirements `eventUploadThreshold` and `eventUploadPeriodMillis` are not met when `logEvent` is called, then no request is sent, but the callback is still called. In these cases, the callback will be called with an input status of 0 and response 'No request sent'.
47 changes: 33 additions & 14 deletions amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,24 @@ Amplitude.prototype.nextEventId = function() {
return this._eventId;
};

Amplitude.prototype._sendEventsIfReady = function() {
// returns true if sendEvents called immediately
Amplitude.prototype._sendEventsIfReady = function(callback) {
if (this._unsentEvents.length === 0) {
return;
return false;
}

if (!this.options.batchEvents) {
this.sendEvents();
return;
this.sendEvents(callback);
return true;
}

if (this._unsentEvents.length >= this.options.eventUploadThreshold) {
this.sendEvents();
} else {
setTimeout(this.sendEvents.bind(this), this.options.eventUploadPeriodMillis);
this.sendEvents(callback);
return true;
}

setTimeout(this.sendEvents.bind(this), this.options.eventUploadPeriodMillis);
return false;
};

var _loadCookieData = function(scope) {
Expand Down Expand Up @@ -431,8 +434,15 @@ Amplitude.prototype.setVersionName = function(versionName) {
/**
* Private logEvent method. Keeps apiProperties from being publicly exposed.
*/
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties) {
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties, callback) {
if (typeof callback !== 'function') {
callback = null;
}

if (!eventType || this.options.optOut) {
if (callback) {
callback(0, 'No request sent');
}
return;
}
try {
Expand Down Expand Up @@ -501,16 +511,18 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
this.saveEvents();
}

this._sendEventsIfReady();
if (!this._sendEventsIfReady(callback) && callback) {
callback(0, 'No request sent');
}

return eventId;
} catch (e) {
log(e);
}
};

Amplitude.prototype.logEvent = function(eventType, eventProperties) {
return this._logEvent(eventType, eventProperties);
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no method overloading in javascript. just keep this version of logEvent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return this._logEvent(eventType, eventProperties, null, callback);
};

// Test that n is a number or a numeric value.
Expand Down Expand Up @@ -547,7 +559,7 @@ Amplitude.prototype.removeEvents = function (maxEventId) {
this._unsentEvents = filteredEvents;
};

Amplitude.prototype.sendEvents = function() {
Amplitude.prototype.sendEvents = function(callback) {
if (!this._sending && !this.options.optOut && this._unsentEvents.length > 0) {
this._sending = true;
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' +
Expand Down Expand Up @@ -581,7 +593,9 @@ Amplitude.prototype.sendEvents = function() {
}

// Send more events if any queued during previous send.
scope._sendEventsIfReady();
if (!scope._sendEventsIfReady(callback) && callback) {
callback(status, response);
}

} else if (status === 413) {
//log('request too large');
Expand All @@ -593,12 +607,17 @@ Amplitude.prototype.sendEvents = function() {
// The server complained about the length of the request.
// Backoff and try again.
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
scope.sendEvents();
scope.sendEvents(callback);

} else if (callback) { // If server turns something like a 400
callback(status, response);
}
} catch (e) {
//log('failed upload');
}
});
} else if (callback) {
callback(0, 'No request sent');
}
};

Expand Down
4 changes: 2 additions & 2 deletions amplitude.min.js

Large diffs are not rendered by default.

47 changes: 33 additions & 14 deletions src/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,24 @@ Amplitude.prototype.nextEventId = function() {
return this._eventId;
};

Amplitude.prototype._sendEventsIfReady = function() {
// returns true if sendEvents called immediately
Amplitude.prototype._sendEventsIfReady = function(callback) {
if (this._unsentEvents.length === 0) {
return;
return false;
}

if (!this.options.batchEvents) {
this.sendEvents();
return;
this.sendEvents(callback);
return true;
}

if (this._unsentEvents.length >= this.options.eventUploadThreshold) {
this.sendEvents();
} else {
setTimeout(this.sendEvents.bind(this), this.options.eventUploadPeriodMillis);
this.sendEvents(callback);
return true;
}

setTimeout(this.sendEvents.bind(this), this.options.eventUploadPeriodMillis);
return false;
};

var _loadCookieData = function(scope) {
Expand Down Expand Up @@ -319,8 +322,15 @@ Amplitude.prototype.setVersionName = function(versionName) {
/**
* Private logEvent method. Keeps apiProperties from being publicly exposed.
*/
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties) {
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties, callback) {
if (typeof callback !== 'function') {
callback = null;
}

if (!eventType || this.options.optOut) {
if (callback) {
callback(0, 'No request sent');
}
return;
}
try {
Expand Down Expand Up @@ -389,16 +399,18 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
this.saveEvents();
}

this._sendEventsIfReady();
if (!this._sendEventsIfReady(callback) && callback) {
callback(0, 'No request sent');
}

return eventId;
} catch (e) {
log(e);
}
};

Amplitude.prototype.logEvent = function(eventType, eventProperties) {
return this._logEvent(eventType, eventProperties);
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
return this._logEvent(eventType, eventProperties, null, callback);
};

// Test that n is a number or a numeric value.
Expand Down Expand Up @@ -435,7 +447,7 @@ Amplitude.prototype.removeEvents = function (maxEventId) {
this._unsentEvents = filteredEvents;
};

Amplitude.prototype.sendEvents = function() {
Amplitude.prototype.sendEvents = function(callback) {
if (!this._sending && !this.options.optOut && this._unsentEvents.length > 0) {
this._sending = true;
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' +
Expand Down Expand Up @@ -469,7 +481,9 @@ Amplitude.prototype.sendEvents = function() {
}

// Send more events if any queued during previous send.
scope._sendEventsIfReady();
if (!scope._sendEventsIfReady(callback) && callback) {
callback(status, response);
}

} else if (status === 413) {
//log('request too large');
Expand All @@ -481,12 +495,17 @@ Amplitude.prototype.sendEvents = function() {
// The server complained about the length of the request.
// Backoff and try again.
scope.options.uploadBatchSize = Math.ceil(numEvents / 2);
scope.sendEvents();
scope.sendEvents(callback);

} else if (callback) { // If server turns something like a 400
callback(status, response);
}
} catch (e) {
//log('failed upload');
}
});
} else if (callback) {
callback(0, 'No request sent');
}
};

Expand Down
Loading