Skip to content

Commit bb3afe8

Browse files
author
Daniel Jih
committed
add tests and update readme
1 parent 1853bde commit bb3afe8

File tree

5 files changed

+82
-6
lines changed

5 files changed

+82
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
* Add `logEventWithTimestamp` to allow logging events with a custom timestamp. The timestamp should a number representing the time in milliseconds since epoch. See [documentation](https://rawgit.com/amplitude/Amplitude-Javascript/master/documentation/AmplitudeClient.html) for more details.
4+
5+
### 3.3.2 (October 28, 2016)
6+
37
* Updated our [UA-parser-js](https://github.com/amplitude/ua-parser-js) fork to properly parse the version number for Chrome Mobile browsers.
48

59
### 3.3.1 (October 26, 2016)

amplitude.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
12801280
eventId = this.nextEventId();
12811281
}
12821282
var sequenceNumber = this.nextSequenceNumber();
1283-
var eventTime = timestamp || new Date().getTime();
1283+
var eventTime = (type(timestamp) === 'number') ? timestamp : new Date().getTime();
12841284
if (!this._sessionId || !this._lastEventTime || eventTime - this._lastEventTime > this.options.sessionTimeout) {
12851285
this._sessionId = eventTime;
12861286
}
@@ -1368,14 +1368,28 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue
13681368
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
13691369
*/
13701370
AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventProperties, opt_callback) {
1371+
return this.logEventWithTimestamp(eventType, eventProperties, null, opt_callback);
1372+
};
1373+
1374+
/**
1375+
* Log an event with eventType and eventProperties and a custom timestamp
1376+
* @public
1377+
* @param {string} eventType - name of event
1378+
* @param {object} eventProperties - (optional) an object with string keys and values for the event properties.
1379+
* @param {number} timesatmp - (optional) the custom timestamp as milliseconds since epoch.
1380+
* @param {Amplitude~eventCallback} opt_callback - (optional) a callback function to run after the event is logged.
1381+
* Note: the server response code and response body from the event upload are passed to the callback function.
1382+
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
1383+
*/
1384+
AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) {
13711385
if (!this._apiKeySet('logEvent()') || !utils.validateInput(eventType, 'eventType', 'string') ||
13721386
utils.isEmptyString(eventType)) {
13731387
if (type(opt_callback) === 'function') {
13741388
opt_callback(0, 'No request sent');
13751389
}
13761390
return -1;
13771391
}
1378-
return this._logEvent(eventType, eventProperties, null, null, null, null, opt_callback);
1392+
return this._logEvent(eventType, eventProperties, null, null, null, timestamp, opt_callback);
13791393
};
13801394

13811395
/**

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
799799
eventId = this.nextEventId();
800800
}
801801
var sequenceNumber = this.nextSequenceNumber();
802-
var eventTime = timestamp || new Date().getTime();
802+
var eventTime = (type(timestamp) === 'number') ? timestamp : new Date().getTime();
803803
if (!this._sessionId || !this._lastEventTime || eventTime - this._lastEventTime > this.options.sessionTimeout) {
804804
this._sessionId = eventTime;
805805
}
@@ -887,14 +887,28 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue
887887
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
888888
*/
889889
AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventProperties, opt_callback) {
890+
return this.logEventWithTimestamp(eventType, eventProperties, null, opt_callback);
891+
};
892+
893+
/**
894+
* Log an event with eventType and eventProperties and a custom timestamp
895+
* @public
896+
* @param {string} eventType - name of event
897+
* @param {object} eventProperties - (optional) an object with string keys and values for the event properties.
898+
* @param {number} timesatmp - (optional) the custom timestamp as milliseconds since epoch.
899+
* @param {Amplitude~eventCallback} opt_callback - (optional) a callback function to run after the event is logged.
900+
* Note: the server response code and response body from the event upload are passed to the callback function.
901+
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
902+
*/
903+
AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) {
890904
if (!this._apiKeySet('logEvent()') || !utils.validateInput(eventType, 'eventType', 'string') ||
891905
utils.isEmptyString(eventType)) {
892906
if (type(opt_callback) === 'function') {
893907
opt_callback(0, 'No request sent');
894908
}
895909
return -1;
896910
}
897-
return this._logEvent(eventType, eventProperties, null, null, null, null, opt_callback);
911+
return this._logEvent(eventType, eventProperties, null, null, null, timestamp, opt_callback);
898912
};
899913

900914
/**

test/amplitude-client.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,50 @@ describe('setVersionName', function() {
20492049
assert.equal(events[0].event_type, 'testEvent');
20502050
assert.isTrue(events[0].user_agent.indexOf(phantomJSUA) > -1);
20512051
});
2052+
2053+
it('should allow logging event with custom timestamp', function() {
2054+
var timestamp = 2000;
2055+
amplitude.logEventWithTimestamp('test', null, timestamp, null);
2056+
assert.lengthOf(server.requests, 1);
2057+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
2058+
assert.lengthOf(events, 1);
2059+
2060+
// verify the event is correct
2061+
var event = events[0];
2062+
assert.equal(event.event_type, 'test');
2063+
assert.equal(event.event_id, 1);
2064+
assert.equal(event.timestamp, timestamp);
2065+
});
2066+
2067+
it('should use current time if timestamp is null', function() {
2068+
var timestamp = 5000;
2069+
clock.tick(timestamp);
2070+
amplitude.logEventWithTimestamp('test', null, null, null);
2071+
assert.lengthOf(server.requests, 1);
2072+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
2073+
assert.lengthOf(events, 1);
2074+
2075+
// verify the event is correct
2076+
var event = events[0];
2077+
assert.equal(event.event_type, 'test');
2078+
assert.equal(event.event_id, 1);
2079+
assert.isTrue(event.timestamp >= timestamp);
2080+
});
2081+
2082+
it('should use current time if timestamp is not valid form', function() {
2083+
var timestamp = 6000;
2084+
clock.tick(timestamp);
2085+
amplitude.logEventWithTimestamp('test', null, 'invalid', null);
2086+
assert.lengthOf(server.requests, 1);
2087+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
2088+
assert.lengthOf(events, 1);
2089+
2090+
// verify the event is correct
2091+
var event = events[0];
2092+
assert.equal(event.event_type, 'test');
2093+
assert.equal(event.event_id, 1);
2094+
assert.isTrue(event.timestamp >= timestamp);
2095+
});
20522096
});
20532097

20542098
describe('optOut', function() {

0 commit comments

Comments
 (0)