Skip to content

Commit fd10163

Browse files
author
Daniel Jih
committed
add option to parse device id from url params
1 parent bb3afe8 commit fd10163

File tree

8 files changed

+73
-9
lines changed

8 files changed

+73
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22

33
* Add `logEventWithTimestamp` to allow logging events with a custom timestamp. The timestamp should a number representing the time in milliseconds since epoch. See [documentation](https://rawgit.com/amplitude/Amplitude-Javascript/master/documentation/AmplitudeClient.html) for more details.
4+
* Add configuration option `deviceIdFromUrlParam`, which when set to `true` will have the SDK parse device Ids from url parameter `amp_device_id` if available. DeviceIds defined in the configuration options during init will take priority over device Ids from url parameters.
45

56
### 3.3.2 (October 28, 2016)
67

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ amplitude.getInstance().init('YOUR_API_KEY_HERE', null, {
328328
| cookieExpiration | number | The number of days after which the Amplitude cookie will expire | 365\*10 (10 years) |
329329
| cookieName | string | Custom name for the Amplitude cookie | 'amplitude_id' |
330330
| deviceId | string | Custom device ID to set. Note this is not recommended unless you really know what you are doing (like if you have your own system for tracking user devices) | Randomly generated UUID |
331+
| deviceIdFromUrlParam | boolean | If `true`, the SDK will parse device Id values from url parameter `amp_device_id` if available. Device Ids defined in the configuration options during init will take priority over device Ids from url parameters.
331332
| domain | string | Custom cookie domain | The top domain of the current page's url |
332333
| eventUploadPeriodMillis | number | Amount of time in milliseconds that the SDK waits before uploading events if `batchEvents` is `true`. | 30\*1000 (30 sec) |
333334
| eventUploadThreshold | number | Minimum number of events to batch together per request if `batchEvents` is `true`. | 30 |

amplitude.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
562562

563563
// load deviceId and userId from input, or try to fetch existing value from cookie
564564
this.options.deviceId = (type(opt_config) === 'object' && type(opt_config.deviceId) === 'string' &&
565-
!utils.isEmptyString(opt_config.deviceId) && opt_config.deviceId) || this.options.deviceId || UUID() + 'R';
565+
!utils.isEmptyString(opt_config.deviceId) && opt_config.deviceId) ||
566+
(this.options.deviceIdFromUrlParam && this._getDeviceIdFromUrlParam(this._getUrlParams())) ||
567+
this.options.deviceId || UUID() + 'R';
566568
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
567569
this.options.userId || null;
568570

@@ -986,6 +988,14 @@ AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
986988
_sendParamsReferrerUserProperties(this, gclidProperties);
987989
};
988990

991+
/**
992+
* Try to fetch Amplitude device id from url params.
993+
* @private
994+
*/
995+
AmplitudeClient.prototype._getDeviceIdFromUrlParam = function _getDeviceIdFromUrlParam(urlParams) {
996+
return utils.getQueryParam(Constants.AMP_DEVICE_ID_PARAM, urlParams);
997+
};
998+
989999
/**
9901000
* Parse the domain from referrer info
9911001
* @private
@@ -1686,7 +1696,9 @@ module.exports = {
16861696
REVENUE_PRODUCT_ID: '$productId',
16871697
REVENUE_QUANTITY: '$quantity',
16881698
REVENUE_PRICE: '$price',
1689-
REVENUE_REVENUE_TYPE: '$revenueType'
1699+
REVENUE_REVENUE_TYPE: '$revenueType',
1700+
1701+
AMP_DEVICE_ID_PARAM: 'amp_device_id' // url param
16901702
};
16911703

16921704
}, {}],
@@ -4991,7 +5003,8 @@ module.exports = {
49915003
eventUploadPeriodMillis: 30 * 1000, // 30s
49925004
forceHttps: false,
49935005
includeGclid: false,
4994-
saveParamsReferrerOncePerSession: true
5006+
saveParamsReferrerOncePerSession: true,
5007+
deviceIdFromUrlParam: false,
49955008
};
49965009

49975010
}, {"./language":29}],

amplitude.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
8181

8282
// load deviceId and userId from input, or try to fetch existing value from cookie
8383
this.options.deviceId = (type(opt_config) === 'object' && type(opt_config.deviceId) === 'string' &&
84-
!utils.isEmptyString(opt_config.deviceId) && opt_config.deviceId) || this.options.deviceId || UUID() + 'R';
84+
!utils.isEmptyString(opt_config.deviceId) && opt_config.deviceId) ||
85+
(this.options.deviceIdFromUrlParam && this._getDeviceIdFromUrlParam(this._getUrlParams())) ||
86+
this.options.deviceId || UUID() + 'R';
8587
this.options.userId = (type(opt_userId) === 'string' && !utils.isEmptyString(opt_userId) && opt_userId) ||
8688
this.options.userId || null;
8789

@@ -505,6 +507,14 @@ AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
505507
_sendParamsReferrerUserProperties(this, gclidProperties);
506508
};
507509

510+
/**
511+
* Try to fetch Amplitude device id from url params.
512+
* @private
513+
*/
514+
AmplitudeClient.prototype._getDeviceIdFromUrlParam = function _getDeviceIdFromUrlParam(urlParams) {
515+
return utils.getQueryParam(Constants.AMP_DEVICE_ID_PARAM, urlParams);
516+
};
517+
508518
/**
509519
* Parse the domain from referrer info
510520
* @private

src/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ module.exports = {
2424
REVENUE_PRODUCT_ID: '$productId',
2525
REVENUE_QUANTITY: '$quantity',
2626
REVENUE_PRICE: '$price',
27-
REVENUE_REVENUE_TYPE: '$revenueType'
27+
REVENUE_REVENUE_TYPE: '$revenueType',
28+
29+
AMP_DEVICE_ID_PARAM: 'amp_device_id' // url param
2830
};

src/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ module.exports = {
2222
eventUploadPeriodMillis: 30 * 1000, // 30s
2323
forceHttps: false,
2424
includeGclid: false,
25-
saveParamsReferrerOncePerSession: true
25+
saveParamsReferrerOncePerSession: true,
26+
deviceIdFromUrlParam: false,
2627
};

test/amplitude-client.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,42 @@ describe('AmplitudeClient', function() {
132132
assert.equal(counter, 1);
133133
});
134134

135+
it ('should load the device id from url params if configured', function() {
136+
var deviceId = 'aa_bb_cc_dd';
137+
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345&amp_device_id=aa_bb_cc_dd');
138+
amplitude.init(apiKey, userId, {deviceIdFromUrlParam: true});
139+
assert.equal(amplitude.options.deviceId, deviceId);
140+
141+
var cookieData = cookie.get(amplitude.options.cookieName);
142+
assert.equal(cookieData.deviceId, deviceId);
143+
144+
amplitude._getUrlParams.restore();
145+
});
146+
147+
it ('should not load device id from url params if not configured', function() {
148+
var deviceId = 'aa_bb_cc_dd';
149+
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345&amp_device_id=aa_bb_cc_dd');
150+
amplitude.init(apiKey, userId, {deviceIdFromUrlParam: false});
151+
assert.notEqual(amplitude.options.deviceId, deviceId);
152+
153+
var cookieData = cookie.get(amplitude.options.cookieName);
154+
assert.notEqual(cookieData.deviceId, deviceId);
155+
156+
amplitude._getUrlParams.restore();
157+
});
158+
159+
it ('should prefer the device id in the config over the url params', function() {
160+
var deviceId = 'dd_cc_bb_aa';
161+
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&gclid=12345&amp_device_id=aa_bb_cc_dd');
162+
amplitude.init(apiKey, userId, {deviceId: deviceId, deviceIdFromUrlParam: true});
163+
assert.equal(amplitude.options.deviceId, deviceId);
164+
165+
var cookieData = cookie.get(amplitude.options.cookieName);
166+
assert.equal(cookieData.deviceId, deviceId);
167+
168+
amplitude._getUrlParams.restore();
169+
});
170+
135171
it ('should migrate deviceId, userId, optOut from localStorage to cookie on default instance', function() {
136172
var deviceId = 'test_device_id';
137173
var userId = 'test_user_id';

0 commit comments

Comments
 (0)