Skip to content

Commit

Permalink
fix: Unavailable deviceId fallbacks (#303)
Browse files Browse the repository at this point in the history
* Make it fall back to other options/a base64Id, if the device Id is not available in the params via amp_device_id. #302
  • Loading branch information
aoshika-lv authored Sep 25, 2020
1 parent bec6e50 commit e0d39fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ AmplitudeClient.prototype._getInitialDeviceId = function (configDeviceId, stored
}

if (this.options.deviceIdFromUrlParam) {
return this._getDeviceIdFromUrlParam(this._getUrlParams());
let deviceIdFromUrlParam = this._getDeviceIdFromUrlParam(this._getUrlParams());
if (deviceIdFromUrlParam) {
return deviceIdFromUrlParam;
}
}

if (this.options.deviceId) {
Expand Down
14 changes: 14 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ describe('AmplitudeClient', function() {
amplitude._getUrlParams.restore();
});

it ('should create device id if not set in the url', function(){
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345');
amplitude.init(apiKey, userId, {deviceIdFromUrlParam: true});
assert.notEqual(amplitude.options.deviceId, null);
assert.lengthOf(amplitude.options.deviceId, 22);

const storage = new MetadataStorage({storageKey: cookieName});
const cookieData = storage.load();
assert.notEqual(cookieData.deviceId, null);
assert.lengthOf(cookieData.deviceId, 22);

amplitude._getUrlParams.restore();
});

it ('should prefer the device id in the config over the url params', function() {
var deviceId = 'dd_cc_bb_aa';
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345&amp_device_id=aa_bb_cc_dd');
Expand Down

0 comments on commit e0d39fd

Please sign in to comment.