implemented amplitude metrics (#1141)

This commit is contained in:
Danny Coates 2019-02-12 11:50:06 -08:00 committed by GitHub
parent 1a483cad55
commit 9b37e92a81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 774 additions and 528 deletions

View file

@ -9,6 +9,16 @@ import storage from './storage';
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
const anonId = arrayToB64(crypto.getRandomValues(new Uint8Array(16)));
async function hashId(id) {
const d = new Date();
const month = d.getUTCMonth();
const year = d.getUTCFullYear();
const encoded = textEncoder.encode(`${id}:${year}:${month}`);
const hash = await crypto.subtle.digest('SHA-256', encoded);
return arrayToB64(new Uint8Array(hash.slice(16)));
}
export default class User {
constructor(storage) {
@ -25,6 +35,14 @@ export default class User {
this.storage.user = data;
}
get firstAction() {
return this.storage.get('firstAction');
}
set firstAction(action) {
this.storage.set('firstAction', action);
}
get avatar() {
const defaultAvatar = assets.get('user.svg');
if (this.info.avatarDefault) {
@ -63,6 +81,14 @@ export default class User {
return this.loggedIn ? LIMITS.MAX_DOWNLOADS : LIMITS.ANON.MAX_DOWNLOADS;
}
async metricId() {
return this.loggedIn ? hashId(this.info.uid) : undefined;
}
async deviceId() {
return this.loggedIn ? hashId(this.storage.id) : hashId(anonId);
}
async login(email) {
const state = arrayToB64(crypto.getRandomValues(new Uint8Array(16)));
storage.set('oauthState', state);