Skip to content

Commit

Permalink
Make it fall back to the other options/a base64Id, if the device Id i…
Browse files Browse the repository at this point in the history
…s not available in the params via amp_device_id. amplitude#302
  • Loading branch information
aoshika-lv committed Sep 24, 2020
1 parent bec6e50 commit abff3e3
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 abff3e3

Please sign in to comment.