Skip to content

Commit 657a4ef

Browse files
committed
Merge user properties
1 parent c9ee486 commit 657a4ef

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

amplitude.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ var _loadCookieData = function(scope) {
238238
scope.options.userId = cookieData.userId;
239239
}
240240
if (cookieData.globalUserProperties) {
241-
scope.options.globalUserProperties = cookieData.globalUserProperties;
241+
scope.options.userProperties = cookieData.globalUserProperties;
242242
}
243243
}
244244
};
@@ -247,7 +247,7 @@ var _saveCookieData = function(scope) {
247247
Cookie.set(scope.options.cookieName, {
248248
deviceId: scope.options.deviceId,
249249
userId: scope.options.userId,
250-
globalUserProperties: scope.options.globalUserProperties
250+
globalUserProperties: scope.options.userProperties
251251
});
252252
};
253253

@@ -283,9 +283,13 @@ Amplitude.prototype.setUserId = function(userId) {
283283
}
284284
};
285285

286-
Amplitude.prototype.setUserProperties = function(userProperties) {
286+
Amplitude.prototype.setUserProperties = function(userProperties, opt_replace) {
287287
try {
288-
this.options.globalUserProperties = userProperties;
288+
if (opt_replace) {
289+
this.options.userProperties = userProperties;
290+
} else {
291+
this.options.userProperties = object.merge(this.options.userProperties || {}, userProperties);
292+
}
289293
_saveCookieData(this);
290294
//log('set userProperties=' + JSON.stringify(userProperties));
291295
} catch (e) {
@@ -334,7 +338,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
334338
os_version: ua.browser.version,
335339
device_model: ua.os.family,
336340
event_properties: eventProperties,
337-
user_properties: this.options.globalUserProperties || {},
341+
user_properties: this.options.userProperties || {},
338342
uuid: UUID(),
339343
library: {
340344
name: 'amplitude-js',

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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ var _loadCookieData = function(scope) {
128128
scope.options.userId = cookieData.userId;
129129
}
130130
if (cookieData.globalUserProperties) {
131-
scope.options.globalUserProperties = cookieData.globalUserProperties;
131+
scope.options.userProperties = cookieData.globalUserProperties;
132132
}
133133
}
134134
};
@@ -137,7 +137,7 @@ var _saveCookieData = function(scope) {
137137
Cookie.set(scope.options.cookieName, {
138138
deviceId: scope.options.deviceId,
139139
userId: scope.options.userId,
140-
globalUserProperties: scope.options.globalUserProperties
140+
globalUserProperties: scope.options.userProperties
141141
});
142142
};
143143

@@ -173,9 +173,13 @@ Amplitude.prototype.setUserId = function(userId) {
173173
}
174174
};
175175

176-
Amplitude.prototype.setUserProperties = function(userProperties) {
176+
Amplitude.prototype.setUserProperties = function(userProperties, opt_replace) {
177177
try {
178-
this.options.globalUserProperties = userProperties;
178+
if (opt_replace) {
179+
this.options.userProperties = userProperties;
180+
} else {
181+
this.options.userProperties = object.merge(this.options.userProperties || {}, userProperties);
182+
}
179183
_saveCookieData(this);
180184
//log('set userProperties=' + JSON.stringify(userProperties));
181185
} catch (e) {
@@ -224,7 +228,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
224228
os_version: ua.browser.version,
225229
device_model: ua.os.family,
226230
event_properties: eventProperties,
227-
user_properties: this.options.globalUserProperties || {},
231+
user_properties: this.options.userProperties || {},
228232
uuid: UUID(),
229233
library: {
230234
name: 'amplitude-js',

test/amplitude.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,41 @@ describe('Amplitude', function() {
5151
});
5252
});
5353

54+
describe('setUserProperties', function() {
55+
beforeEach(function() {
56+
amplitude.init(apiKey);
57+
});
58+
59+
afterEach(function() {
60+
reset();
61+
});
62+
63+
it('should set user properties', function() {
64+
amplitude.setUserProperties({'prop': true});
65+
assert.propertyVal(amplitude.options.userProperties, 'prop', true);
66+
});
67+
68+
it('should merge user properties by default', function() {
69+
amplitude.setUserProperties({'prop': true, 'prop2': true});
70+
assert.propertyVal(amplitude.options.userProperties, 'prop', true);
71+
72+
amplitude.setUserProperties({'prop': false, 'prop3': false});
73+
assert.propertyVal(amplitude.options.userProperties, 'prop', false);
74+
assert.propertyVal(amplitude.options.userProperties, 'prop2', true);
75+
assert.propertyVal(amplitude.options.userProperties, 'prop3', false);
76+
});
77+
78+
it('should allow overwriting user properties', function() {
79+
amplitude.setUserProperties({'prop': true, 'prop2': true});
80+
assert.propertyVal(amplitude.options.userProperties, 'prop', true);
81+
82+
amplitude.setUserProperties({'prop': false, 'prop3': false}, true);
83+
assert.notProperty(amplitude.options.userProperties, 'prop2');
84+
assert.propertyVal(amplitude.options.userProperties, 'prop', false);
85+
assert.propertyVal(amplitude.options.userProperties, 'prop3', false);
86+
});
87+
});
88+
5489
describe('logEvent', function() {
5590

5691
beforeEach(function() {

0 commit comments

Comments
 (0)