Skip to content

Commit e94a79e

Browse files
committed
Add setDeviceId
1 parent 9ef7257 commit e94a79e

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

amplitude.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,17 @@ Amplitude.prototype.setUserId = function(userId) {
283283
}
284284
};
285285

286+
Amplitude.prototype.setDeviceId = function(deviceId) {
287+
try {
288+
if (deviceId) {
289+
this.options.deviceId = ('' + deviceId);
290+
_saveCookieData(this);
291+
}
292+
} catch (e) {
293+
log(e);
294+
}
295+
};
296+
286297
Amplitude.prototype.setUserProperties = function(userProperties, opt_replace) {
287298
try {
288299
if (opt_replace) {

amplitude.min.js

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

src/amplitude-snippet.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
amplitude._q.push([fn].concat(Array.prototype.slice.call(arguments, 0)));
1313
};
1414
}
15-
var funcs = ["init", "logEvent", "setUserId", "setUserProperties", "setVersionName", "setDomain", "setGlobalUserProperties"];
15+
var funcs = ["init", "logEvent", "setUserId", "setUserProperties", "setVersionName", "setDomain", "setDeviceId",
16+
"setGlobalUserProperties"];
1617
for (var i = 0; i < funcs.length; i++) {
1718
proxy(funcs[i]);
1819
}

src/amplitude.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ Amplitude.prototype.setUserId = function(userId) {
173173
}
174174
};
175175

176+
Amplitude.prototype.setDeviceId = function(deviceId) {
177+
try {
178+
if (deviceId) {
179+
this.options.deviceId = ('' + deviceId);
180+
_saveCookieData(this);
181+
}
182+
} catch (e) {
183+
log(e);
184+
}
185+
};
186+
176187
Amplitude.prototype.setUserProperties = function(userProperties, opt_replace) {
177188
try {
178189
if (opt_replace) {

test/amplitude.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,37 @@ describe('Amplitude', function() {
8686
});
8787
});
8888

89+
describe('setDeviceId', function() {
90+
91+
afterEach(function() {
92+
reset();
93+
});
94+
95+
it('should change device id', function() {
96+
amplitude.setDeviceId('deviceId');
97+
amplitude.init(apiKey);
98+
assert.equal(amplitude.options.deviceId, 'deviceId');
99+
});
100+
101+
it('should not change device id if empty', function() {
102+
amplitude.setDeviceId('');
103+
amplitude.init(apiKey);
104+
assert.notEqual(amplitude.options.deviceId, '');
105+
});
106+
107+
it('should not change device id if null', function() {
108+
amplitude.setDeviceId(null);
109+
amplitude.init(apiKey);
110+
assert.notEqual(amplitude.options.deviceId, null);
111+
});
112+
113+
it('should store device id in cookie', function() {
114+
amplitude.setDeviceId('deviceId');
115+
var stored = cookie.get(amplitude.options.cookieName);
116+
assert.propertyVal(stored, 'deviceId', 'deviceId');
117+
});
118+
});
119+
89120
describe('logEvent', function() {
90121

91122
beforeEach(function() {

0 commit comments

Comments
 (0)