This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathuser.ts
210 lines (178 loc) · 4.69 KB
/
user.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use strict';
/*
* Module dependencies.
*/
import { InitOptions } from './types';
var Entity = require('./entity');
var bindAll = require('bind-all');
var cookie = require('./cookie');
var debug = require('debug')('analytics:user');
var inherit = require('inherits');
var rawCookie = require('@segment/cookie');
var { v4: uuidv4 } = require('uuid');
var localStorage = require('./store');
/**
* User defaults
*/
interface UserDefaults {
persist: boolean;
cookie: {
key: string;
oldKey: string;
};
localStorage: {
key: string;
};
}
interface User {
defaults: UserDefaults;
debug: unknown;
}
User.defaults = {
persist: true,
cookie: {
key: 'ajs_user_id',
oldKey: 'ajs_user',
},
localStorage: {
key: 'ajs_user_traits',
},
};
/**
* Initialize a new `User` with `options`.
*/
function User(options?: InitOptions) {
this.defaults = User.defaults;
this.debug = debug;
Entity.call(this, options);
}
/**
* Inherit `Entity`
*/
inherit(User, Entity);
/**
* Set/get the user id.
*
* When the user id changes, the method will reset his anonymousId to a new one.
*
* @example
* // didn't change because the user didn't have previous id.
* anonymousId = user.anonymousId();
* user.id('foo');
* assert.equal(anonymousId, user.anonymousId());
*
* // didn't change because the user id changed to null.
* anonymousId = user.anonymousId();
* user.id('foo');
* user.id(null);
* assert.equal(anonymousId, user.anonymousId());
*
* // change because the user had previous id.
* anonymousId = user.anonymousId();
* user.id('foo');
* user.id('baz'); // triggers change
* user.id('baz'); // no change
* assert.notEqual(anonymousId, user.anonymousId());
*/
User.prototype.id = function (id?: string): string | undefined {
var prev = this._getId();
var ret = Entity.prototype.id.apply(this, arguments);
if (prev == null) return ret;
// FIXME: We're relying on coercion here (1 == "1"), but our API treats these
// two values differently. Figure out what will break if we remove this and
// change to strict equality
/* eslint-disable eqeqeq */
if (prev != id && id) this.anonymousId(null);
/* eslint-enable eqeqeq */
return ret;
};
/**
* Set / get / remove anonymousId.
*
* @param {String} anonymousId
* @return {String|User}
*/
User.prototype.anonymousId = function (anonymousId?: string): string | User {
var store = this.storage();
// set / remove
if (arguments.length) {
store.set('ajs_anonymous_id', anonymousId);
this._setAnonymousIdInLocalStorage(anonymousId);
return this;
}
// new
anonymousId = store.get('ajs_anonymous_id');
if (anonymousId) {
// value exists in cookie, copy it to localStorage
this._setAnonymousIdInLocalStorage(anonymousId);
// refresh cookie to extend expiry
store.set('ajs_anonymous_id', anonymousId);
return anonymousId;
}
if (!this._options.localStorageFallbackDisabled) {
// if anonymousId doesn't exist in cookies, check localStorage
anonymousId = localStorage.get('ajs_anonymous_id');
if (anonymousId) {
// Write to cookies if available in localStorage but not cookies
store.set('ajs_anonymous_id', anonymousId);
return anonymousId;
}
}
// old - it is not stringified so we use the raw cookie.
anonymousId = rawCookie('_sio');
if (anonymousId) {
anonymousId = anonymousId.split('----')[0];
store.set('ajs_anonymous_id', anonymousId);
this._setAnonymousIdInLocalStorage(anonymousId);
store.remove('_sio');
return anonymousId;
}
// empty
anonymousId = uuidv4();
store.set('ajs_anonymous_id', anonymousId);
this._setAnonymousIdInLocalStorage(anonymousId);
return store.get('ajs_anonymous_id');
};
/**
* Set the user's `anonymousid` in local storage.
*/
User.prototype._setAnonymousIdInLocalStorage = function (id: string) {
if (!this._options.localStorageFallbackDisabled) {
localStorage.set('ajs_anonymous_id', id);
}
};
/**
* Remove anonymous id on logout too.
*/
User.prototype.logout = function () {
Entity.prototype.logout.call(this);
this.anonymousId(null);
};
/**
* Load saved user `id` or `traits` from storage.
*/
User.prototype.load = function () {
if (this._loadOldCookie()) return;
Entity.prototype.load.call(this);
};
/**
* BACKWARDS COMPATIBILITY: Load the old user from the cookie.
*
* @api private
*/
User.prototype._loadOldCookie = function (): boolean {
var user = cookie.get(this._options.cookie.oldKey);
if (!user) return false;
this.id(user.id);
this.traits(user.traits);
cookie.remove(this._options.cookie.oldKey);
return true;
};
/**
* Expose the user singleton.
*/
module.exports = bindAll(new User());
/**
* Expose the `User` constructor.
*/
module.exports.User = User;