From abff3e35d86c30f5caf23e6b2627fcfc58d1e34d Mon Sep 17 00:00:00 2001 From: aoshika-lv Date: Thu, 24 Sep 2020 17:11:57 +0900 Subject: [PATCH] Make it fall back to the other options/a base64Id, if the device Id is not available in the params via amp_device_id. #302 --- src/amplitude-client.js | 5 ++++- test/amplitude-client.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index c15dd9ca..95aaf9d7 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -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) { diff --git a/test/amplitude-client.js b/test/amplitude-client.js index d370e95c..6bf950e6 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -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&_device_id=aa_bb_cc_dd');