Skip to content

Commit

Permalink
fix: #480 - analytics fixes + course.id => course.courseId
Browse files Browse the repository at this point in the history
  • Loading branch information
edeleastar committed Sep 8, 2023
1 parent 7d44317 commit f218845
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
45 changes: 17 additions & 28 deletions app/src/lib/services/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { updateLo } from "$lib/services/utils/course";
import type { Course, Lo } from "$lib/services/models/lo-types";
import type { TokenResponse } from "$lib/services/types/auth";
import { currentCourse, currentLo, currentUser } from "$lib/stores";
import { currentCourse, currentLo, currentUser, onlineStatus } from "$lib/stores";

import {
readValue,
sanitise,
updateCalendar,
updateCount,
updateCountValue,
updateLastAccess,
updateStr,
updateVisits
} from "$lib/services/utils/firebase";
import { readValue, sanitise, updateCalendar, updateCount, updateCountValue, updateLastAccess, updateStr, updateVisits } from "$lib/services/utils/firebase";

let course: Course;
let user: TokenResponse;
Expand Down Expand Up @@ -40,53 +31,51 @@ export const analyticsService = {

setOnlineStatus(status: boolean, session: TokenResponse) {
const onlineStatus = status ? "online" : "offline";
const key = `${course.id}/users/${sanitise(session.user.email)}/onlineStatus`;
const key = `${course.courseId}/users/${sanitise(session.user.email)}/onlineStatus`;
updateStr(key, onlineStatus);
},

async getOnlineStatus(course: Course, session: TokenResponse): Promise<string> {
if (!course || !user) {
return "online";
}
const courseId = course.url.substring(0, course.url.indexOf("."));
const courseId = course.courseUrl.substring(0, course.courseUrl.indexOf("."));
const key = `${courseId}/users/${sanitise(session.user.email)}/onlineStatus`;
const status = await readValue(key);
return status || "online";
},

reportPageLoad(session: TokenResponse) {
if (!lo) return;
updateLastAccess(`${course.id}/usage/${this.loRoute}`, course.title);
updateLastAccess(`${course.courseId}/usage/${this.loRoute}`, course.title);
updateVisits(course.courseUrl.substring(0, course.courseUrl.indexOf(".")));

if (!session || (session && session.onlineStatus === "online")) {
updateLastAccess(`all-course-access/${course.id}`, course.title);
updateVisits(`all-course-access/${course.id}`);
updateLo(`all-course-access/${course.id}`, course, lo);
updateLastAccess(`all-course-access/${course.courseId}`, course.title);
updateVisits(`all-course-access/${course.courseId}`);
updateLo(`all-course-access/${course.courseId}`, course, lo);
}

if (session) {
const key = `${course.courseUrl.substring(0, course.courseUrl.indexOf("."))}/users/${sanitise(
session.user.email
)}/${this.loRoute}`;
const key = `${course.courseUrl.substring(0, course.courseUrl.indexOf("."))}/users/${sanitise(session.user.email)}/${this.loRoute}`;
updateLastAccess(key, lo.title);
updateVisits(key);
}
},

updatePageCount(session: TokenResponse) {
if (!lo) return;
updateLastAccess(`${course.id}/usage/${this.loRoute}`, course.title);
updateCount(course.id);
if (user) {
updateCount(`all-course-access/${course.id}`);
if (user.onlineStatus === "online") {
updateLo(`all-course-access/${course.id}`, course, lo);
updateLastAccess(`${course.courseId}/usage/${this.loRoute}`, course.title);
updateCount(course.courseId);
if (session.user) {
updateCount(`all-course-access/${course.courseId}`);
if (onlineStatus) {
updateLo(`all-course-access/${course.courseId}`, course, lo);
}
const key = `${course.id}/users/${sanitise(session.user.email)}/${this.loRoute}`;
const key = `${course.courseId}/users/${sanitise(session.user.email)}/${this.loRoute}`;
updateLastAccess(key, lo.title);
updateCount(key);
updateCalendar(`${course.id}/users/${sanitise(session.user.email)}`);
updateCalendar(`${course.courseId}/users/${sanitise(session.user.email)}`);
}
},

Expand Down
9 changes: 2 additions & 7 deletions app/src/lib/services/tutors-startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@ export async function initServices(session: Token) {
if (get(currentCourse)) {
const course = get(currentCourse);
if (session && course) {
analyticsService.updateLogin(course.id, session);
analyticsService.updateLogin(course.courseId, session);
}
}
}
}

page.subscribe((path) => {
transitionKey.set(path.url.pathname);
if (
path.url.pathname.includes("book") ||
path.url.pathname.includes("pdf") ||
path.url.pathname.includes("video") ||
path.url.pathname.includes("note")
) {
if (path.url.pathname.includes("book") || path.url.pathname.includes("pdf") || path.url.pathname.includes("video") || path.url.pathname.includes("note")) {
transitionKey.set("none");
}
});
Expand Down
4 changes: 2 additions & 2 deletions app/src/routes/(course-reader)/course/[courseid]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
onMount(async () => {
if (data.course.authLevel > 0) {
if (!session) {
localStorage.setItem("course_url", data.course.url);
localStorage.setItem("course_url", data.course.courseUrl);
localStorage.setItem("isAuthenticating", "true");
goto("/auth");
} else {
session.onlineStatus = await analyticsService.getOnlineStatus(data.course, session);
// analyticsService.updateLogin(data.course.id, data.session);
// analyticsService.updateLogin(data.course.courseId, data.session);
}
}
Expand Down

0 comments on commit f218845

Please sign in to comment.