Skip to content

[Analytics] Implement tracking for user profile changes #11176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/dashboard/src/settings/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function Account() {
const [modal, setModal] = useState(false);
const primaryEmail = User.getPrimaryEmail(user!) || "";
const [typedEmail, setTypedEmail] = useState("");
const original = ProfileState.getProfileState(user!);
const original = User.getProfile(user!);
const [profileState, setProfileState] = useState(original);
const [errorMessage, setErrorMessage] = useState("");
const [updated, setUpdated] = useState(false);
Expand All @@ -31,7 +31,7 @@ export default function Account() {
if (error) {
return;
}
const updatedUser = ProfileState.setProfileState(user!, profileState);
const updatedUser = User.setProfile(user!, profileState);
setUser(updatedUser);
getGitpodService().server.updateLoggedInUser(updatedUser);
setUpdated(true);
Expand Down
52 changes: 5 additions & 47 deletions components/dashboard/src/settings/ProfileInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,6 @@ import { UserContext } from "../user-context";
import { isGitpodIo } from "../utils";

export namespace ProfileState {
export interface ProfileState {
name: string;
email: string;
company?: string;
avatarURL?: string;
}

export function getProfileState(user: User): ProfileState {
return {
name: User.getName(user!) || "",
email: User.getPrimaryEmail(user!) || "",
company: user?.additionalData?.profile?.companyName,
avatarURL: user?.avatarUrl,
};
}

export function setProfileState(user: User, profileState: ProfileState): User {
user.fullName = profileState.name;
user.avatarUrl = profileState.avatarURL;

if (!user.additionalData) {
user.additionalData = {};
}
if (!user.additionalData.profile) {
user.additionalData.profile = {};
}
user.additionalData.profile.emailAddress = profileState.email;
user.additionalData.profile.companyName = profileState.company;
user.additionalData.profile.lastUpdatedDetailsNudge = new Date().toISOString();

return user;
}

export function hasChanges(before: ProfileState, after: ProfileState) {
return (
before.name !== after.name ||
before.email !== after.email ||
before.company !== after.company ||
before.avatarURL !== after.avatarURL
);
}

function shouldNudgeForUpdate(user: User): boolean {
if (!isGitpodIo()) {
return false;
Expand All @@ -79,7 +37,7 @@ export namespace ProfileState {
* @param state
* @returns error message or empty string when valid
*/
export function validate(state: ProfileState): string {
export function validate(state: User.Profile): string {
if (state.name.trim() === "") {
return "Name must not be empty.";
}
Expand All @@ -98,7 +56,7 @@ export namespace ProfileState {

export function NudgeForProfileUpdateModal() {
const { user, setUser } = useContext(UserContext);
const original = ProfileState.getProfileState(user!);
const original = User.getProfile(user!);
const [profileState, setProfileState] = useState(original);
const [errorMessage, setErrorMessage] = useState("");
const [visible, setVisible] = useState(shouldNudgeForUpdate(user!));
Expand All @@ -109,7 +67,7 @@ export namespace ProfileState {
if (error) {
return;
}
const updatedUser = ProfileState.setProfileState(user!, profileState);
const updatedUser = User.setProfile(user!, profileState);
setUser(updatedUser);
getGitpodService().server.updateLoggedInUser(updatedUser);
setVisible(shouldNudgeForUpdate(updatedUser!));
Expand Down Expand Up @@ -150,8 +108,8 @@ export namespace ProfileState {
}

export default function ProfileInformation(props: {
profileState: ProfileState.ProfileState;
setProfileState: (newState: ProfileState.ProfileState) => void;
profileState: User.Profile;
setProfileState: (newState: User.Profile) => void;
errorMessage: string;
updated: boolean;
children?: React.ReactChild[] | React.ReactChild;
Expand Down
45 changes: 45 additions & 0 deletions components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,50 @@ export namespace User {
}
user.additionalData.ideSettings = newIDESettings;
}

export function getProfile(user: User): Profile {
return {
name: User.getName(user!) || "",
email: User.getPrimaryEmail(user!) || "",
company: user?.additionalData?.profile?.companyName,
avatarURL: user?.avatarUrl,
};
}

export function setProfile(user: User, profile: Profile): User {
user.fullName = profile.name;
user.avatarUrl = profile.avatarURL;

if (!user.additionalData) {
user.additionalData = {};
}
if (!user.additionalData.profile) {
user.additionalData.profile = {};
}
user.additionalData.profile.emailAddress = profile.email;
user.additionalData.profile.companyName = profile.company;
user.additionalData.profile.lastUpdatedDetailsNudge = new Date().toISOString();

return user;
}

// The actual Profile of a User
export interface Profile {
name: string;
email: string;
company?: string;
avatarURL?: string;
}
export namespace Profile {
export function hasChanges(before: Profile, after: Profile) {
return (
before.name !== after.name ||
before.email !== after.email ||
before.company !== after.company ||
before.avatarURL !== after.avatarURL
);
}
}
}

export interface AdditionalUserData {
Expand All @@ -163,6 +207,7 @@ export interface AdditionalUserData {
profile?: ProfileDetails;
}

// The format in which we store User Profiles in
export interface ProfileDetails {
// when was the last time the user updated their profile information or has been nudged to do so.
lastUpdatedDetailsNudge?: string;
Expand Down
1 change: 1 addition & 0 deletions components/server/src/user/user-deletion-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class UserDeletionService {
bitbucket_slug: "deleted-user",
email: "deleted-user",
full_name: "deleted-user",
name: "deleted-user",
},
});
}
Expand Down
18 changes: 18 additions & 0 deletions components/server/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
const user = this.checkUser("updateLoggedInUser");
await this.guardAccess({ kind: "user", subject: user }, "update");

//hang on to user profile before it's overwritten for analytics below
const oldProfile = User.getProfile(user);

const allowedFields: (keyof User)[] = ["avatarUrl", "fullName", "additionalData"];
for (const p of allowedFields) {
if (p in partialUser) {
Expand All @@ -429,6 +432,21 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
}

await this.userDB.updateUserPartial(user);

//track event and user profile if profile of partialUser changed
const newProfile = User.getProfile(user);
if (User.Profile.hasChanges(oldProfile, newProfile)) {
this.analytics.track({
userId: user.id,
event: "profile_changed",
properties: { new: newProfile, old: oldProfile },
});
this.analytics.identify({
userId: user.id,
traits: { email: newProfile.email, company: newProfile.company, name: newProfile.name },
});
}

return user;
}

Expand Down