Skip to content
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

chore: product analytics #962

Merged
merged 4 commits into from
Aug 12, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/generate-pot-file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
workflow_dispatch:

jobs:
regeneratee-pot-file:
regenerate-pot-file:
name: Release
runs-on: ubuntu-latest
strategy:
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
<script setup>
import { Toasts } from 'frappe-ui'
import { Dialogs } from '@/utils/dialogs'
import { computed, defineAsyncComponent } from 'vue'
import { computed, onMounted, onUnmounted } from 'vue'
import { useScreenSize } from './utils/composables'
import DesktopLayout from './components/DesktopLayout.vue'
import MobileLayout from './components/MobileLayout.vue'
import { stopSession } from '@/telemetry'
import { init as initTelemetry } from '@/telemetry'

const screenSize = useScreenSize()

Expand All @@ -22,4 +24,12 @@ const Layout = computed(() => {
return DesktopLayout
}
})

onMounted(async () => {
await initTelemetry()
})

onUnmounted(() => {
stopSession()
})
</script>
2 changes: 1 addition & 1 deletion frontend/src/components/BatchOverlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<router-link
v-if="isModerator"
:to="{
name: 'BatchCreation',
name: 'BatchForm',
params: {
batchName: batch.data.name,
},
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/Controls/Link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ const valuePropPassed = computed(() => 'value' in attrs)
const value = computed({
get: () => (valuePropPassed.value ? attrs.value : props.modelValue),
set: (val) => {
console.log(valuePropPassed.value)
return (
val?.value &&
emit(valuePropPassed.value ? 'change' : 'update:modelValue', val?.value)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/CourseCardOverlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<router-link
v-if="user?.data?.is_moderator || is_instructor()"
:to="{
name: 'CreateCourse',
name: 'CourseForm',
params: {
courseName: course.data.name,
},
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/components/LessonPlugins.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ const props = defineProps({
})

const addQuiz = (value) => {
console.log('here')
console.log(value)
getCurrentEditor().caret.setToLastBlock('end', 0)
if (value) {
getCurrentEditor().blocks.insert('quiz', {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/Modals/ChapterModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { Dialog, FormControl, createResource } from 'frappe-ui'
import { defineModel, reactive, watch } from 'vue'
import { createToast } from '@/utils/'
import { capture } from '@/telemetry'

const show = defineModel()
const outline = defineModel('outline')
Expand Down Expand Up @@ -91,6 +92,7 @@ const addChapter = (close) => {
}
},
onSuccess: (data) => {
capture('chapter_created')
chapterReference.submit(
{ name: data.name },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ import MultiSelect from '@/components/Controls/MultiSelect.vue'
import { useRouter } from 'vue-router'
import { getFileSize, showToast } from '../utils'
import { X, FileText } from 'lucide-vue-next'
import { capture } from '@/telemetry'

const router = useRouter()
const user = inject('$user')
Expand Down Expand Up @@ -274,6 +275,8 @@ onMounted(() => {
if (!user.data) window.location.href = '/login'
if (props.batchName != 'new') {
batchDetail.reload()
} else {
capture('batch_form_opened')
}
window.addEventListener('keydown', keyboardShortcut)
})
Expand Down Expand Up @@ -377,6 +380,7 @@ const createNewBatch = () => {
{},
{
onSuccess(data) {
capture('batch_created')
router.push({
name: 'BatchDetail',
params: {
Expand Down Expand Up @@ -447,7 +451,7 @@ const breadcrumbs = computed(() => {
}
crumbs.push({
label: props.batchName == 'new' ? 'New Batch' : 'Edit Batch',
route: { name: 'BatchCreation', params: { batchName: props.batchName } },
route: { name: 'BatchForm', params: { batchName: props.batchName } },
})
return crumbs
})
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Batches.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<router-link
v-if="user.data?.is_moderator"
:to="{
name: 'BatchCreation',
name: 'BatchForm',
params: { batchName: 'new' },
}"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ import { FileText, X } from 'lucide-vue-next'
import { useRouter } from 'vue-router'
import CourseOutline from '@/components/CourseOutline.vue'
import MultiSelect from '@/components/Controls/MultiSelect.vue'
import { capture } from '@/telemetry'

const user = inject('$user')
const newTag = ref('')
Expand Down Expand Up @@ -268,6 +269,8 @@ onMounted(() => {

if (props.courseName !== 'new') {
courseResource.reload()
} else {
capture('course_form_opened')
}
window.addEventListener('keydown', keyboardShortcut)
})
Expand Down Expand Up @@ -388,9 +391,10 @@ const submitCourse = () => {
} else {
courseCreationResource.submit(course, {
onSuccess(data) {
capture('course_created')
showToast('Success', 'Course created successfully', 'check')
router.push({
name: 'CreateCourse',
name: 'CourseForm',
params: { courseName: data.name },
})
},
Expand Down Expand Up @@ -489,7 +493,7 @@ const breadcrumbs = computed(() => {
}
crumbs.push({
label: props.courseName == 'new' ? 'New Course' : 'Edit Course',
route: { name: 'CreateCourse', params: { courseName: props.courseName } },
route: { name: 'CourseForm', params: { courseName: props.courseName } },
})
return crumbs
})
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Courses.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</div>
<router-link
:to="{
name: 'CreateCourse',
name: 'CourseForm',
params: {
courseName: 'new',
},
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/pages/LessonForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import EditorJS from '@editorjs/editorjs'
import LessonPlugins from '@/components/LessonPlugins.vue'
import { ChevronRight } from 'lucide-vue-next'
import { updateDocumentTitle, createToast, getEditorTools } from '@/utils'
import { capture } from '@/telemetry'

const editor = ref(null)
const instructorEditor = ref(null)
Expand All @@ -108,6 +109,7 @@ onMounted(() => {
if (!user.data?.is_moderator && !user.data?.is_instructor) {
window.location.href = '/login'
}
capture('lesson_form_opened')
editor.value = renderEditor('content')
instructorEditor.value = renderEditor('instructor-notes')
})
Expand Down Expand Up @@ -360,6 +362,7 @@ const createNewLesson = () => {
{ lesson: data.name },
{
onSuccess() {
capture('lesson_created')
showToast('Success', 'Lesson created successfully', 'check')
lessonDetails.reload()
},
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const routes = [
},
{
path: '/courses/:courseName/edit',
name: 'CreateCourse',
component: () => import('@/pages/CreateCourse.vue'),
name: 'CourseForm',
component: () => import('@/pages/CourseForm.vue'),
props: true,
},
{
Expand All @@ -109,8 +109,8 @@ const routes = [
},
{
path: '/batches/:batchName/edit',
name: 'BatchCreation',
component: () => import('@/pages/BatchCreation.vue'),
name: 'BatchForm',
component: () => import('@/pages/BatchForm.vue'),
props: true,
},
{
Expand Down
98 changes: 98 additions & 0 deletions frontend/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { useStorage } from "@vueuse/core";
import { call } from "frappe-ui";
import "../../../frappe/frappe/public/js/lib/posthog.js";

const APP = "lms";
const SITENAME = window.location.hostname;

declare global {
interface Window {
posthog: any;
}
}

const telemetry = useStorage("telemetry", {
enabled: false,
project_id: "",
host: "",
});

export async function init() {
await set_enabled();
if (!telemetry.value.enabled) return;
try {
await set_credentials();
window.posthog.init(telemetry.value.project_id, {
api_host: telemetry.value.host,
autocapture: false,
person_profiles: "always",
capture_pageview: true,
capture_pageleave: true,
disable_session_recording: false,
session_recording: {
maskAllInputs: false,
maskInputOptions: {
password: true,
},
},
loaded: (posthog) => {
window.posthog = posthog;
window.posthog.identify(SITENAME);
},
});
} catch (e) {
console.trace("Failed to initialize telemetry", e);
telemetry.value.enabled = false;
}
}

async function set_enabled() {
if (telemetry.value.enabled) return;

await call("lms.lms.telemetry.is_enabled").then((res) => {
telemetry.value.enabled = res;
});
}

async function set_credentials() {
if (!telemetry.value.enabled) return;
if (telemetry.value.project_id && telemetry.value.host) return;

await call("lms.lms.telemetry.get_credentials").then((res) => {
telemetry.value.project_id = res.project_id;
telemetry.value.host = res.telemetry_host;
});
}

interface CaptureOptions {
data: {
user: string;
[key: string]: string | number | boolean | object;
};
}

export function capture(
event: string,
options: CaptureOptions = { data: { user: "" } }
) {
if (!telemetry.value.enabled) return;
window.posthog.capture(`${APP}_${event}`, options);
}

export function recordSession() {
if (!telemetry.value.enabled) return;
if (window.posthog && window.posthog.__loaded) {
window.posthog.startSessionRecording();
}
}

export function stopSession() {
if (!telemetry.value.enabled) return;
if (
window.posthog &&
window.posthog.__loaded &&
window.posthog.sessionRecordingStarted()
) {
window.posthog.stopSessionRecording();
}
}
4 changes: 2 additions & 2 deletions frontend/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,15 @@ export function getSidebarLinks() {
'Courses',
'CourseDetail',
'Lesson',
'CreateCourse',
'CourseForm',
'LessonForm',
],
},
{
label: 'Batches',
icon: 'Users',
to: 'Batches',
activeFor: ['Batches', 'BatchDetail', 'Batch', 'BatchCreation'],
activeFor: ['Batches', 'BatchDetail', 'Batch', 'BatchForm'],
},
{
label: 'Certified Participants',
Expand Down
1 change: 1 addition & 0 deletions lms/lms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from frappe import _
from frappe.query_builder import DocType
from frappe.query_builder.functions import Count
from frappe.utils import time_diff, now_datetime, get_datetime


@frappe.whitelist()
Expand Down
18 changes: 18 additions & 0 deletions lms/lms/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import frappe


@frappe.whitelist()
def is_enabled():
return bool(
frappe.get_system_settings("enable_telemetry")
and frappe.conf.get("posthog_host")
and frappe.conf.get("posthog_project_id")
)


@frappe.whitelist()
def get_credentials():
return {
"project_id": frappe.conf.get("posthog_project_id"),
"telemetry_host": frappe.conf.get("posthog_host"),
}
Loading
Loading