Skip to content

Commit 396aacf

Browse files
author
Krishna Rajendran
authored
Use shorter base64 based ids for device IDs (#251)
1 parent 77d3065 commit 396aacf

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

src/amplitude-client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type from './type';
1111
import UAParser from '@amplitude/ua-parser-js';
1212
import utils from './utils';
1313
import UUID from './uuid';
14+
import base64Id from './base64Id';
1415
import { version } from '../package.json';
1516
import DEFAULT_OPTIONS from './options';
1617

@@ -141,7 +142,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
141142
this.options.deviceId = (type(opt_config) === 'object' && type(opt_config.deviceId) === 'string' &&
142143
!utils.isEmptyString(opt_config.deviceId) && opt_config.deviceId) ||
143144
(this.options.deviceIdFromUrlParam && this._getDeviceIdFromUrlParam(this._getUrlParams())) ||
144-
this.options.deviceId || deviceId || UUID() + 'R';
145+
this.options.deviceId || deviceId || base64Id();
145146
this.options.userId =
146147
(type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
147148
(type(opt_userId) === 'number' && opt_userId.toString()) ||
@@ -885,7 +886,7 @@ AmplitudeClient.prototype.regenerateDeviceId = function regenerateDeviceId() {
885886
return this._q.push(['regenerateDeviceId'].concat(Array.prototype.slice.call(arguments, 0)));
886887
}
887888

888-
this.setDeviceId(UUID() + 'R');
889+
this.setDeviceId(base64Id());
889890
};
890891

891892
/**

src/base64Id.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// A URL safe variation on the the list of Base64 characters
2+
const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
3+
4+
const base64Id = () => {
5+
let str = '';
6+
for (let i = 0; i < 22; ++i) {
7+
str += base64Chars.charAt(Math.floor(Math.random() * 64));
8+
}
9+
return str;
10+
};
11+
12+
export default base64Id;

test/amplitude-client.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('AmplitudeClient', function() {
130130

131131
amplitude.init(apiKey);
132132
assert.equal(amplitude.options.apiKey, apiKey);
133-
assert.lengthOf(amplitude.options.deviceId, 37);
133+
assert.lengthOf(amplitude.options.deviceId, 22);
134134
});
135135

136136
it('should accept userId', function() {
@@ -146,8 +146,7 @@ describe('AmplitudeClient', function() {
146146

147147
it('should generate a random deviceId', function() {
148148
amplitude.init(apiKey, userId);
149-
assert.lengthOf(amplitude.options.deviceId, 37); // UUID is length 36, but we append 'R' at end
150-
assert.equal(amplitude.options.deviceId[36], 'R');
149+
assert.lengthOf(amplitude.options.deviceId, 22)
151150
});
152151

153152
it('should validate config values', function() {
@@ -193,7 +192,7 @@ describe('AmplitudeClient', function() {
193192
const stored = storage.load();
194193
assert.property(stored, 'deviceId');
195194
assert.propertyVal(stored, 'userId', userId);
196-
assert.lengthOf(stored.deviceId, 37); // increase deviceId length by 1 for 'R' character
195+
assert.lengthOf(stored.deviceId, 22);
197196
});
198197

199198
it('should set language', function() {
@@ -918,8 +917,7 @@ describe('setVersionName', function() {
918917
amplitude.init(apiKey, null, {'deviceId': deviceId});
919918
amplitude.regenerateDeviceId();
920919
assert.notEqual(amplitude.options.deviceId, deviceId);
921-
assert.lengthOf(amplitude.options.deviceId, 37);
922-
assert.equal(amplitude.options.deviceId[36], 'R');
920+
assert.lengthOf(amplitude.options.deviceId, 22);
923921
});
924922
});
925923

test/amplitude.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ describe('Amplitude', function() {
245245

246246
amplitude.init(apiKey);
247247
assert.equal(amplitude.options.apiKey, apiKey);
248-
assert.lengthOf(amplitude.options.deviceId, 37);
248+
assert.lengthOf(amplitude.options.deviceId, 22);
249249
});
250250

251251
it('should accept userId', function() {
@@ -255,8 +255,7 @@ describe('Amplitude', function() {
255255

256256
it('should generate a random deviceId', function() {
257257
amplitude.init(apiKey, userId);
258-
assert.lengthOf(amplitude.options.deviceId, 37); // UUID is length 36, but we append 'R' at end
259-
assert.equal(amplitude.options.deviceId[36], 'R');
258+
assert.lengthOf(amplitude.options.deviceId, 22);
260259
});
261260

262261
it('should validate config values', function() {
@@ -285,7 +284,7 @@ describe('Amplitude', function() {
285284
var stored = amplitude.getInstance()._metadataStorage.load();
286285
assert.property(stored, 'deviceId');
287286
assert.propertyVal(stored, 'userId', userId);
288-
assert.lengthOf(stored.deviceId, 37); // increase deviceId length by 1 for 'R' character
287+
assert.lengthOf(stored.deviceId, 22)
289288
});
290289

291290
it('should set language', function() {
@@ -654,8 +653,7 @@ describe('setVersionName', function() {
654653
amplitude.init(apiKey, null, {'deviceId': deviceId});
655654
amplitude.regenerateDeviceId();
656655
assert.notEqual(amplitude.options.deviceId, deviceId);
657-
assert.lengthOf(amplitude.options.deviceId, 37);
658-
assert.equal(amplitude.options.deviceId[36], 'R');
656+
assert.lengthOf(amplitude.options.deviceId, 22);
659657
});
660658
});
661659

test/base64id.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import base64Id from '../src/base64Id';
2+
3+
describe('base64Id', () => {
4+
it('should return an id with length 22', () => {
5+
assert.equal(base64Id().length, 22);
6+
});
7+
8+
// If this test fails randomly it'll be frustrating to reproduce. Ideally
9+
// there would be some reproducible seed we would print for every test run.
10+
it('should return an id of safe base64 characters', () => {
11+
assert.equal(true, /^[a-zA-Z0-9\-_]*$/.test(base64Id()))
12+
});
13+
});

test/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ import './utils.js';
1313
import './revenue.js';
1414
import './base-cookie.js';
1515
import './top-domain.js';
16+
import './base64Id.js';

0 commit comments

Comments
 (0)