Skip to content

Commit 59a1e8e

Browse files
committed
add helper function to regenerate deviceId
1 parent eb67967 commit 59a1e8e

14 files changed

+171
-28
lines changed

CHANGELOG.md

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

3+
* Add helper method to regenerate a new random deviceId. This can be used to anonymize a user after they log out. Note this is not recommended unless you know what are you doing. See [Readme](https://github.com/amplitude/Amplitude-Javascript#logging-out-and-anonymous-users) for more information.
4+
35
### 2.11.0 (April 14, 2016)
46

57
* Add tracking of each user's initial_utm parameters (which is captured as a set once operation). Utm parameters are now sent only once per user session.

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This Readme will guide you through using Amplitude's Javascript SDK to track use
1717
i.parentNode.insertBefore(r,i);var s=function(){this._q=[];return this};function o(e){
1818
s.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
1919
return this}}var a=["add","append","clearAll","prepend","set","setOnce","unset"];for(var c=0;c<a.length;c++){
20-
o(a[c])}n.Identify=s;var u=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
20+
o(a[c])}n.Identify=s;var u=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","regenerateDeviceId"];
2121
function p(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
2222
}}for(var n=0;n<u.length;n++){t(u[n])}}p(n);e.amplitude=n})(window,document);
2323
@@ -59,16 +59,19 @@ If your app has its own login system that you want to track users with, you can
5959
amplitude.setUserId('USER_ID_HERE');
6060
```
6161

62-
A user's data will be merged on the backend so that any events up to that point from the same browser will be tracked under the same user. Note: if a user logs out, or you want to log the events under an anonymous user, you may set the userId to `null` like so:
62+
You can also add the user ID as an argument to the `init` call:
6363

6464
```javascript
65-
amplitude.setUserId(null); // not string 'null'
65+
amplitude.init('YOUR_API_KEY_HERE', 'USER_ID_HERE');
6666
```
6767

68-
You can also add the user ID as an argument to the `init` call:
68+
### Logging Out and Anonymous Users ###
69+
70+
A user's data will be merged on the backend so that any events up to that point from the same browser will be tracked under the same user. Note: if a user logs out, or you want to log the events under an anonymous user, you need to do 2 things: 1) set the userId to `null` 2) regenerate a new deviceId. After doing that, events coming from the current user will appear as a brand new user in Amplitude dashboards. Note if you choose to do this, then you won't be able to see that the 2 users were using the same browser/device.
6971

7072
```javascript
71-
amplitude.init('YOUR_API_KEY_HERE', 'USER_ID_HERE');
73+
amplitude.setUserId(null); // not string 'null'
74+
amplitude.regenerateDeviceId();
7275
```
7376

7477
# Setting Event Properties #

amplitude-segment-snippet.min.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
(function(e,t){var n=e.amplitude||{_q:[]};var r=function(){this._q=[];return this;
2-
};function i(e){r.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
1+
(function(e,t){var r=e.amplitude||{_q:[]};var n=function(){this._q=[];return this;
2+
};function i(e){n.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
33
return this}}var o=["add","append","clearAll","prepend","set","setOnce","unset"];for(var s=0;s<o.length;s++){
4-
i(o[s])}n.Identify=r;var a=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
4+
i(o[s])}r.Identify=n;var a=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","regenerateDeviceId"];
55
function c(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
6-
}}for(var n=0;n<a.length;n++){t(a[n])}}c(n);e.amplitude=n})(window,document);
6+
}}for(var r=0;r<a.length;r++){t(a[r])}}c(r);e.amplitude=r})(window,document);

amplitude-snippet.min.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ r.onload=function(){e.amplitude.runQueuedFunctions()};var i=t.getElementsByTagNa
44
i.parentNode.insertBefore(r,i);var s=function(){this._q=[];return this};function o(e){
55
s.prototype[e]=function(){this._q.push([e].concat(Array.prototype.slice.call(arguments,0)));
66
return this}}var a=["add","append","clearAll","prepend","set","setOnce","unset"];for(var c=0;c<a.length;c++){
7-
o(a[c])}n.Identify=s;var u=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties"];
7+
o(a[c])}n.Identify=s;var u=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","regenerateDeviceId"];
88
function p(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
99
}}for(var n=0;n<u.length;n++){t(u[n])}}p(n);e.amplitude=n})(window,document);

amplitude.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,17 @@ Amplitude.prototype.setOptOut = function setOptOut(enable) {
675675
}
676676
};
677677

678+
/**
679+
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you konw what you
680+
* are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.
681+
* With a null userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
682+
* This uses src/uuid.js to regenerate the deviceId.
683+
* @public
684+
*/
685+
Amplitude.prototype.regenerateDeviceId = function regenerateDeviceId() {
686+
this.setDeviceId(UUID() + 'R');
687+
};
688+
678689
/**
679690
* Sets a custom deviceId for current user. Note: this is not recommended unless you know what you are doing
680691
* (like if you have your own system for managing deviceIds). Make sure the deviceId you set is sufficiently unique

amplitude.min.js

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

documentation/Amplitude.html

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ <h4 class="name" id="__VERSION__"><span class="type-signature"></span>__VERSION_
184184

185185
<dt class="tag-source">Source:</dt>
186186
<dd class="tag-source"><ul class="dummy"><li>
187-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1010">line 1010</a>
187+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1021">line 1021</a>
188188
</li></ul></dd>
189189

190190

@@ -267,7 +267,7 @@ <h4 class="name" id="clearUserProperties"><span class="type-signature"></span>cl
267267

268268
<dt class="tag-source">Source:</dt>
269269
<dd class="tag-source"><ul class="dummy"><li>
270-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line622">line 622</a>
270+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line633">line 633</a>
271271
</li></ul></dd>
272272

273273

@@ -533,7 +533,7 @@ <h5>Parameters:</h5>
533533

534534
<dt class="tag-source">Source:</dt>
535535
<dd class="tag-source"><ul class="dummy"><li>
536-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line643">line 643</a>
536+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line654">line 654</a>
537537
</li></ul></dd>
538538

539539

@@ -1028,7 +1028,7 @@ <h5>Parameters:</h5>
10281028

10291029
<dt class="tag-source">Source:</dt>
10301030
<dd class="tag-source"><ul class="dummy"><li>
1031-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line795">line 795</a>
1031+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line806">line 806</a>
10321032
</li></ul></dd>
10331033

10341034

@@ -1210,7 +1210,7 @@ <h5>Parameters:</h5>
12101210

12111211
<dt class="tag-source">Source:</dt>
12121212
<dd class="tag-source"><ul class="dummy"><li>
1213-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line822">line 822</a>
1213+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line833">line 833</a>
12141214
</li></ul></dd>
12151215

12161216

@@ -1245,6 +1245,91 @@ <h5>Example</h5>
12451245

12461246

12471247

1248+
1249+
<h4 class="name" id="regenerateDeviceId"><span class="type-signature"></span>regenerateDeviceId<span class="signature">()</span><span class="type-signature"></span></h4>
1250+
1251+
1252+
1253+
1254+
1255+
<div class="description">
1256+
Regenerates a new random deviceId for current user. Note: this is not recommended unless you konw what you
1257+
are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.
1258+
With a null userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
1259+
This uses src/uuid.js to regenerate the deviceId.
1260+
</div>
1261+
1262+
1263+
1264+
1265+
1266+
1267+
1268+
1269+
1270+
1271+
1272+
1273+
1274+
<dl class="details">
1275+
1276+
1277+
1278+
1279+
1280+
1281+
1282+
1283+
1284+
1285+
1286+
1287+
1288+
1289+
1290+
1291+
1292+
1293+
1294+
1295+
1296+
1297+
1298+
1299+
1300+
1301+
<dt class="tag-source">Source:</dt>
1302+
<dd class="tag-source"><ul class="dummy"><li>
1303+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line579">line 579</a>
1304+
</li></ul></dd>
1305+
1306+
1307+
1308+
1309+
1310+
1311+
1312+
</dl>
1313+
1314+
1315+
1316+
1317+
1318+
1319+
1320+
1321+
1322+
1323+
1324+
1325+
1326+
1327+
1328+
1329+
1330+
1331+
1332+
12481333

12491334
<h4 class="name" id="setDeviceId"><span class="type-signature"></span>setDeviceId<span class="signature">(deviceId)</span><span class="type-signature"></span></h4>
12501335

@@ -1348,7 +1433,7 @@ <h5>Parameters:</h5>
13481433

13491434
<dt class="tag-source">Source:</dt>
13501435
<dd class="tag-source"><ul class="dummy"><li>
1351-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line580">line 580</a>
1436+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line591">line 591</a>
13521437
</li></ul></dd>
13531438

13541439

@@ -1573,7 +1658,7 @@ <h4 class="name" id="setGlobalUserProperties"><span class="type-signature"></spa
15731658

15741659
<dt class="tag-source">Source:</dt>
15751660
<dd class="tag-source"><ul class="dummy"><li>
1576-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1000">line 1000</a>
1661+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1011">line 1011</a>
15771662
</li></ul></dd>
15781663

15791664

@@ -1995,7 +2080,7 @@ <h5>Parameters:</h5>
19952080

19962081
<dt class="tag-source">Source:</dt>
19972082
<dd class="tag-source"><ul class="dummy"><li>
1998-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line603">line 603</a>
2083+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line614">line 614</a>
19992084
</li></ul></dd>
20002085

20012086

@@ -2131,7 +2216,7 @@ <h5>Parameters:</h5>
21312216

21322217
<dt class="tag-source">Source:</dt>
21332218
<dd class="tag-source"><ul class="dummy"><li>
2134-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line683">line 683</a>
2219+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line694">line 694</a>
21352220
</li></ul></dd>
21362221

21372222

@@ -2297,7 +2382,7 @@ <h5>Parameters:</h5>
22972382

22982383
<dt class="tag-source">Source:</dt>
22992384
<dd class="tag-source"><ul class="dummy"><li>
2300-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line778">line 778</a>
2385+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line789">line 789</a>
23012386
</li></ul></dd>
23022387

23032388

@@ -2343,7 +2428,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
23432428
<br class="clear">
23442429

23452430
<footer>
2346-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 05 2016 14:28:54 GMT-0700 (PDT)
2431+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:58:36 GMT-0700 (PDT)
23472432
</footer>
23482433

23492434
<script> prettyPrint(); </script>

documentation/Identify.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
12951295
<br class="clear">
12961296

12971297
<footer>
1298-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 05 2016 14:28:54 GMT-0700 (PDT)
1298+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:58:36 GMT-0700 (PDT)
12991299
</footer>
13001300

13011301
<script> prettyPrint(); </script>

documentation/amplitude.js.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,17 @@ <h1 class="page-title">Source: amplitude.js</h1>
597597
}
598598
};
599599

600+
/**
601+
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you konw what you
602+
* are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.
603+
* With a null userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
604+
* This uses src/uuid.js to regenerate the deviceId.
605+
* @public
606+
*/
607+
Amplitude.prototype.regenerateDeviceId = function regenerateDeviceId() {
608+
this.setDeviceId(UUID() + 'R');
609+
};
610+
600611
/**
601612
* Sets a custom deviceId for current user. Note: this is not recommended unless you know what you are doing
602613
* (like if you have your own system for managing deviceIds). Make sure the deviceId you set is sufficiently unique
@@ -1054,7 +1065,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
10541065
<br class="clear">
10551066

10561067
<footer>
1057-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 05 2016 14:28:54 GMT-0700 (PDT)
1068+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:58:36 GMT-0700 (PDT)
10581069
</footer>
10591070

10601071
<script> prettyPrint(); </script>

documentation/identify.js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
226226
<br class="clear">
227227

228228
<footer>
229-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 05 2016 14:28:54 GMT-0700 (PDT)
229+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Tue Apr 19 2016 16:58:36 GMT-0700 (PDT)
230230
</footer>
231231

232232
<script> prettyPrint(); </script>

0 commit comments

Comments
 (0)