Skip to content

Commit af9024d

Browse files
committed
Make it fall back to the other options/a base64Id, if the device Id is not available in the params via amp_device_id. amplitude#302
1 parent bec6e50 commit af9024d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/amplitude-client.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,10 @@ AmplitudeClient.prototype._getInitialDeviceId = function (configDeviceId, stored
283283
}
284284

285285
if (this.options.deviceIdFromUrlParam) {
286-
return this._getDeviceIdFromUrlParam(this._getUrlParams());
286+
let deviceIdFromUrlParam = this._getDeviceIdFromUrlParam(this._getUrlParams());
287+
if(deviceIdFromUrlParam) {
288+
return deviceIdFromUrlParam;
289+
}
287290
}
288291

289292
if (this.options.deviceId) {

test/amplitude-client.js

+14
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ describe('AmplitudeClient', function() {
248248
amplitude._getUrlParams.restore();
249249
});
250250

251+
it ('should create device id if not set in the url', function(){
252+
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345');
253+
amplitude.init(apiKey, userId, {deviceIdFromUrlParam: true});
254+
assert.notEqual(amplitude.options.deviceId, null);
255+
assert.lengthOf(amplitude.options.deviceId, 22);
256+
257+
const storage = new MetadataStorage({storageKey: cookieName});
258+
const cookieData = storage.load();
259+
assert.notEqual(cookieData.deviceId, null);
260+
assert.lengthOf(cookieData.deviceId, 22);
261+
262+
amplitude._getUrlParams.restore();
263+
});
264+
251265
it ('should prefer the device id in the config over the url params', function() {
252266
var deviceId = 'dd_cc_bb_aa';
253267
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345&amp_device_id=aa_bb_cc_dd');

0 commit comments

Comments
 (0)