Skip to content

Commit 5a0ed4c

Browse files
committed
When adding function call into this._q, send all the arguments by .slice.call(arguments) and add additional tests to verify that existing users (e.g. users with an amplitude cookie) will continue to be logged
1 parent 9ea307c commit 5a0ed4c

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/amplitude-client.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ AmplitudeClient.prototype.saveEvents = function saveEvents() {
773773
*/
774774
AmplitudeClient.prototype.setDomain = function setDomain(domain) {
775775
if (this._shouldDeferCall()) {
776-
return this._q.push(['setDomain', domain]);
776+
return this._q.push(['setDomain'].concat(Array.prototype.slice.call(arguments, 0)));
777777
}
778778

779779
if (!utils.validateInput(domain, 'domain', 'string')) {
@@ -802,7 +802,7 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) {
802802
*/
803803
AmplitudeClient.prototype.setUserId = function setUserId(userId) {
804804
if (this._shouldDeferCall()) {
805-
return this._q.push(['setUserId', userId]);
805+
return this._q.push(['setUserId'].concat(Array.prototype.slice.call(arguments, 0)));
806806
}
807807

808808
try {
@@ -828,7 +828,7 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId) {
828828
*/
829829
AmplitudeClient.prototype.setGroup = function(groupType, groupName) {
830830
if (this._shouldDeferCall()) {
831-
return this._q.push(['setGroup', groupType, groupName]);
831+
return this._q.push(['setGroup'].concat(Array.prototype.slice.call(arguments, 0)));
832832
}
833833

834834
if (!this._apiKeySet('setGroup()') || !utils.validateInput(groupType, 'groupType', 'string') ||
@@ -850,7 +850,7 @@ AmplitudeClient.prototype.setGroup = function(groupType, groupName) {
850850
*/
851851
AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
852852
if (this._shouldDeferCall()) {
853-
return this._q.push(['setOptOut', enable]);
853+
return this._q.push(['setOptOut'].concat(Array.prototype.slice.call(arguments, 0)));
854854
}
855855

856856
if (!utils.validateInput(enable, 'enable', 'boolean')) {
@@ -891,7 +891,7 @@ AmplitudeClient.prototype.resetSessionId = function resetSessionId() {
891891
*/
892892
AmplitudeClient.prototype.regenerateDeviceId = function regenerateDeviceId() {
893893
if (this._shouldDeferCall()) {
894-
return this._q.push(['regenerateDeviceId']);
894+
return this._q.push(['regenerateDeviceId'].concat(Array.prototype.slice.call(arguments, 0)));
895895
}
896896

897897
this.setDeviceId(UUID() + 'R');
@@ -907,7 +907,7 @@ AmplitudeClient.prototype.regenerateDeviceId = function regenerateDeviceId() {
907907
*/
908908
AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
909909
if (this._shouldDeferCall()) {
910-
return this._q.push(['setDeviceId', deviceId]);
910+
return this._q.push(['setDeviceId'].concat(Array.prototype.slice.call(arguments, 0)));
911911
}
912912

913913
if (!utils.validateInput(deviceId, 'deviceId', 'string')) {
@@ -934,7 +934,7 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
934934
*/
935935
AmplitudeClient.prototype.setUserProperties = function setUserProperties(userProperties) {
936936
if (this._shouldDeferCall()) {
937-
return this._q.push(['setUserProperties', userProperties]);
937+
return this._q.push(['setUserProperties'].concat(Array.prototype.slice.call(arguments, 0)));
938938
}
939939
if (!this._apiKeySet('setUserProperties()') || !utils.validateInput(userProperties, 'userProperties', 'object')) {
940940
return;
@@ -962,7 +962,7 @@ AmplitudeClient.prototype.setUserProperties = function setUserProperties(userPro
962962
*/
963963
AmplitudeClient.prototype.clearUserProperties = function clearUserProperties(){
964964
if (this._shouldDeferCall()) {
965-
return this._q.push(['clearUserProperties']);
965+
return this._q.push(['clearUserProperties'].concat(Array.prototype.slice.call(arguments, 0)));
966966
}
967967

968968
if (!this._apiKeySet('clearUserProperties()')) {
@@ -1002,7 +1002,7 @@ var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(i
10021002
*/
10031003
AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
10041004
if (this._shouldDeferCall()) {
1005-
return this._q.push(['identify', identify_obj, opt_callback]);
1005+
return this._q.push(['identify'].concat(Array.prototype.slice.call(arguments, 0)));
10061006
}
10071007
if (!this._apiKeySet('identify()')) {
10081008
if (type(opt_callback) === 'function') {
@@ -1037,7 +1037,7 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
10371037

10381038
AmplitudeClient.prototype.groupIdentify = function(group_type, group_name, identify_obj, opt_callback) {
10391039
if (this._shouldDeferCall()) {
1040-
return this._q.push(['groupIdentify', group_type, group_name, identify_obj, opt_callback]);
1040+
return this._q.push(['groupIdentify'].concat(Array.prototype.slice.call(arguments, 0)));
10411041
}
10421042
if (!this._apiKeySet('groupIdentify()')) {
10431043
if (type(opt_callback) === 'function') {
@@ -1093,7 +1093,7 @@ AmplitudeClient.prototype.groupIdentify = function(group_type, group_name, ident
10931093
*/
10941094
AmplitudeClient.prototype.setVersionName = function setVersionName(versionName) {
10951095
if (this._shouldDeferCall()) {
1096-
return this._q.push(['setVersionName', versionName]);
1096+
return this._q.push(['setVersionName'].concat(Array.prototype.slice.call(arguments, 0)));
10971097
}
10981098

10991099
if (!utils.validateInput(versionName, 'versionName', 'string')) {
@@ -1256,7 +1256,7 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue
12561256
*/
12571257
AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventProperties, opt_callback) {
12581258
if (this._shouldDeferCall()) {
1259-
return this._q.push(['logEvent', eventType, eventProperties, opt_callback]);
1259+
return this._q.push(['logEvent'].concat(Array.prototype.slice.call(arguments, 0)));
12601260
}
12611261
return this.logEventWithTimestamp(eventType, eventProperties, null, opt_callback);
12621262
};
@@ -1273,7 +1273,7 @@ AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventPropertie
12731273
*/
12741274
AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) {
12751275
if (this._shouldDeferCall()) {
1276-
return this._q.push(['logEventWithTimestamp', eventType, eventProperties, timestamp, opt_callback]);
1276+
return this._q.push(['logEventWithTimestamp'].concat(Array.prototype.slice.call(arguments, 0)));
12771277
}
12781278
if (!this._apiKeySet('logEvent()')) {
12791279
if (type(opt_callback) === 'function') {
@@ -1313,7 +1313,7 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e
13131313
*/
13141314
AmplitudeClient.prototype.logEventWithGroups = function(eventType, eventProperties, groups, opt_callback) {
13151315
if (this._shouldDeferCall()) {
1316-
return this._q.push(['logEventWithGroups', eventType, eventProperties, groups, opt_callback]);
1316+
return this._q.push(['logEventWithGroups'].concat(Array.prototype.slice.call(arguments, 0)));
13171317
}
13181318
if (!this._apiKeySet('logEventWithGroups()')) {
13191319
if (type(opt_callback) === 'function') {
@@ -1350,7 +1350,7 @@ var _isNumber = function _isNumber(n) {
13501350
*/
13511351
AmplitudeClient.prototype.logRevenueV2 = function logRevenueV2(revenue_obj) {
13521352
if (this._shouldDeferCall()) {
1353-
return this._q.push(['logRevenueV2', revenue_obj]);
1353+
return this._q.push(['logRevenueV2'].concat(Array.prototype.slice.call(arguments, 0)));
13541354
}
13551355

13561356
if (!this._apiKeySet('logRevenueV2()')) {
@@ -1384,7 +1384,7 @@ if (BUILD_COMPAT_2_0) {
13841384
*/
13851385
AmplitudeClient.prototype.logRevenue = function logRevenue(price, quantity, product) {
13861386
if (this._shouldDeferCall()) {
1387-
return this._q.push(['logRevenue', price, quantity, product]);
1387+
return this._q.push(['logRevenue'].concat(Array.prototype.slice.call(arguments, 0)));
13881388
}
13891389

13901390
// Test that the parameters are of the right type.
@@ -1611,9 +1611,9 @@ AmplitudeClient.prototype._shouldDeferCall = function _shouldDeferCall() {
16111611
* have accepted terms for tracking
16121612
* @private
16131613
*/
1614-
AmplitudeClient.prototype._deferInitialization = function _deferInitialization(apiKey, opt_userId, opt_config, opt_callback) {
1614+
AmplitudeClient.prototype._deferInitialization = function _deferInitialization() {
16151615
this._initializationDeferred = true;
1616-
this._q.push(['init', apiKey, opt_userId, opt_config, opt_callback]);
1616+
this._q.push(['init'].concat(Array.prototype.slice.call(arguments, 0)));
16171617
};
16181618

16191619
/**

test/amplitude-client.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,12 +3333,30 @@ describe('setVersionName', function() {
33333333
});
33343334

33353335
describe('deferInitialization config', function () {
3336-
beforeEach(function () {
3337-
reset();
3336+
it('should keep tracking users who already have an amplitude cookie', function () {
3337+
var now = new Date().getTime();
3338+
var cookieData = {
3339+
userId: 'test_user_id',
3340+
optOut: false,
3341+
sessionId: now,
3342+
lastEventTime: now,
3343+
eventId: 50,
3344+
identifyId: 60
3345+
}
3346+
3347+
cookie.set(amplitude.options.cookieName + keySuffix, cookieData);
33383348
amplitude.init(apiKey, null, { cookieExpiration: 365, deferInitialization: true });
3339-
});
3349+
amplitude.identify(new Identify().set('prop1', 'value1'));
33403350

3351+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3352+
assert.lengthOf(server.requests, 1, 'should have sent a request to Amplitude');
3353+
assert.equal(events[0].event_type, '$identify');
3354+
});
33413355
describe('prior to opting into analytics', function () {
3356+
beforeEach(function () {
3357+
reset();
3358+
amplitude.init(apiKey, null, { cookieExpiration: 365, deferInitialization: true });
3359+
});
33423360
it('should not initially drop a cookie if deferInitialization is set to true', function () {
33433361
var cookieData = cookie.get(amplitude.options.cookieName + '_' + apiKey);
33443362
assert.isNull(cookieData);
@@ -3370,6 +3388,10 @@ describe('setVersionName', function() {
33703388
});
33713389

33723390
describe('upon opting into analytics', function () {
3391+
beforeEach(function () {
3392+
reset();
3393+
amplitude.init(apiKey, null, { cookieExpiration: 365, deferInitialization: true });
3394+
});
33733395
it('should drop a cookie', function () {
33743396
amplitude.enableTracking();
33753397
var cookieData = cookie.get(amplitude.options.cookieName + '_' + apiKey);
@@ -3411,5 +3433,5 @@ describe('setVersionName', function() {
34113433
assert.lengthOf(events, 1, 'should have sent a request to Amplitude');
34123434
});
34133435
});
3414-
})
3436+
});
34153437
});

0 commit comments

Comments
 (0)