diff --git a/lms/djangoapps/certificates/generation.py b/lms/djangoapps/certificates/generation.py index 9e438f9ff2..382a0e8a94 100644 --- a/lms/djangoapps/certificates/generation.py +++ b/lms/djangoapps/certificates/generation.py @@ -16,7 +16,11 @@ from common.djangoapps.student.models import CourseEnrollment, UserProfile from lms.djangoapps.certificates.data import CertificateStatuses from lms.djangoapps.certificates.models import GeneratedCertificate from lms.djangoapps.certificates.queue import XQueueCertInterface -from lms.djangoapps.certificates.utils import emit_certificate_event, has_html_certificates_enabled +from lms.djangoapps.certificates.utils import ( + emit_certificate_event, + emit_segment_event, + has_html_certificates_enabled +) from lms.djangoapps.grades.api import CourseGradeFactory from lms.djangoapps.instructor.access import list_with_level from openedx.core.djangoapps.content.course_overviews.api import get_course_overview_or_none @@ -50,6 +54,7 @@ def generate_course_certificate(user, course_key, generation_mode): 'generation_mode': generation_mode } emit_certificate_event(event_name='created', user=user, course_id=course_key, event_data=event_data) + emit_segment_event(user_id=user.id, course_id=course_key) return cert diff --git a/lms/djangoapps/certificates/tests/test_generation.py b/lms/djangoapps/certificates/tests/test_generation.py index 139c89bc52..6f5ef1355f 100644 --- a/lms/djangoapps/certificates/tests/test_generation.py +++ b/lms/djangoapps/certificates/tests/test_generation.py @@ -2,11 +2,12 @@ Tests for certificate generation """ import logging +from unittest.mock import patch from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory from common.djangoapps.util.testing import EventTestMixin -from lms.djangoapps.certificates.generation import generate_course_certificate from lms.djangoapps.certificates.data import CertificateStatuses +from lms.djangoapps.certificates.generation import generate_course_certificate from lms.djangoapps.certificates.models import GeneratedCertificate from lms.djangoapps.certificates.tests.factories import GeneratedCertificateFactory from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -58,6 +59,27 @@ class CertificateTests(EventTestMixin, ModuleStoreTestCase): generation_mode=self.gen_mode ) + def test_segment_event(self): + """ + Test that a segment event is created + """ + analytics_patcher = patch('lms.djangoapps.certificates.utils.segment') + mock_tracker = analytics_patcher.start() + self.addCleanup(analytics_patcher.stop) + + generated_cert = generate_course_certificate(self.u, self.key, self.gen_mode) + assert generated_cert.status, CertificateStatuses.downloadable + + mock_tracker.track.assert_called_once_with( + self.u.id, + 'edx.bi.user.certificate.generate', + { + 'category': 'certificates', + 'label': str(self.key) + }, + ) + mock_tracker.reset_mock() + def test_generation_existing(self): """ Test certificate generation when a certificate already exists diff --git a/lms/djangoapps/certificates/utils.py b/lms/djangoapps/certificates/utils.py index 421a6772c5..853a7554c6 100644 --- a/lms/djangoapps/certificates/utils.py +++ b/lms/djangoapps/certificates/utils.py @@ -9,6 +9,7 @@ from django.urls import reverse from eventtracking import tracker from opaque_keys.edx.keys import CourseKey +from common.djangoapps.track import segment from lms.djangoapps.certificates.models import GeneratedCertificate from openedx.core.djangoapps.content.course_overviews.api import get_course_overview @@ -26,9 +27,9 @@ def emit_certificate_event(event_name, user, course_id, course_overview=None, ev a learner. - `edx.certificate.shared` - Emit when a learner shares their course certificate to social media (LinkedIn, Facebook, or Twitter). - - `edx.certificate.evidence_visisted` - Emit when a user (other than the learner who owns a certificate) views a - course certificate (e.g., someone views a course certificate shared on a - LinkedIn profile). + - `edx.certificate.evidence_visited` - Emit when a user (other than the learner who owns a certificate) views a + course certificate (e.g., someone views a course certificate shared on a + LinkedIn profile). Args: event_name (String) - Text describing the action/event that we are tracking. Examples include `revoked`, @@ -61,6 +62,23 @@ def emit_certificate_event(event_name, user, course_id, course_overview=None, ev tracker.emit(event_name, event_data) +def emit_segment_event(user_id, course_id): + """ + Track a successful certificate generation event in segment. + + Arguments: + user_id (str): The ID of the user associated with the certificate. + course_id (CourseKey): Identifier for the course. + Returns: + None + """ + event_name = 'edx.bi.user.certificate.generate' + segment.track(user_id, event_name, { + 'category': 'certificates', + 'label': str(course_id) + }) + + def get_certificate_url(user_id=None, course_id=None, uuid=None, user_certificate=None): """ Returns the certificate URL