Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
update analytics metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
wceolin committed Jun 23, 2020
1 parent 14a80f5 commit ca0d4c7
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 31 deletions.
22 changes: 4 additions & 18 deletions src/components/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import { db } from '@zoonk/firebase/db';
import { performance } from '@zoonk/firebase/performance';
import { User } from '@zoonk/models';
import { logIPAddress } from '@zoonk/services/users';
import {
appLanguage,
AuthContext,
isProduction,
logEvent,
pageview,
setUserID,
} from '@zoonk/utils';
import { AuthContext, pageview } from '@zoonk/utils';

const Auth = () => {
const { user, setProfile, setUser } = useContext(AuthContext);
Expand All @@ -36,7 +29,6 @@ const Auth = () => {

// Store the user data to be saved in the AuthContext.
setUser({ ...fbUser, uid: authState.uid });
setUserID(authState.uid);
}
});
}
Expand Down Expand Up @@ -82,7 +74,7 @@ const Auth = () => {
* IP address in case of multiple violations.
*/
useEffect(() => {
if (authState && isProduction) {
if (authState) {
logIPAddress();
}
}, [authState]);
Expand All @@ -94,14 +86,8 @@ const Auth = () => {
useEffect(() => {
const handleRouteChange = (url: string) => {
// Don't log data when the user is an admin.
if (user?.role === 'admin' || !isProduction) return;
pageview(url);
logEvent({
action: 'view',
category: 'app_language',
label: appLanguage,
value: 1,
});
if (user?.role === 'admin') return;
pageview(url, user?.uid);
};

Router.events.on('routeChangeComplete', handleRouteChange);
Expand Down
6 changes: 6 additions & 0 deletions src/components/PostCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const PostCreate = ({
}
}, [chapterId]);

// Log when a user started creating a post
useEffect(() => {
const start = new Date().getTime();
localStorage.setItem('postStart', String(start));
}, []);

if (!user || !profile) {
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion src/services/chapters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db, timestamp } from '@zoonk/firebase/db';
import { Chapter, Profile } from '@zoonk/models';
import { generateRandomSlug } from '@zoonk/utils';
import { generateRandomSlug, logEdit } from '@zoonk/utils';
import { updateTopic } from './topics';
import { serializeChapter } from '../serializers';

Expand All @@ -19,13 +19,15 @@ export const createChapter = async (data: Chapter.Create): Promise<string> => {
const { title } = data;
const slug = generateRandomSlug(title);
await db.doc(`chapters/${slug}`).set(data);
logEdit('chapters', 'add', data.createdById);
return slug;
};

export const updateChapter = (
data: Chapter.Update,
id: string,
): Promise<void> => {
logEdit('chapters', 'edit', data.updatedById);
return db.doc(`chapters/${id}`).update(data);
};

Expand Down
3 changes: 2 additions & 1 deletion src/services/comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db, timestamp } from '@zoonk/firebase/db';
import { Comment, Profile } from '@zoonk/models';
import { appLanguage } from '@zoonk/utils';
import { appLanguage, logEdit } from '@zoonk/utils';
import { serializeComment } from '../serializers';
import { updatePost } from './posts';

Expand All @@ -18,6 +18,7 @@ const commentConverter: firebase.firestore.FirestoreDataConverter<Comment.Get> =
export const createComment = (
comment: Comment.Create,
): Promise<firebase.firestore.DocumentReference> => {
logEdit('comments', 'add', comment.createdById);
return db.collection('comments').add(comment);
};

Expand Down
3 changes: 3 additions & 0 deletions src/services/followers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db, timestamp } from '@zoonk/firebase/db';
import { Follower } from '@zoonk/models';
import { logFollow } from '@zoonk/utils';
import { serializeFollower } from '../serializers';

const followerConverter: firebase.firestore.FirestoreDataConverter<Follower.Get> = {
Expand Down Expand Up @@ -58,6 +59,7 @@ export const follow = (
id: string,
userId: string,
): Promise<void> => {
logFollow('follow', collection, id, userId);
return db
.doc(`${collection}/${id}/followers/${userId}`)
.set({ joined: timestamp });
Expand All @@ -68,6 +70,7 @@ export const unfollow = (
id: string,
userId: string,
): Promise<void> => {
logFollow('unfollow', collection, id, userId);
return db.doc(`${collection}/${id}/followers/${userId}`).delete();
};

Expand Down
4 changes: 3 additions & 1 deletion src/services/groups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db, timestamp } from '@zoonk/firebase/db';
import { Group, Profile } from '@zoonk/models';
import { appLanguage, getRandomId, generateSlug } from '@zoonk/utils';
import { appLanguage, getRandomId, generateSlug, logEdit } from '@zoonk/utils';
import { serializeGroup } from '../serializers';

const groupConverter: firebase.firestore.FirestoreDataConverter<Group.Get> = {
Expand Down Expand Up @@ -32,10 +32,12 @@ export const createGroup = async (group: Group.Create): Promise<string> => {
}

await db.doc(`groups/${id}`).set(group);
logEdit('groups', 'add', group.createdById);
return id;
};

export const updateGroup = (group: Group.Update, id: string): Promise<void> => {
logEdit('groups', 'edit', group.updatedById);
return db.doc(`groups/${id}`).update(group);
};

Expand Down
2 changes: 2 additions & 0 deletions src/services/likes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db } from '@zoonk/firebase/db';
import { ItemLike } from '@zoonk/models';
import { logLike } from '@zoonk/utils';

/**
* Check if a user has liked an item or not.
Expand All @@ -25,6 +26,7 @@ export const toggleLike = (
uid: string,
current: boolean,
): Promise<void> => {
logLike(!current, itemPath, uid);
return db
.doc(`${itemPath}/likes/${uid}`)
.set({ like: !current }, { merge: true });
Expand Down
19 changes: 18 additions & 1 deletion src/services/posts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { arrayRemove, arrayUnion, db, timestamp } from '@zoonk/firebase/db';
import { functions } from '@zoonk/firebase/functions';
import { ChapterProgress, Dictionary, Post, Profile } from '@zoonk/models';
import { appLanguage, generateRandomSlug } from '@zoonk/utils';
import {
appLanguage,
generateRandomSlug,
logEdit,
logPostCreation,
} from '@zoonk/utils';
import { serializePost } from '../serializers';
import { getChapter, updateChapter } from './chapters';
import { getTopic } from './topics';
Expand All @@ -20,10 +25,21 @@ export const postConverter: firebase.firestore.FirestoreDataConverter<Post.Get>
export const createPost = async (data: Post.Create): Promise<string> => {
const slug = generateRandomSlug(data.title);
await db.doc(`posts/${slug}`).set(data);
logEdit(data.category, 'add', data.createdById);

// Log how long a user took to create a post. This is used to improve the UX.
const start = localStorage.getItem('postStart');
if (start) {
const end = new Date().getTime();
const time = end - Number(start);
logPostCreation(time, data.category);
}

return slug;
};

export const updatePost = (data: Post.Update, id: string): Promise<void> => {
logEdit('posts', 'edit', data.updatedById);
return db.doc(`posts/${id}`).update(data);
};

Expand All @@ -50,6 +66,7 @@ export const deletePost = async (
},
id,
);
logEdit('posts', 'delete', editorId);
return db.doc(`posts/${id}`).delete();
};

Expand Down
4 changes: 3 additions & 1 deletion src/services/topics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db } from '@zoonk/firebase/db';
import { Topic } from '@zoonk/models';
import { appLanguage } from '@zoonk/utils';
import { appLanguage, logEdit } from '@zoonk/utils';
import { serializeTopic } from '../serializers';

const topicConverter: firebase.firestore.FirestoreDataConverter<Topic.Get> = {
Expand All @@ -15,10 +15,12 @@ const topicConverter: firebase.firestore.FirestoreDataConverter<Topic.Get> = {
};

export const createTopic = (topic: Topic.Create, id: string): Promise<void> => {
logEdit('topics', 'add', topic.createdById);
return db.doc(`topics/${id}`).set(topic);
};

export const updateTopic = (topic: Topic.Update, id: string): Promise<void> => {
logEdit('topics', 'edit', topic.updatedById);
return db.doc(`topics/${id}`).update(topic);
};

Expand Down
2 changes: 2 additions & 0 deletions src/services/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { storage } from '@zoonk/firebase/storage';
import { logImageUpload } from '@zoonk/utils';

/**
* Upload a file to Firebase.
Expand All @@ -13,6 +14,7 @@ export const upload = async (file: File, folder: string): Promise<string> => {
// It uses the current timestamp to make sure we're creating a unique file.
const fileUpload = ref.child(`${folder}/${file.name}-${now}`).put(file);
const snap = await fileUpload;
logImageUpload(folder);

// Return a string containing the file URL.
return snap.ref.getDownloadURL();
Expand Down
5 changes: 5 additions & 0 deletions src/services/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { auth } from '@zoonk/firebase/auth';
import { db } from '@zoonk/firebase/db';
import { functions } from '@zoonk/firebase/functions';
import { Notification, User } from '@zoonk/models';
import { logSignIn, logSignUp } from '@zoonk/utils';

/**
* Log a user's IP address to the database.
Expand All @@ -18,6 +19,7 @@ export const signIn = (
email: string,
password: string,
): Promise<firebase.auth.UserCredential> => {
logSignIn('email');
return auth.signInWithEmailAndPassword(email, password);
};

Expand All @@ -30,6 +32,7 @@ export const signOut = () => {
*/
export const signInWithFacebook = (): Promise<firebase.auth.UserCredential> => {
const provider = new firebase.auth.FacebookAuthProvider();
logSignIn('facebook');
return auth.signInWithPopup(provider);
};

Expand All @@ -38,6 +41,7 @@ export const signInWithFacebook = (): Promise<firebase.auth.UserCredential> => {
*/
export const signInWithGoogle = (): Promise<firebase.auth.UserCredential> => {
const provider = new firebase.auth.GoogleAuthProvider();
logSignIn('google');
return auth.signInWithPopup(provider);
};

Expand Down Expand Up @@ -77,5 +81,6 @@ export const resetPassword = async (email: string) => {
};

export const signUp = (email: string, password: string) => {
logSignUp('email');
return auth.signInWithEmailAndPassword(email, password);
};
Loading

1 comment on commit ca0d4c7

@vercel
Copy link

@vercel vercel bot commented on ca0d4c7 Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.