Skip to content

Commit b75154b

Browse files
committed
changed set user properties to use identify call
1 parent 9577212 commit b75154b

File tree

6 files changed

+79
-73
lines changed

6 files changed

+79
-73
lines changed

amplitude.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ var log = function(s) {
126126
console.log('[Amplitude] ' + s);
127127
};
128128

129+
var IDENTIFY_EVENT = '$identify';
129130
var API_VERSION = 2;
130131
var DEFAULT_OPTIONS = {
131132
apiEndpoint: 'api.amplitude.com',
@@ -160,8 +161,6 @@ var Amplitude = function() {
160161
this.options = object.merge({}, DEFAULT_OPTIONS);
161162
};
162163

163-
Amplitude.prototype._test = new Identify();
164-
165164
Amplitude.prototype._eventId = 0;
166165
Amplitude.prototype._sending = false;
167166
Amplitude.prototype._lastEventTime = null;
@@ -410,17 +409,23 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
410409
}
411410
};
412411

413-
Amplitude.prototype.setUserProperties = function(userProperties, opt_replace) {
414-
try {
415-
if (opt_replace) {
416-
this.options.userProperties = userProperties;
417-
} else {
418-
this.options.userProperties = object.merge(this.options.userProperties || {}, userProperties);
412+
Amplitude.prototype.setUserProperties = function(userProperties) {
413+
414+
// convert userProperties into an identify call
415+
var identify = new Identify();
416+
for (var property in userProperties) {
417+
if (userProperties.hasOwnProperty(property)) {
418+
identify.set(property, userProperties[property]);
419419
}
420-
_saveCookieData(this);
421-
//log('set userProperties=' + JSON.stringify(userProperties));
422-
} catch (e) {
423-
log(e);
420+
}
421+
422+
// _saveCookieData(this); ??
423+
this.identify(identify);
424+
};
425+
426+
Amplitude.prototype.identify = function(identify) {
427+
if (identify instanceof Identify) {
428+
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
424429
}
425430
};
426431

@@ -436,7 +441,7 @@ Amplitude.prototype.setVersionName = function(versionName) {
436441
/**
437442
* Private logEvent method. Keeps apiProperties from being publicly exposed.
438443
*/
439-
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties, callback) {
444+
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties, userProperties, callback) {
440445
if (typeof callback !== 'function') {
441446
callback = null;
442447
}
@@ -459,10 +464,8 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
459464
localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime);
460465
localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID, eventId);
461466

462-
var userProperties = {};
463-
object.merge(userProperties, this.options.userProperties || {});
464-
465467
// Add the utm properties, if any, onto the user properties.
468+
userProperties = userProperties || {};
466469
object.merge(userProperties, this._utmProperties);
467470

468471
// Add referral info onto the user properties
@@ -524,7 +527,7 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
524527
};
525528

526529
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
527-
return this._logEvent(eventType, eventProperties, null, callback);
530+
return this._logEvent(eventType, eventProperties, null, null, callback);
528531
};
529532

530533
// Test that n is a number or a numeric value.
@@ -3121,8 +3124,6 @@ module.exports = '2.4.0';
31213124

31223125
}, {}],
31233126
13: [function(require, module, exports) {
3124-
// var querystring = require('querystring');
3125-
31263127
/*
31273128
* Wrapper for a user properties JSON object that supports operations
31283129
*/

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.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var log = function(s) {
1414
console.log('[Amplitude] ' + s);
1515
};
1616

17+
var IDENTIFY_EVENT = '$identify';
1718
var API_VERSION = 2;
1819
var DEFAULT_OPTIONS = {
1920
apiEndpoint: 'api.amplitude.com',
@@ -48,8 +49,6 @@ var Amplitude = function() {
4849
this.options = object.merge({}, DEFAULT_OPTIONS);
4950
};
5051

51-
Amplitude.prototype._test = new Identify();
52-
5352
Amplitude.prototype._eventId = 0;
5453
Amplitude.prototype._sending = false;
5554
Amplitude.prototype._lastEventTime = null;
@@ -298,17 +297,23 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
298297
}
299298
};
300299

301-
Amplitude.prototype.setUserProperties = function(userProperties, opt_replace) {
302-
try {
303-
if (opt_replace) {
304-
this.options.userProperties = userProperties;
305-
} else {
306-
this.options.userProperties = object.merge(this.options.userProperties || {}, userProperties);
300+
Amplitude.prototype.setUserProperties = function(userProperties) {
301+
302+
// convert userProperties into an identify call
303+
var identify = new Identify();
304+
for (var property in userProperties) {
305+
if (userProperties.hasOwnProperty(property)) {
306+
identify.set(property, userProperties[property]);
307307
}
308-
_saveCookieData(this);
309-
//log('set userProperties=' + JSON.stringify(userProperties));
310-
} catch (e) {
311-
log(e);
308+
}
309+
310+
// _saveCookieData(this); ??
311+
this.identify(identify);
312+
};
313+
314+
Amplitude.prototype.identify = function(identify) {
315+
if (identify instanceof Identify) {
316+
this._logEvent(IDENTIFY_EVENT, null, null, identify.userPropertiesOperations);
312317
}
313318
};
314319

@@ -324,7 +329,7 @@ Amplitude.prototype.setVersionName = function(versionName) {
324329
/**
325330
* Private logEvent method. Keeps apiProperties from being publicly exposed.
326331
*/
327-
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties, callback) {
332+
Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperties, userProperties, callback) {
328333
if (typeof callback !== 'function') {
329334
callback = null;
330335
}
@@ -347,10 +352,8 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
347352
localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime);
348353
localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID, eventId);
349354

350-
var userProperties = {};
351-
object.merge(userProperties, this.options.userProperties || {});
352-
353355
// Add the utm properties, if any, onto the user properties.
356+
userProperties = userProperties || {};
354357
object.merge(userProperties, this._utmProperties);
355358

356359
// Add referral info onto the user properties
@@ -412,7 +415,7 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
412415
};
413416

414417
Amplitude.prototype.logEvent = function(eventType, eventProperties, callback) {
415-
return this._logEvent(eventType, eventProperties, null, callback);
418+
return this._logEvent(eventType, eventProperties, null, null, callback);
416419
};
417420

418421
// Test that n is a number or a numeric value.

src/identify.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// var querystring = require('querystring');
2-
31
/*
42
* Wrapper for a user properties JSON object that supports operations
53
*/

test/amplitude.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,24 @@ describe('Amplitude', function() {
7171
reset();
7272
});
7373

74-
it('should set user properties', function() {
75-
amplitude.setUserProperties({'prop': true});
76-
assert.propertyVal(amplitude.options.userProperties, 'prop', true);
77-
});
78-
79-
it('should merge user properties by default', function() {
80-
amplitude.setUserProperties({'prop': true, 'prop2': true});
81-
assert.propertyVal(amplitude.options.userProperties, 'prop', true);
82-
83-
amplitude.setUserProperties({'prop': false, 'prop3': false});
84-
assert.propertyVal(amplitude.options.userProperties, 'prop', false);
85-
assert.propertyVal(amplitude.options.userProperties, 'prop2', true);
86-
assert.propertyVal(amplitude.options.userProperties, 'prop3', false);
87-
});
88-
89-
it('should allow overwriting user properties', function() {
90-
amplitude.setUserProperties({'prop': true, 'prop2': true});
91-
assert.propertyVal(amplitude.options.userProperties, 'prop', true);
74+
it('should log identify call from set user properties', function() {
75+
assert.lengthOf(amplitude._unsentEvents, 0);
76+
amplitude.setUserProperties({'prop': true, 'key': 'value'});
9277

93-
amplitude.setUserProperties({'prop': false, 'prop3': false}, true);
94-
assert.notProperty(amplitude.options.userProperties, 'prop2');
95-
assert.propertyVal(amplitude.options.userProperties, 'prop', false);
96-
assert.propertyVal(amplitude.options.userProperties, 'prop3', false);
78+
assert.lengthOf(amplitude._unsentEvents, 1);
79+
assert.lengthOf(server.requests, 1);
80+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
81+
assert.lengthOf(events, 1);
82+
assert.equal(events[0].event_type, '$identify');
83+
assert.deepEqual(events[0].event_properties, {});
84+
85+
var expected = {
86+
'$set': {
87+
'prop': true,
88+
'key': 'value'
89+
}
90+
};
91+
assert.deepEqual(events[0].user_properties, expected);
9792
});
9893
});
9994

@@ -690,7 +685,6 @@ describe('Amplitude', function() {
690685
amplitude.init(apiKey, undefined, {});
691686

692687
amplitude.setUserProperties({user_prop: true});
693-
amplitude.logEvent('UTM Test Event', {});
694688
assert.lengthOf(server.requests, 1);
695689
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
696690
assert.equal(events[0].user_properties.utm_campaign, undefined);
@@ -722,12 +716,25 @@ describe('Amplitude', function() {
722716
amplitude._initUtmData(utmParams);
723717

724718
amplitude.setUserProperties({user_prop: true});
725-
amplitude.logEvent('UTM Test Event', {});
726-
727719
assert.lengthOf(server.requests, 1);
728720
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
729721
assert.deepEqual(events[0].user_properties, {
730-
user_prop: true,
722+
'$set': {
723+
'user_prop': true
724+
},
725+
utm_campaign: 'new',
726+
utm_content: 'top',
727+
utm_medium: 'email',
728+
utm_source: 'amplitude',
729+
utm_term: 'terms'
730+
});
731+
server.respondWith('success');
732+
server.respond();
733+
734+
amplitude.logEvent('UTM Test Event', {});
735+
assert.lengthOf(server.requests, 2);
736+
var events = JSON.parse(querystring.parse(server.requests[1].requestBody).e);
737+
assert.deepEqual(events[0].user_properties, {
731738
utm_campaign: 'new',
732739
utm_content: 'top',
733740
utm_medium: 'email',
@@ -792,7 +799,6 @@ describe('Amplitude', function() {
792799
amplitude.init(apiKey, undefined, {});
793800

794801
amplitude.setUserProperties({user_prop: true});
795-
amplitude.logEvent('Referrer Test Event', {});
796802
assert.lengthOf(server.requests, 1);
797803
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
798804
assert.equal(events[0].user_properties.referrer, undefined);
@@ -804,7 +810,6 @@ describe('Amplitude', function() {
804810
amplitude.init(apiKey, undefined, {includeReferrer: true});
805811

806812
amplitude.logEvent('Referrer Test Event', {});
807-
808813
assert.lengthOf(server.requests, 1);
809814
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
810815
assert.deepEqual(events[0].user_properties, {
@@ -818,12 +823,12 @@ describe('Amplitude', function() {
818823
amplitude.init(apiKey, undefined, {includeReferrer: true});
819824

820825
amplitude.setUserProperties({user_prop: true});
821-
amplitude.logEvent('Referrer Test Event', {});
822-
823-
assert.lengthOf(server.requests, 1);
826+
assert.lengthOf(server.requests, 1);
824827
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
825828
assert.deepEqual(events[0].user_properties, {
826-
user_prop: true,
829+
$set: {
830+
'user_prop': true
831+
},
827832
referrer: 'https://amplitude.com/contact',
828833
referring_domain: 'amplitude.com'
829834
});

test/identify.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,4 @@ describe('Identify', function() {
157157
assert.deepEqual(expected, identify.userPropertiesOperations);
158158
assert.deepEqual([property], identify.properties);
159159
});
160-
161160
});

0 commit comments

Comments
 (0)