Skip to content

Commit 6ba8ca2

Browse files
committed
Merge pull request #69 from amplitude/regenerate_device_id
add helper function to regenerate deviceId
2 parents edced64 + 3d379a3 commit 6ba8ca2

18 files changed

+176
-35
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 `logRevenueV2` and new `Revenue` class to support logging revenue events with properties, and revenue type. See [Readme](https://github.com/amplitude/Amplitude-Javascript#tracking-revenue) for more info.
4+
* 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.
45

56
### 2.11.0 (April 14, 2016)
67

README.md

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

63-
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:
63+
You can also add the user ID as an argument to the `init` call:
6464

6565
```javascript
66-
amplitude.setUserId(null); // not string 'null'
66+
amplitude.init('YOUR_API_KEY_HERE', 'USER_ID_HERE');
6767
```
6868

69-
You can also add the user ID as an argument to the `init` call:
69+
### Logging Out and Anonymous Users ###
70+
71+
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.
7072

7173
```javascript
72-
amplitude.init('YOUR_API_KEY_HERE', 'USER_ID_HERE');
74+
amplitude.setUserId(null); // not string 'null'
75+
amplitude.regenerateDeviceId();
7376
```
7477

7578
# Setting Event Properties #

amplitude-segment-snippet.min.js

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

amplitude-snippet.min.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ s.parentNode.insertBefore(r,s);function i(e,t){e.prototype[t]=function(){this._q
55
return this}}var o=function(){this._q=[];return this};var a=["add","append","clearAll","prepend","set","setOnce","unset"];
66
for(var u=0;u<a.length;u++){i(o,a[u])}n.Identify=o;var c=function(){this._q=[];return this;
77
};var p=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"];
8-
for(var l=0;l<p.length;l++){i(c,p[l])}n.Revenue=c;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","logRevenueV2"];
8+
for(var l=0;l<p.length;l++){i(c,p[l])}n.Revenue=c;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","logRevenueV2","regenerateDeviceId"];
99
function v(e){function t(t){e[t]=function(){e._q.push([t].concat(Array.prototype.slice.call(arguments,0)));
1010
}}for(var n=0;n<d.length;n++){t(d[n])}}v(n);e.amplitude=n})(window,document);

amplitude.js

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

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

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.

documentation/Amplitude.html

Lines changed: 97 additions & 12 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#line1051">line 1051</a>
187+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1062">line 1062</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#line624">line 624</a>
270+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line635">line 635</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#line660">line 660</a>
536+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line671">line 671</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#line805">line 805</a>
1031+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line816">line 816</a>
10321032
</li></ul></dd>
10331033

10341034

@@ -1212,7 +1212,7 @@ <h5>Parameters:</h5>
12121212

12131213
<dt class="tag-source">Source:</dt>
12141214
<dd class="tag-source"><ul class="dummy"><li>
1215-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line863">line 863</a>
1215+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line874">line 874</a>
12161216
</li></ul></dd>
12171217

12181218

@@ -1351,7 +1351,7 @@ <h5>Parameters:</h5>
13511351

13521352
<dt class="tag-source">Source:</dt>
13531353
<dd class="tag-source"><ul class="dummy"><li>
1354-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line834">line 834</a>
1354+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line845">line 845</a>
13551355
</li></ul></dd>
13561356

13571357

@@ -1387,6 +1387,91 @@ <h5>Example</h5>
13871387

13881388

13891389

1390+
1391+
<h4 class="name" id="regenerateDeviceId"><span class="type-signature"></span>regenerateDeviceId<span class="signature">()</span><span class="type-signature"></span></h4>
1392+
1393+
1394+
1395+
1396+
1397+
<div class="description">
1398+
Regenerates a new random deviceId for current user. Note: this is not recommended unless you konw what you
1399+
are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.
1400+
With a null userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
1401+
This uses src/uuid.js to regenerate the deviceId.
1402+
</div>
1403+
1404+
1405+
1406+
1407+
1408+
1409+
1410+
1411+
1412+
1413+
1414+
1415+
1416+
<dl class="details">
1417+
1418+
1419+
1420+
1421+
1422+
1423+
1424+
1425+
1426+
1427+
1428+
1429+
1430+
1431+
1432+
1433+
1434+
1435+
1436+
1437+
1438+
1439+
1440+
1441+
1442+
1443+
<dt class="tag-source">Source:</dt>
1444+
<dd class="tag-source"><ul class="dummy"><li>
1445+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line581">line 581</a>
1446+
</li></ul></dd>
1447+
1448+
1449+
1450+
1451+
1452+
1453+
1454+
</dl>
1455+
1456+
1457+
1458+
1459+
1460+
1461+
1462+
1463+
1464+
1465+
1466+
1467+
1468+
1469+
1470+
1471+
1472+
1473+
1474+
13901475

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

@@ -1490,7 +1575,7 @@ <h5>Parameters:</h5>
14901575

14911576
<dt class="tag-source">Source:</dt>
14921577
<dd class="tag-source"><ul class="dummy"><li>
1493-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line582">line 582</a>
1578+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line593">line 593</a>
14941579
</li></ul></dd>
14951580

14961581

@@ -1715,7 +1800,7 @@ <h4 class="name" id="setGlobalUserProperties"><span class="type-signature"></spa
17151800

17161801
<dt class="tag-source">Source:</dt>
17171802
<dd class="tag-source"><ul class="dummy"><li>
1718-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1041">line 1041</a>
1803+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line1052">line 1052</a>
17191804
</li></ul></dd>
17201805

17211806

@@ -2137,7 +2222,7 @@ <h5>Parameters:</h5>
21372222

21382223
<dt class="tag-source">Source:</dt>
21392224
<dd class="tag-source"><ul class="dummy"><li>
2140-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line605">line 605</a>
2225+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line616">line 616</a>
21412226
</li></ul></dd>
21422227

21432228

@@ -2273,7 +2358,7 @@ <h5>Parameters:</h5>
22732358

22742359
<dt class="tag-source">Source:</dt>
22752360
<dd class="tag-source"><ul class="dummy"><li>
2276-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line693">line 693</a>
2361+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line704">line 704</a>
22772362
</li></ul></dd>
22782363

22792364

@@ -2439,7 +2524,7 @@ <h5>Parameters:</h5>
24392524

24402525
<dt class="tag-source">Source:</dt>
24412526
<dd class="tag-source"><ul class="dummy"><li>
2442-
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line788">line 788</a>
2527+
<a href="amplitude.js.html">amplitude.js</a>, <a href="amplitude.js.html#line799">line 799</a>
24432528
</li></ul></dd>
24442529

24452530

@@ -2485,7 +2570,7 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Amplitude
24852570
<br class="clear">
24862571

24872572
<footer>
2488-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 00:53:16 GMT-0700 (PDT)
2573+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 01:07:31 GMT-0700 (PDT)
24892574
</footer>
24902575

24912576
<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 Wed Apr 20 2016 00:53:16 GMT-0700 (PDT)
1298+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 01:07:31 GMT-0700 (PDT)
12991299
</footer>
13001300

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

documentation/Revenue.html

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

967967
<footer>
968-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 00:53:16 GMT-0700 (PDT)
968+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 01:07:31 GMT-0700 (PDT)
969969
</footer>
970970

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

documentation/amplitude.js.html

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

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

10971108
<footer>
1098-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 00:53:16 GMT-0700 (PDT)
1109+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Apr 20 2016 01:07:31 GMT-0700 (PDT)
10991110
</footer>
11001111

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

0 commit comments

Comments
 (0)